OOP Design & Patterns

Preview — 3 of 10 questions

In which case will calling super cause an error in a subclass?

javascript
class Parent {
  constructor(name) {
    this.name = name;
  }
}

class Child extends Parent {
  constructor(name) {
    this.age = 10; // ReferenceError: Must call super before accessing 'this'
    super(name);
  }
}
AWhen super() is called after using this
BWhen the parent class has no constructor
CWhen super.method() is called, but the method does not exist in the parent class
DWhen super() is called inside a static method

What will be the output of the following code?

javascript
function Parent() {}
Parent.prototype.greet = function () {
  return "Hello";
};

function Child() {}
Child.prototype = Parent.prototype;

const instance = new Child();
console.log(instance.greet());
A"Hello"
BTypeError: instance.greet is not a function
Cundefined
DReferenceError: Parent is not defined

Given the following class, what is the correct output order?

javascript
class A {
  x = console.log("A property");
  constructor() {
    console.log("A constructor");
  }
}

class B extends A {
  y = console.log("B property");
  constructor() {
    super();
    console.log("B constructor");
  }
}

new B();
A"A constructor" → "B constructor" → "A property" → "B property"
B"A property" → "A constructor" → "B property" → "B constructor"
C"A property" → "B property" → "A constructor" → "B constructor"
D"A constructor" → "A property" → "B constructor" → "B property"

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.