TypeScript Assignment- 3

Basic Questions

  1. Create a TypeScript function addNumbers that accepts two numbers and returns their sum.
  2. Write a function greetUser that accepts a string name and returns “Hello, <name>”.
  3. Create a function multiply with two parameters, where the second parameter is optional.
  4. Write a function getFullName that takes firstName and lastName (with a default value of “Doe”) and returns the full name.
  5. Write a TypeScript function isEven that returns true if a number is even, else false.
  6. Define a class Car with properties brand and model and a method getDetails().
  7. Create a class Person with a constructor that initializes name and age, then log them.
  8. Write a class Circle with a property radius and a method getArea() that returns the area.
  9. Create a class Student with a readonly property rollNo and a method display().
  10. Write a class BankAccount with a private property balance and a method deposit().
  11. Define a class Animal with a method makeSound() that logs “Some sound”.
  12. Create a class Dog that extends Animal and overrides makeSound() to log “Woof!”.
  13. Write a class Bird that extends Animal and adds a method fly().
  14. Create an abstract class Shape with an abstract method getArea().
  15. Create a class Rectangle that extends Shape and implements getArea().
  16. Define an interface Playable with a method play(), then implement it in a class Guitar.
  17. Create an interface Drivable with a method drive() and implement it in a class Bike.
  18. Create a class Employee that implements two interfaces: Payable and Workable.
  19. Write a function divide that accepts two numbers and returns the result. Handle divide-by-zero by returning “Error”.
  20. Write a function sayHello that can take either a string name or no parameter (overloading).

Intermediate Questions

  1. Write a function calculateArea that accepts a number (for square) or two numbers (for rectangle) using function overloading.
  2. Create a class User with public properties username and email. Add a method getUserInfo().
  3. Define a class Laptop with private properties brand and price. Add getters and setters for them.
  4. Write a class Book with protected property title and a subclass EBook that accesses title.
  5. Create a class BankAccount with deposit() and withdraw() methods. Ensure balance cannot go negative.
  6. Write a class Teacher that has a constructor with default parameter subject = “Math”.
  7. Create a class Employee with a readonly property id and method displayDetails().
  8. Write a class Shape with method draw(). Extend it with Circle and Square classes that override draw().
  9. Define an abstract class Vehicle with abstract method move(). Create subclasses Car and Bike implementing move().
  10. Create an interface Logger with method log(message: string) and implement it in a class ConsoleLogger.
  11. Write a class Student that implements an interface Person with properties name and age.
  12. Create an interface Storable with methods save() and load(). Implement it in a class FileStorage.
  13. Write a function getMax that takes an array of numbers and returns the maximum.
  14. Write a function filterEven that takes an array of numbers and returns only even numbers.
  15. Write a class Counter with a private property count, methods increment(), decrement(), and getCount().
  16. Write a class Author with properties name, books[] and a method addBook().
  17. Create a class Employee with method calculateSalary(hoursWorked: number, hourlyRate: number) that returns total salary.
  18. Write a function mergeNames using union types that accepts a string or array of strings and returns a single concatenated string.
  19. Create a function getLength that accepts a string or array and returns its length.
  20. Write a class RemoteControl that implements two interfaces: Power (on/off) and Channel (next/previous).

Advanced Questions

  1. Create an abstract class Payment with abstract method processPayment(amount: number). Implement CreditCardPayment and UPIPayment classes.
  2. Write a class ShapeFactory that returns different Shape objects (Circle, Rectangle) based on input.
  3. Write a class ECommerceCart that has private items[], methods addItem(), removeItem(), and getTotalPrice().
  4. Write a class University where Department classes inherit common properties and override getDetails().
  5. Implement multiple inheritance using interfaces: Flyable, Swimmable, Walkable. Create a class Duck implementing all three.
  6. Write a function that accepts a class type and returns a new instance of that class (generic + constructor typing).
  7. Define an interface Database with methods connect(), disconnect(), and query(). Write TypeScript code that declares a class MongoDB which implements this interface but only provides empty method bodies (no logic inside).
  8. Create an abstract class Employee with abstract method calculateBonus(). Subclasses: Manager, Developer, Intern. Each calculates bonus differently.
  9. Write a class EventEmitter with methods on(event, callback), emit(event, data) to simulate Node.js EventEmitter.
  10. Write a program with class Game that loads Player and Team classes dynamically using inheritance and interfaces.