Challenge - 5 Problems
Abstract Methods Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of abstract method implementation
What is the output of this TypeScript code that uses an abstract method?
Typescript
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());
Attempts:
2 left
💡 Hint
Remember that abstract methods must be implemented in subclasses.
✗ Incorrect
The Dog class implements the abstract method makeSound, so calling dog.makeSound() returns "Bark". The move method is concrete and returns "Moving". The console logs print these in order.
❓ Predict Output
intermediate2:00remaining
What error occurs with missing abstract method implementation?
What error will this TypeScript code produce?
Typescript
abstract class Vehicle { abstract startEngine(): void; } class Car extends Vehicle { // Missing startEngine implementation } const myCar = new Car();
Attempts:
2 left
💡 Hint
Check if all abstract methods are implemented in subclasses.
✗ Incorrect
The Car class does not implement the abstract method startEngine, so TypeScript raises a compilation error.
🔧 Debug
advanced2:00remaining
Identify the error in abstract method usage
Find the error in this TypeScript code involving abstract methods.
Typescript
abstract class Shape { abstract area(): number; } class Circle extends Shape { radius: number; constructor(radius: number) { super(); this.radius = radius; } area(): number { return Math.PI * this.radius * this.radius; } } const shape = new Shape();
Attempts:
2 left
💡 Hint
Abstract classes cannot be instantiated directly.
✗ Incorrect
The code tries to create an instance of the abstract class Shape, which is not allowed and causes a compilation error.
📝 Syntax
advanced2:00remaining
Which option correctly declares an abstract method?
Which of the following is the correct syntax to declare an abstract method in a TypeScript abstract class?
Attempts:
2 left
💡 Hint
Look at the placement of the 'abstract' keyword.
✗ Incorrect
The correct syntax places 'abstract' before the method name and omits the method body.
🚀 Application
expert2:00remaining
Determine the number of abstract methods in the class
Given this TypeScript code, how many abstract methods does class B have after compilation?
Typescript
abstract class A { abstract foo(): void; abstract bar(): void; baz(): void {} } abstract class B extends A { foo(): void {} abstract qux(): void; }
Attempts:
2 left
💡 Hint
Count abstract methods not implemented in class B.
✗ Incorrect
Class B implements foo(), so it's no longer abstract there. bar() is not implemented in B, but it was abstract in A and not overridden, so it remains abstract in B. qux() is declared abstract in B. So B has 2 abstract methods: bar() and qux(). But since bar() is inherited abstract and not implemented, B remains abstract with 2 abstract methods. However, the question asks how many abstract methods does B have after compilation. In TypeScript, abstract methods inherited but not implemented remain abstract in subclass. So B has 2 abstract methods: bar() and qux(). The correct answer is 2.