What is the output of this TypeScript code?
abstract class Animal { abstract makeSound(): string; move(): string { return "Moving"; } } class Dog extends Animal { makeSound(): string { return "Bark"; } } const dog = new Dog(); console.log(dog.makeSound()); console.log(dog.move());
Remember that abstract classes cannot be instantiated directly, but their subclasses can be.
The class Dog extends the abstract class Animal and implements the abstract method makeSound. Calling dog.makeSound() returns "Bark" and dog.move() returns "Moving".
What is the main purpose of using an abstract class in TypeScript?
Think about how abstract classes help organize code and enforce method implementation.
Abstract classes allow you to define some common behavior and force subclasses to implement specific methods, ensuring a consistent interface.
What error will this TypeScript code produce?
abstract class Vehicle { abstract startEngine(): void; } const v = new Vehicle();
Can you create an object directly from an abstract class?
Abstract classes cannot be instantiated directly. Trying to create an instance of Vehicle causes a compile-time error.
Which option shows the correct syntax for declaring an abstract method in a TypeScript abstract class?
abstract class Shape { // Choose the correct abstract method declaration }
Abstract methods do not have a body and end with a semicolon.
Abstract methods are declared with the abstract keyword and have no implementation (no body). Option A is correct.
Given this abstract class and subclass, how many methods does the Circle class implement?
abstract class Shape { abstract area(): number; abstract perimeter(): number; description(): string { return "A shape"; } } class Circle extends Shape { radius: number; constructor(radius: number) { super(); this.radius = radius; } area(): number { return Math.PI * this.radius ** 2; } perimeter(): number { return 2 * Math.PI * this.radius; } }
Count only the methods that must be implemented by the subclass.
The abstract class Shape has two abstract methods: area and perimeter. The subclass Circle implements both. The description method is already implemented in the abstract class and does not need to be redefined.