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 two interfaces
What will be the output of the following TypeScript code?
Typescript
interface A {
greet(): string;
}
interface B {
farewell(): string;
}
class C implements A, B {
greet() {
return "Hello";
}
farewell() {
return "Goodbye";
}
}
const obj = new C();
console.log(obj.greet() + ", " + obj.farewell());Attempts:
2 left
💡 Hint
Check the methods implemented by class C and their return values.
✗ Incorrect
Class C implements both interfaces A and B by providing greet() and farewell() methods. Calling greet() returns "Hello" and farewell() returns "Goodbye", so the combined output is "Hello, Goodbye".
❓ Predict Output
intermediate1:30remaining
Value of property after implementing multiple interfaces
Given the following code, what is the value of obj.status after execution?
Typescript
interface X {
status: string;
}
interface Y {
status: string;
}
class Z implements X, Y {
status = "active";
}
const obj = new Z();Attempts:
2 left
💡 Hint
Consider how TypeScript handles properties with the same name in multiple interfaces.
✗ Incorrect
Both interfaces declare a property named status of type string. The class Z implements both and defines status as "active". This is valid and obj.status is "active".
🔧 Debug
advanced2:30remaining
Identify the error in class implementing multiple interfaces
What error will this TypeScript code produce?
Typescript
interface First {
doSomething(): void;
}
interface Second {
doSomething(): number;
}
class Test implements First, Second {
doSomething() {
return 42;
}
}Attempts:
2 left
💡 Hint
Check the return types of the method in both interfaces and the class.
✗ Incorrect
The method doSomething() in interface First returns void, but in Second it returns number. The class method returns number, which conflicts with First's void return type, causing a TypeScript error.
📝 Syntax
advanced1:30remaining
Correct syntax for implementing multiple interfaces
Which of the following is the correct syntax for a class implementing interfaces Alpha and Beta in TypeScript?
Attempts:
2 left
💡 Hint
Remember how TypeScript lists multiple interfaces in the implements clause.
✗ Incorrect
The correct syntax uses a comma-separated list of interfaces after implements. Using & or arrays or extends with multiple interfaces is invalid syntax.
🚀 Application
expert3:00remaining
Determine the number of methods in a class implementing multiple interfaces
Given these interfaces and class, how many methods does the class FinalClass have?
Typescript
interface One {
methodA(): void;
methodB(): void;
}
interface Two {
methodB(): void;
methodC(): void;
}
interface Three {
methodD(): void;
}
class FinalClass implements One, Two, Three {
methodA() {}
methodB() {}
methodC() {}
methodD() {}
}Attempts:
2 left
💡 Hint
Count unique method names implemented in the class.
✗ Incorrect
The class implements all methods from the three interfaces. methodB appears in two interfaces but is implemented once. Total unique methods are methodA, methodB, methodC, methodD = 4.