0
0
Typescriptprogramming~20 mins

Abstract classes in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Abstract Class Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of abstract class method call

What is the output of this TypeScript code?

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());
ABark\nundefined
BBark\nMoving
CError: Cannot create an instance of an abstract class
DMoving\nBark
Attempts:
2 left
💡 Hint

Remember that abstract classes cannot be instantiated directly, but their subclasses can be.

🧠 Conceptual
intermediate
1:30remaining
Purpose of abstract classes

What is the main purpose of using an abstract class in TypeScript?

ATo create objects directly without subclassing
BTo allow multiple inheritance of classes
CTo prevent any class from inheriting from it
DTo provide a base class with some implemented methods and some methods that must be implemented by subclasses
Attempts:
2 left
💡 Hint

Think about how abstract classes help organize code and enforce method implementation.

🔧 Debug
advanced
1:30remaining
Identify the error in abstract class usage

What error will this TypeScript code produce?

Typescript
abstract class Vehicle {
  abstract startEngine(): void;
}

const v = new Vehicle();
AError: Cannot create an instance of an abstract class
BError: Abstract method must have an implementation
CNo error, instance created successfully
DError: Missing constructor in abstract class
Attempts:
2 left
💡 Hint

Can you create an object directly from an abstract class?

📝 Syntax
advanced
1:30remaining
Correct syntax for abstract method

Which option shows the correct syntax for declaring an abstract method in a TypeScript abstract class?

Typescript
abstract class Shape {
  // Choose the correct abstract method declaration
}
Aabstract calculateArea(): number;
Babstract calculateArea() { return 0; }
CcalculateArea(): abstract number;
Dabstract calculateArea: () => number;
Attempts:
2 left
💡 Hint

Abstract methods do not have a body and end with a semicolon.

🚀 Application
expert
2:00remaining
Number of methods implemented in subclass

Given this abstract class and subclass, how many methods does the Circle class implement?

Typescript
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;
  }
}
A0
B3
C2
D1
Attempts:
2 left
💡 Hint

Count only the methods that must be implemented by the subclass.