0
0
Javascriptprogramming~20 mins

Method overriding in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Method Overriding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of overridden method in subclass
What is the output of the following JavaScript code?
Javascript
class Animal {
  speak() {
    return "Animal speaks";
  }
}

class Dog extends Animal {
  speak() {
    return "Dog barks";
  }
}

const pet = new Dog();
console.log(pet.speak());
A"Dog barks"
Bundefined
C"Animal speaks"
DTypeError
Attempts:
2 left
💡 Hint
Remember that the subclass method overrides the parent class method.
Predict Output
intermediate
2:00remaining
Calling super method in overridden method
What will this code output when run?
Javascript
class Vehicle {
  start() {
    return "Vehicle started";
  }
}

class Car extends Vehicle {
  start() {
    return super.start() + " and Car is ready";
  }
}

const myCar = new Car();
console.log(myCar.start());
AReferenceError
B"Car is ready"
C"Vehicle started and Car is ready"
D"Vehicle started"
Attempts:
2 left
💡 Hint
Look at how super.start() is used inside the overridden method.
Predict Output
advanced
2:00remaining
Overriding method with different parameters
What is the output of this code snippet?
Javascript
class Printer {
  print(message) {
    return `Printing: ${message}`;
  }
}

class ColorPrinter extends Printer {
  print(message, color) {
    return `Printing: ${message} in ${color}`;
  }
}

const cp = new ColorPrinter();
console.log(cp.print("Hello"));
A"Printing: Hello in undefined"
BTypeError
C"Printing: Hello"
DSyntaxError
Attempts:
2 left
💡 Hint
Check how JavaScript handles missing arguments in overridden methods.
Predict Output
advanced
2:00remaining
Overriding method and using polymorphism
What will be the output of this code?
Javascript
class Shape {
  area() {
    return 0;
  }
}

class Square extends Shape {
  constructor(side) {
    super();
    this.side = side;
  }
  area() {
    return this.side * this.side;
  }
}

class Circle extends Shape {
  constructor(radius) {
    super();
    this.radius = radius;
  }
  area() {
    return Math.PI * this.radius * this.radius;
  }
}

const shapes = [new Square(3), new Circle(2)];
const areas = shapes.map(shape => shape.area());
console.log(areas);
ATypeError
B[0, 0]
C[NaN, NaN]
D[9, 12.566370614359172]
Attempts:
2 left
💡 Hint
Each subclass overrides area() to calculate its own area.
Predict Output
expert
3:00remaining
Overriding method with dynamic dispatch and prototype chain
What is the output of this code?
Javascript
function Person() {}
Person.prototype.greet = function() {
  return "Hello from Person";
};

function Employee() {}
Employee.prototype = Object.create(Person.prototype);
Employee.prototype.greet = function() {
  return "Hello from Employee";
};

const emp = new Employee();
console.log(emp.greet());
console.log(Object.getPrototypeOf(emp) === Employee.prototype);
console.log(Object.getPrototypeOf(Object.getPrototypeOf(emp)) === Person.prototype);
A
"Hello from Person"
true
false
B
"Hello from Employee"
true
true
C
"Hello from Employee"
false
true
DTypeError
Attempts:
2 left
💡 Hint
Check the prototype chain and which greet method is called.