0
0
Javascriptprogramming~20 mins

Inheritance using classes in Javascript - Practice Problems & Coding Challenges

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

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

const pet = new Dog();
console.log(pet.speak());
Aundefined
BTypeError
C"Bark"
D"Animal sound"
Attempts:
2 left
💡 Hint
Look at which class's speak method is called by the Dog instance.
Predict Output
intermediate
2:00remaining
Output of super keyword in subclass method
What will this code print to the console?
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());
A"Vehicle started and car is ready"
B"Vehicle started"
C"Car started and car is ready"
DReferenceError
Attempts:
2 left
💡 Hint
super calls the method from the parent class.
🔧 Debug
advanced
2:00remaining
Identify the error in subclass constructor
What error will this code produce when run?
Javascript
class Person {
  constructor(name) {
    this.name = name;
  }
}

class Employee extends Person {
  constructor(name, id) {
    this.id = id;
    super(name);
  }
}

const emp = new Employee("Alice", 123);
console.log(emp.name, emp.id);
ATypeError: Cannot read property 'name' of undefined
BReferenceError: Must call super constructor before accessing 'this' in derived class
Cundefined 123
DAlice 123
Attempts:
2 left
💡 Hint
In subclass constructors, super() must be called before using 'this'.
Predict Output
advanced
2:00remaining
Output of static method inheritance
What will be printed by this code?
Javascript
class Parent {
  static greet() {
    return "Hello from Parent";
  }
}

class Child extends Parent {}

console.log(Child.greet());
Aundefined
B"Hello from Child"
CTypeError: Child.greet is not a function
D"Hello from Parent"
Attempts:
2 left
💡 Hint
Static methods are inherited by subclasses.
🧠 Conceptual
expert
3:00remaining
Understanding prototype chain in class inheritance
Given the following code, what is the value of Object.getPrototypeOf(dog) === Dog.prototype and Object.getPrototypeOf(Dog.prototype) === Animal.prototype?
Javascript
class Animal {}
class Dog extends Animal {}
const dog = new Dog();
Atrue and true
Bfalse and true
Ctrue and false
Dfalse and false
Attempts:
2 left
💡 Hint
Remember how JavaScript sets prototypes for instances and classes.