0
0
Typescriptprogramming~20 mins

Abstract methods in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Abstract Methods Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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());
A"Moving" followed by "Bark"
B"Bark" followed by "Moving"
CCompilation error because abstract method is not implemented
DRuntime error because abstract method cannot be called
Attempts:
2 left
💡 Hint
Remember that abstract methods must be implemented in subclasses.
Predict Output
intermediate
2: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();
ACompilation error: Abstract class 'Vehicle' cannot be instantiated.
BRuntime error: Cannot instantiate abstract class 'Vehicle'.
CNo error, code runs fine.
DCompilation error: Non-abstract class 'Car' does not implement inherited abstract member 'startEngine'.
Attempts:
2 left
💡 Hint
Check if all abstract methods are implemented in subclasses.
🔧 Debug
advanced
2: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();
AProperty 'radius' is not initialized.
BMissing implementation of abstract method 'area' in Circle.
CCannot create an instance of an abstract class 'Shape'.
DNo error, code runs correctly.
Attempts:
2 left
💡 Hint
Abstract classes cannot be instantiated directly.
📝 Syntax
advanced
2: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?
Aabstract class A { abstract method(): void; }
Babstract class A { method(): abstract void; }
Cabstract class A { void abstract method(); }
Dabstract class A { method(): void abstract; }
Attempts:
2 left
💡 Hint
Look at the placement of the 'abstract' keyword.
🚀 Application
expert
2: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;
}
A2
B0
C3
D1
Attempts:
2 left
💡 Hint
Count abstract methods not implemented in class B.