TypeScript Assignment- 3
Basic Questions
- Create a TypeScript function addNumbers that accepts two numbers and returns their sum.
- Write a function greetUser that accepts a string name and returns “Hello, <name>”.
- Create a function multiply with two parameters, where the second parameter is optional.
- Write a function getFullName that takes firstName and lastName (with a default value of “Doe”) and returns the full name.
- Write a TypeScript function isEven that returns true if a number is even, else false.
- Define a class Car with properties brand and model and a method getDetails().
- Create a class Person with a constructor that initializes name and age, then log them.
- Write a class Circle with a property radius and a method getArea() that returns the area.
- Create a class Student with a readonly property rollNo and a method display().
- Write a class BankAccount with a private property balance and a method deposit().
- Define a class Animal with a method makeSound() that logs “Some sound”.
- Create a class Dog that extends Animal and overrides makeSound() to log “Woof!”.
- Write a class Bird that extends Animal and adds a method fly().
- Create an abstract class Shape with an abstract method getArea().
- Create a class Rectangle that extends Shape and implements getArea().
- Define an interface Playable with a method play(), then implement it in a class Guitar.
- Create an interface Drivable with a method drive() and implement it in a class Bike.
- Create a class Employee that implements two interfaces: Payable and Workable.
- Write a function divide that accepts two numbers and returns the result. Handle divide-by-zero by returning “Error”.
- Write a function sayHello that can take either a string name or no parameter (overloading).
Intermediate Questions
- Write a function calculateArea that accepts a number (for square) or two numbers (for rectangle) using function overloading.
- Create a class User with public properties username and email. Add a method getUserInfo().
- Define a class Laptop with private properties brand and price. Add getters and setters for them.
- Write a class Book with protected property title and a subclass EBook that accesses title.
- Create a class BankAccount with deposit() and withdraw() methods. Ensure balance cannot go negative.
- Write a class Teacher that has a constructor with default parameter subject = “Math”.
- Create a class Employee with a readonly property id and method displayDetails().
- Write a class Shape with method draw(). Extend it with Circle and Square classes that override draw().
- Define an abstract class Vehicle with abstract method move(). Create subclasses Car and Bike implementing move().
- Create an interface Logger with method log(message: string) and implement it in a class ConsoleLogger.
- Write a class Student that implements an interface Person with properties name and age.
- Create an interface Storable with methods save() and load(). Implement it in a class FileStorage.
- Write a function getMax that takes an array of numbers and returns the maximum.
- Write a function filterEven that takes an array of numbers and returns only even numbers.
- Write a class Counter with a private property count, methods increment(), decrement(), and getCount().
- Write a class Author with properties name, books[] and a method addBook().
- Create a class Employee with method calculateSalary(hoursWorked: number, hourlyRate: number) that returns total salary.
- Write a function mergeNames using union types that accepts a string or array of strings and returns a single concatenated string.
- Create a function getLength that accepts a string or array and returns its length.
- Write a class RemoteControl that implements two interfaces: Power (on/off) and Channel (next/previous).
Advanced Questions
- Create an abstract class Payment with abstract method processPayment(amount: number). Implement CreditCardPayment and UPIPayment classes.
- Write a class ShapeFactory that returns different Shape objects (Circle, Rectangle) based on input.
- Write a class ECommerceCart that has private items[], methods addItem(), removeItem(), and getTotalPrice().
- Write a class University where Department classes inherit common properties and override getDetails().
- Implement multiple inheritance using interfaces: Flyable, Swimmable, Walkable. Create a class Duck implementing all three.
- Write a function that accepts a class type and returns a new instance of that class (generic + constructor typing).
- 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).
- Create an abstract class Employee with abstract method calculateBonus(). Subclasses: Manager, Developer, Intern. Each calculates bonus differently.
- Write a class EventEmitter with methods on(event, callback), emit(event, data) to simulate Node.js EventEmitter.
- Write a program with class Game that loads Player and Team classes dynamically using inheritance and interfaces.