0
0
Typescriptprogramming~20 mins

Polymorphism through interfaces in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Polymorphism Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of polymorphic method calls

What is the output of the following TypeScript code?

Typescript
interface Animal {
  speak(): string;
}

class Dog implements Animal {
  speak(): string {
    return "Woof!";
  }
}

class Cat implements Animal {
  speak(): string {
    return "Meow!";
  }
}

function makeSpeak(animal: Animal) {
  console.log(animal.speak());
}

const dog = new Dog();
const cat = new Cat();
makeSpeak(dog);
makeSpeak(cat);
AWoof!\nWoof!
BWoof!\nMeow!
CMeow!\nWoof!
DMeow!\nMeow!
Attempts:
2 left
💡 Hint

Look at which class instance is passed to makeSpeak and what speak() returns.

🧠 Conceptual
intermediate
1:30remaining
Understanding interface polymorphism

Which statement best describes polymorphism through interfaces in TypeScript?

ADifferent classes can implement the same interface and provide their own method behavior.
BInterfaces can only be used for type checking and cannot enforce method signatures.
CAn interface can inherit implementation from another interface.
DPolymorphism means a class can have multiple constructors.
Attempts:
2 left
💡 Hint

Think about how different classes can share the same method names but behave differently.

🔧 Debug
advanced
2:30remaining
Identify the runtime error in polymorphic code

What error will this TypeScript code produce when run?

Typescript
interface Shape {
  area(): number;
}

class Rectangle implements Shape {
  constructor(public width: number, public height: number) {}
  area(): number {
    return this.width * this.height;
  }
}

class Circle implements Shape {
  constructor(public radius: number) {}
  area(): number {
    return Math.PI * this.radius * this.radius;
  }
}

const shapes: Shape[] = [new Rectangle(5, 10), new Circle(3)];

shapes.forEach(shape => {
  if (shape instanceof Rectangle) {
    console.log(shape.area());
  } else {
    console.log(shape.diameter());
  }
});
AReferenceError: shape is not defined
BNo error, outputs area of rectangle and circle
CSyntaxError due to missing method in interface
DTypeError: shape.diameter is not a function
Attempts:
2 left
💡 Hint

Check if all methods called on shape exist in the classes.

📝 Syntax
advanced
2:00remaining
Correct interface implementation syntax

Which option correctly implements the Vehicle interface with a move() method?

Typescript
interface Vehicle {
  move(): string;
}
A
class Car implements Vehicle {
  move(): void {
    console.log("Car is moving");
  }
}
B
class Car implements Vehicle {
  move: () => "Car is moving";
}
C
class Car implements Vehicle {
  move() {
    return "Car is moving";
  }
}
D
class Car implements Vehicle {
  move(): string {
    console.log("Car is moving");
  }
}
Attempts:
2 left
💡 Hint

Check the method signature and return type matching the interface.

🚀 Application
expert
3:00remaining
Determine output with polymorphic array processing

Given the following code, what is the output?

Typescript
interface Logger {
  log(message: string): string;
}

class ConsoleLogger implements Logger {
  log(message: string): string {
    return `Console: ${message}`;
  }
}

class FileLogger implements Logger {
  log(message: string): string {
    return `File: ${message}`;
  }
}

function processLoggers(loggers: Logger[], msg: string): string[] {
  return loggers.map(logger => logger.log(msg));
}

const loggers: Logger[] = [new ConsoleLogger(), new FileLogger()];
const results = processLoggers(loggers, "Test message");
console.log(results.join(", "));
AConsole: Test message, File: Test message
BFile: Test message, Console: Test message
C["Console: Test message", "File: Test message"]
DTypeError: logger.log is not a function
Attempts:
2 left
💡 Hint

Look at the order of loggers in the array and what their log methods return.