Challenge - 5 Problems
Interface Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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());Attempts:
2 left
💡 Hint
Remember that the class implements the interface and provides the greet method.
✗ Incorrect
The class FriendlyGreeter implements the Greeter interface and defines the greet method returning the string "Hello, friend!". Calling greeter.greet() prints this string.
🧠 Conceptual
intermediate1:30remaining
Interface method implementation requirement
Which statement about implementing interfaces in TypeScript classes is true?
Attempts:
2 left
💡 Hint
Think about what happens if a class does not implement all interface methods.
✗ Incorrect
In TypeScript, a class that implements an interface must provide concrete implementations for all methods declared in that interface. Otherwise, TypeScript will report an error.
🔧 Debug
advanced2: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);
}
}Attempts:
2 left
💡 Hint
Check the method signature in the class versus the interface.
✗ Incorrect
The interface declares makeSound with no parameters, but the class method expects one parameter. This mismatch causes a TypeScript error about incompatible types.
📝 Syntax
advanced1:30remaining
Correct syntax for implementing multiple interfaces
Which option shows the correct way to implement two interfaces
Flyer and Swimmer in a class Duck?Attempts:
2 left
💡 Hint
Look at the syntax for multiple interface implementation in TypeScript.
✗ Incorrect
In TypeScript, multiple interfaces are implemented by listing them separated by commas after the implements keyword without brackets or braces.
🚀 Application
expert2: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());Attempts:
2 left
💡 Hint
Check how the class implements the interface and what the describe method returns.
✗ Incorrect
The Car class implements Vehicle and its describe method returns the string with the speed. The instance vehicle calls describe and outputs the string with speed 120.