0
0
Typescriptprogramming~20 mins

Implementing interfaces in classes in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Interface Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a class implementing an interface
What is the output of this TypeScript code when the greet method is called?
Typescript
interface Greeter {
  greet(): string;
}

class FriendlyGreeter implements Greeter {
  greet(): string {
    return "Hello, friend!";
  }
}

const greeter = new FriendlyGreeter();
console.log(greeter.greet());
A"Hello, friend!"
Bundefined
CTypeError: greeter.greet is not a function
DSyntaxError
Attempts:
2 left
💡 Hint
Remember that the class implements the interface and provides the greet method.
🧠 Conceptual
intermediate
1:30remaining
Interface method implementation requirement
Which statement about implementing interfaces in TypeScript classes is true?
AA class must implement all methods declared in the interface it implements.
BInterfaces automatically add methods to the class without implementation.
CA class can implement methods with different names than those in the interface.
DA class can implement only some methods of the interface and skip others.
Attempts:
2 left
💡 Hint
Think about what happens if a class does not implement all interface methods.
🔧 Debug
advanced
2:00remaining
Identify the error in interface implementation
What error will this TypeScript code produce?
Typescript
interface Animal {
  makeSound(): void;
}

class Dog implements Animal {
  makeSound(sound: string): void {
    console.log(sound);
  }
}
ANo error, code runs fine.
BError: Class 'Dog' incorrectly implements interface 'Animal'. Types of property 'makeSound' are incompatible.
CError: Missing return statement in method 'makeSound'.
DError: Interface 'Animal' cannot be implemented by class 'Dog'.
Attempts:
2 left
💡 Hint
Check the method signature in the class versus the interface.
📝 Syntax
advanced
1:30remaining
Correct syntax for implementing multiple interfaces
Which option shows the correct way to implement two interfaces Flyer and Swimmer in a class Duck?
A
class Duck implements {Flyer, Swimmer} {
  fly() {}
  swim() {}
}
B
class Duck implements [Flyer, Swimmer] {
  fly() {}
  swim() {}
}
C
class Duck implements Flyer, Swimmer {
  fly() {}
  swim() {}
}
D
class Duck implements Flyer & Swimmer {
  fly() {}
  swim() {}
}
Attempts:
2 left
💡 Hint
Look at the syntax for multiple interface implementation in TypeScript.
🚀 Application
expert
2:30remaining
Determine the output of interface and class interaction
Given the following code, what will be the output when console.log(vehicle.describe()) is executed?
Typescript
interface Vehicle {
  speed: number;
  describe(): string;
}

class Car implements Vehicle {
  speed: number;
  constructor(speed: number) {
    this.speed = speed;
  }
  describe(): string {
    return `Car speed is ${this.speed} km/h`;
  }
}

const vehicle: Vehicle = new Car(120);
console.log(vehicle.describe());
Aundefined
B"Vehicle speed is 120 km/h"
CTypeError: vehicle.describe is not a function
D"Car speed is 120 km/h"
Attempts:
2 left
💡 Hint
Check how the class implements the interface and what the describe method returns.