Prototypes & Classes Questions with Answers

13 practice questions on Prototypes & Classes (JavaScript) with answers and step-by-step solutions. Try 10 samples below, then practise all 13 free in the AptiNest app.

Sample questions

Question 1Easy

What is the output?

function Car() {}
Car.prototype.wheels = 4;
const c = new Car();
console.log(c.wheels, c.hasOwnProperty('wheels'));
  1. 4, true
  2. 4, false
  3. undefined, false
  4. undefined, true

Answer: B. 4, false

Explanation

c inherits wheels from Car.prototype, so it is readable but not an own property.

Step-by-step solution

  1. Lookup walks the prototype chain.
  2. wheels is on the prototype only.

Why the other options are wrong

  • A. wheels lives on the prototype.
  • C. Instances inherit from the prototype.
  • D. The property is found through the prototype chain.

Question 2Easy

What does the extends keyword do in a class declaration?

  1. Copies all parent methods onto each instance
  2. Imports the parent class from another file
  3. Makes the class abstract
  4. 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

  1. Child.prototype links to Parent.prototype.
  2. 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 3Easy

What is the output?

class A {
  constructor(n) { this.n = n; }
  hi() { return 'Hi ' + this.n; }
}
console.log(new A('Sam').hi());
  1. Hi undefined
  2. Hi Sam
  3. Hi n
  4. TypeError

Answer: B. Hi Sam

Explanation

The constructor stores n, and the method concatenates it.

Step-by-step solution

  1. new A runs the constructor.
  2. hi uses this.n = "Sam".

Why the other options are wrong

  • A. The constructor sets this.n.
  • C. this.n is the property value.
  • D. Class methods are called on instances.

Question 4Easy

What is the output?

class Animal { speak() { return 'generic'; } }
class Dog extends Animal { speak() { return super.speak() + ' woof'; } }
console.log(new Dog().speak());
  1. woof
  2. generic
  3. generic woof!
  4. generic woof

Answer: D. generic woof

Explanation

super.speak() calls the parent method, and the override appends " woof".

Step-by-step solution

  1. Dog overrides speak.
  2. super.speak() returns "generic".
  3. Concatenate " woof".

Why the other options are wrong

  • A. super.speak() is included.
  • B. The override adds more text.
  • C. The exact string has no exclamation mark.

Question 5Medium

Why must a derived class constructor call super() before using this?

  1. Because super() sets default values for all fields automatically
  2. Because this is undefined in all constructors
  3. Because super() makes the class static
  4. 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

  1. Derived constructors do not create this.
  2. super() runs the parent constructor.
  3. 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);
  1. true, true, true
  2. true, true, false
  3. false, true, true
  4. 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

  1. A.prototype.constructor is A.
  2. a.constructor resolves through the chain.
  3. 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 7Medium

What is the output?

class C {
  static count = 0;
  constructor() { C.count++; }
}
new C(); new C();
console.log(C.count, new C().count);
  1. 3, 3
  2. 2, 2
  3. 3, undefined
  4. 2, undefined

Answer: D. 2, undefined

Explanation

C.count is read first (2); then the third instance is created, but static members are not visible on instances, so its count is undefined.

Step-by-step solution

  1. Two instances: count = 2.
  2. C.count is read as 2.
  3. new C().count is undefined (static is on the class).

Why the other options are wrong

  • A. Static members are not available on instances.
  • B. The instance has no count property.
  • C. Arguments are evaluated left to right, so C.count is read before the third instance exists.

Question 8Medium

What is the output?

const base = { greet() { return 'hi ' + this.n; } };
const child = Object.create(base);
child.n = 'Z';
console.log(child.greet(), Object.getPrototypeOf(child) === base);
  1. hi undefined, true
  2. hi Z, false
  3. hi Z, true
  4. TypeError, true

Answer: C. hi Z, true

Explanation

Object.create(base) makes base the prototype; greet runs with this = child, so it prints "hi Z".

Step-by-step solution

  1. child inherits greet from base.
  2. this is child, so this.n = "Z".
  3. Prototype equals base.

Why the other options are wrong

  • A. this is child inside the call.
  • B. Object.create sets the prototype to base.
  • D. greet is found on the prototype.

Question 9Hard

What is the output?

const o = { n: 1 };
function F() {}
F.prototype = o;
const f = new F();
o.n = 2;
F.prototype = { n: 3 };
console.log(f.n, new F().n);
  1. 1, 3
  2. 2, 3
  3. 2, 2
  4. 3, 3

Answer: B. 2, 3

Explanation

f keeps its original prototype o (now n = 2); instances created after the reassignment use the new prototype with n = 3.

Step-by-step solution

  1. f links to o at creation.
  2. o.n changed to 2.
  3. Reassigning F.prototype affects only later instances.

Why the other options are wrong

  • A. The object is shared, so n changed to 2.
  • C. The new prototype object is used for new instances.
  • D. Existing instances keep their original prototype.

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. [1], ['own'], []
  2. [], ['own'], []
  3. [1], ['own'], ['own']
  4. [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

  1. x.items.push mutates the shared array.
  2. y.items = ... creates an own property.
  3. 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.

Practise all 13 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all JavaScript topics · All topics