Question 2Easy
What does the extends keyword do in a class declaration?
- Copies all parent methods onto each instance
- Imports the parent class from another file
- Makes the class abstract
- Sets up prototype inheritance from the parent class
Answer: D. Sets up prototype inheritance from the parent class
Explanation
extends links the child prototype to the parent, so instances inherit methods through the chain.
Step-by-step solution
- Child.prototype links to Parent.prototype.
- Lookups walk up the chain.
Why the other options are wrong
- A. Methods are inherited via the prototype chain, not copied.
- B. Imports use the import statement.
- C. JavaScript has no abstract keyword.
Question 5Medium
Why must a derived class constructor call super() before using this?
- Because super() sets default values for all fields automatically
- Because this is undefined in all constructors
- Because super() makes the class static
- The parent constructor creates and initialises this for a derived class
Answer: D. The parent constructor creates and initialises this for a derived class
Explanation
In derived classes this is not created until super() runs, so touching this earlier throws a ReferenceError.
Step-by-step solution
- Derived constructors do not create this.
- super() runs the parent constructor.
- Then this becomes available.
Why the other options are wrong
- A. It only runs the parent constructor.
- B. this is defined once it is initialised.
- C. It has no effect on static members.
Question 6Medium
What is the output?
function A() {}
const a = new A();
console.log(a.constructor === A, A.prototype.constructor === A, Object.getPrototypeOf(a) === A.prototype);
- true, true, true
- true, true, false
- false, true, true
- true, false, true
Answer: A. true, true, true
Explanation
All three are true: a inherits constructor from A.prototype, which points to A, and a's prototype is A.prototype.
Step-by-step solution
- A.prototype.constructor is A.
- a.constructor resolves through the chain.
- Object.getPrototypeOf(a) is A.prototype.
Why the other options are wrong
- B. The prototype of a is A.prototype.
- C. a.constructor is found through the prototype.
- D. A.prototype.constructor points back to A.
Question 10Hard
What is the output?
function Parent() {}
Parent.prototype.items = [];
const x = new Parent(), y = new Parent();
x.items.push(1);
y.items = ['own'];
console.log(x.items, y.items, new Parent().items);
- [1], ['own'], []
- [], ['own'], []
- [1], ['own'], ['own']
- [1], ['own'], [1]
Answer: D. [1], ['own'], [1]
Explanation
x.items is the shared prototype array which now holds 1; y gets its own property; a new instance still sees the shared array [1].
Step-by-step solution
- x.items.push mutates the shared array.
- y.items = ... creates an own property.
- New instances read the shared array [1].
Why the other options are wrong
- A. Mutating the shared array affects all instances without an own property.
- B. The push mutated the shared array.
- C. The assignment creates an own property only on y.