JavaScript Prototype & Inheritance Quiz

JavaScript
0 Passed
0% acceptance

Tackle 75 questions covering prototype chain fundamentals, __proto__ vs prototype, constructor functions, adding and overriding prototype methods, Object.create patterns, class syntax with extends/super, distinguishing instance vs prototype data, checking inheritance relationships, understanding this, performance considerations, pitfalls, and best practices.

75 Questions
~150 minutes
1

Question 1

What does the prototype chain represent?

A
A linked list of objects used for property lookups
B
A set of CSS rules
C
A DOM tree
D
A module system
2

Question 2

Which property references the internal prototype of an object in modern spec terminology?

A
[[Prototype]]
B
__proto__ only
C
prototype
D
parentPrototype
3

Question 3

How do you check whether obj inherits from proto?

A
proto.isPrototypeOf(obj)
B
obj.prototype === proto
C
obj.__proto__ === proto.prototype
D
Object.hasOwn(obj, proto)
4

Question 4

What value terminates every prototype chain?

A
null
B
undefined
C
window
D
Object
5

Question 5

Which statement about Object.prototype is true?

A
It provides shared methods like toString and valueOf for objects
B
It contains DOM APIs
C
It is inaccessible
D
It cannot be modified
6

Question 6

What does Object.getPrototypeOf(obj) return?

A
The prototype object referenced by obj
B
The constructor function
C
A string
D
undefined for all objects
7

Question 7

What is the relationship between __proto__ and prototype?

A
__proto__ is an accessor on objects; prototype is a property on constructor functions
B
They are identical keywords
C
prototype exists on all objects
D
__proto__ is standard but prototype is not
8

Question 8

Which statement best describes property lookup?

A
JavaScript checks own properties first, then prototypes up the chain until null
B
The prototype is checked before own props
C
Only own properties matter
D
Prototype order is random
9

Question 9

What happens if two objects share the same prototype?

A
They inherit identical shared methods from that prototype
B
They become the same object
C
One overrides the other
D
They lose own properties
10

Question 10

What does overriding mean in prototypal inheritance?

A
Defining an own property that shadows a prototype property with the same name
B
Removing Object.prototype
C
Deleting __proto__
D
Cloning prototypes
11

Question 11

What does the following code log?

javascript
const base = { greet() { return 'hi' } }
const obj = Object.create(base)
console.log(obj.greet())
A
hi
B
undefined
C
Throws TypeError
D
base
12

Question 12

How can you confirm Object.create(null) produces a prototype-less object?

A
Object.getPrototypeOf(obj) === null
B
obj instanceof Object
C
obj.__proto__ === Object.prototype
D
Object.prototype.isPrototypeOf(obj)
13

Question 13

What does this print?

javascript
const proto = { value: 42 }
const obj = Object.create(proto)
obj.value = 7
console.log(obj.value, proto.value)
A
7 42
B
42 42
C
7 7
D
undefined undefined
14

Question 14

Why might you use Object.create over new Constructor()?

A
Object.create allows building objects from arbitrary prototypes without running a constructor
B
It is faster for all cases
C
Constructors cannot set prototypes
D
Object.create clones properties
15

Question 15

In the code below, what does child.__proto__ reference?

javascript
const parent = { role: 'parent' }
const child = Object.create(parent)
console.log(child.__proto__ === parent)
A
true, because parent is the prototype of child
B
false, __proto__ is always Object.prototype
C
Throws because __proto__ is deprecated
D
Undefined
16

Question 16

What is stored in FunctionName.prototype by default?

A
An object with a constructor property pointing back to the function
B
null
C
The function itself
D
An array of instances
17

Question 17

How do instances created via new Constructor() link to the prototype?

A
Their [[Prototype]] references Constructor.prototype
B
They copy properties from the prototype
C
They set __proto__ to null
D
They have no prototype
18

Question 18

What does this constructor do?

javascript
function Person(name) {
  this.name = name
}
Person.prototype.greet = function () {
  return `Hi ${this.name}`
}
const bob = new Person('Bob')
console.log(bob.greet())
A
Outputs Hi Bob by sharing greet through the prototype
B
Throws because prototype is undefined
C
Creates a new greet per instance
D
Sets name on the prototype
19

Question 19

Why add methods to Constructor.prototype instead of inside the constructor?

A
Methods on the prototype are shared by all instances, using less memory
B
Constructors cannot define methods
C
Instance methods are slower
D
Prototype methods cannot access this
20

Question 20

What does redefining Constructor.prototype = {} do after instances exist?

javascript
function Widget() {}
const a = new Widget()
Widget.prototype = { kind: 'new' }
const b = new Widget()
console.log('a', a.kind, 'b', b.kind)
A
a lacks kind (undefined); b inherits kind = new
B
Both inherit new
C
Both undefined
D
Throws error
21

Question 21

What does FunctionName.prototype.constructor typically reference?

A
The function itself
B
Object
C
null
D
The parent prototype
22

Question 22

How do you restore constructor after overriding the prototype object?

A
Set NewProto.constructor = FunctionName after assignment
B
It fixes itself automatically
C
Use Object.fixPrototype
D
constructor is read-only
23

Question 23

What does this log?

javascript
function Gadget(type) {
  this.type = type
}
Gadget.prototype.describe = function () {
  return `Type: ${this.type}`
}
const phone = new Gadget('phone')
console.log(phone.describe === Gadget.prototype.describe)
A
true because instances share the same function reference
B
false because methods are copied
C
Throws TypeError
D
undefined
24

Question 24

What is the benefit of Object.getOwnPropertyNames(Constructor.prototype)?

A
Lists all prototype methods, even non-enumerable, aiding introspection
B
Lists only enumerable methods
C
Returns instance properties
D
Returns constructors
25

Question 25

What does overriding a prototype method inside an instance do?

A
Defines an own property shadowing the prototype method
B
Modifies the prototype for all instances
C
Throws
D
Removes the method
26

Question 26

Which statement about this in prototype methods is correct?

A
this refers to the instance invoking the method (unless bound otherwise)
B
this is always the prototype
C
this is window by default even in strict mode
D
this cannot be changed
27

Question 27

Why should instance properties be defined inside the constructor?

A
So each instance has its own values instead of sharing state
B
Because prototypes cannot hold data
C
Because constructors run only once
D
Prototypes are read-only
28

Question 28

What does this code demonstrate?

javascript
function Counter() {
  this.count = 0
}
Counter.prototype.increment = function () {
  this.count++
}
const c1 = new Counter()
const c2 = new Counter()
c1.increment()
console.log(c1.count, c2.count)
A
c1 has 1, c2 has 0 because count is per instance
B
Both share count
C
increment modifies prototype count
D
Throws
29

Question 29

What is the effect of deleting an own property?

A
Prototype lookup can expose inherited properties with the same name again
B
Prototype property also deletes
C
Prototype chain breaks
D
delete is disallowed
30

Question 30

How do you detect whether a property exists anywhere on an object including prototypes?

A
'prop' in obj
B
obj.hasOwnProperty(prop)
C
Object.hasOwn(obj, prop)
D
obj.prototype[prop]
31

Question 31

Which method enumerates both own and inherited but enumerable properties?

A
for...in loop
B
Object.keys
C
Object.getOwnPropertyNames
D
Reflect.ownKeys
32

Question 32

What does this snippet show about method overriding?

javascript
function Hero() {}
Hero.prototype.attack = function () {
  return 'basic hit'
}
const player = new Hero()
player.attack = function () {
  return 'special hit'
}
console.log(player.attack(), Hero.prototype.attack())
A
"special hit basic hit" demonstrating own method overriding prototype
B
basic hit basic hit
C
Throws due to overriding
D
special hit special hit
33

Question 33

How does overriding affect other instances?

A
Only the overridden instance changes; others still use prototype method
B
All instances change
C
Prototype is removed
D
Constructors rerun
34

Question 34

What does Object.hasOwn(obj, prop) return?

A
True only if obj has an own property prop
B
True for prototype properties
C
Always true
D
Throws
35

Question 35

How do prototypes impact memory usage?

A
Sharing methods on prototypes avoids duplicating functions per instance
B
They increase memory because of recursion
C
They store copies of methods per object
D
They are ignored by engines
36

Question 36

What does this inheritance pattern do?

javascript
function Animal(name) {
  this.name = name
}
Animal.prototype.speak = function () {
  return `${this.name} makes noise`
}
function Dog(name) {
  Animal.call(this, name)
}
Dog.prototype = Object.create(Animal.prototype)
Dog.prototype.constructor = Dog
Dog.prototype.speak = function () {
  return `${this.name} barks`
}
const rex = new Dog('Rex')
console.log(rex.speak(), rex instanceof Dog, rex instanceof Animal)
A
Outputs "Rex barks true true" establishing classical prototypal inheritance
B
Throws due to Object.create
C
Animal.call fails
D
Dog instanceof Animal is false
37

Question 37

Why call Animal.call(this, name) inside Dog constructor?

A
To initialize instance fields defined in the parent constructor within the child instance
B
To inherit prototype methods
C
To modify Animal.prototype
D
It is optional for inheritance
38

Question 38

What happens if you omit Dog.prototype.constructor = Dog?

javascript
function Base() {}
function Sub() {}
Sub.prototype = Object.create(Base.prototype)
const s = new Sub()
console.log(s.constructor === Sub)
A
false because constructor still points to Base
B
true automatically
C
Throws
D
constructor becomes null
39

Question 39

What is Object.create primarily used for?

A
Creating objects with a given prototype without running constructors
B
Cloning objects deeply
C
Converting arrays to objects
D
Binding methods
40

Question 40

What does this example demonstrate?

javascript
const baseConfig = { cache: true }
const envConfig = Object.create(baseConfig)
envConfig.api = '/v1'
console.log(envConfig.cache, envConfig.hasOwnProperty('cache'))
A
true false — inherited properties appear accessible but not own
B
true true
C
undefined false
D
Throws
41

Question 41

Which method copies all enumerable properties from sources to target but does not preserve prototype?

A
Object.assign
B
Object.create
C
Object.setPrototypeOf
D
Reflect.construct
42

Question 42

How does Object.setPrototypeOf affect an object?

javascript
const target = { name: 'widget' }
const proto = { type: 'gadget' }
Object.setPrototypeOf(target, proto)
console.log(target.type)
A
It reassigns target’s prototype so type resolves to gadget
B
It copies properties
C
It throws in strict mode
D
It deletes own props
43

Question 43

Why is Object.setPrototypeOf discouraged in hotspots?

A
Changing prototypes after object creation slows down property access optimizations
B
It is deprecated
C
It clones objects
D
It only works in Node
44

Question 44

What does this class syntax compile down to conceptually?

javascript
class Shape {
  area() { return 0 }
}
class Square extends Shape {
  constructor(size) {
    super()
    this.size = size
  }
  area() { return this.size ** 2 }
}
const sq = new Square(3)
console.log(sq.area(), sq instanceof Shape)
A
Prototype inheritance using extends/super sugar; outputs 9 true
B
Class-based inheritance like other languages
C
Throws because super not allowed
D
sq instanceof Shape is false
45

Question 45

What is the role of super in class constructors?

A
It invokes the parent constructor, setting up this before accessing it
B
It copies parent methods
C
It sets prototype to null
D
Optional unless extends
46

Question 46

What does this class override demonstrate?

javascript
class Parent {
  speak() { return 'parent' }
}
class Child extends Parent {
  speak() {
    return super.speak() + ' child'
  }
}
const c = new Child()
console.log(c.speak())
A
parent child — child method calls super then extends result
B
child — super ignored
C
Throws because super unusable
D
parent — child cannot override
47

Question 47

How are class fields defined compared to prototype methods?

A
Fields declared directly in class body become instance properties initialized per construction
B
They are prototype-only
C
They require prototypes manually
D
They are static by default
48

Question 48

What does static keyword do in class definitions?

A
Adds methods/properties on the constructor function itself instead of the prototype
B
Creates private fields
C
Prevents inheritance
D
Runs once per instance
49

Question 49

What does this static method example log?

javascript
class MathUtil {
  static square(n) {
    return n * n
  }
}
console.log(MathUtil.square(4))
A
16
B
undefined
C
Throws because instances required
D
NaN
50

Question 50

What does Object.getPrototypeOf(ClassName) return for ES classes?

A
The constructor’s prototype (Function.prototype for base classes)
B
Class body object
C
null always
D
Instance prototype
51

Question 51

How do you create inheritance without constructors using Object.create?

A
const child = Object.create(parent); child.init(...)
B
child = parent.clone()
C
parent.inherit(child)
D
Use new parent() always
52

Question 52

What does this factory pattern show?

javascript
const base = {
  describe() {
    return `${this.type} ${this.name}`
  }
}
function createPlayer(name) {
  const player = Object.create(base)
  player.type = 'player'
  player.name = name
  return player
}
const alice = createPlayer('Alice')
console.log(alice.describe())
A
player Alice — factory returns objects inheriting shared behavior from base
B
undefined
C
Throws
D
describe is undefined
53

Question 53

What is the advantage of using Object.create(null) for dictionaries?

A
No inherited properties (like toString) that could collide with keys
B
It improves performance automatically
C
It adds built-in hashing
D
It prevents JSON serialization
54

Question 54

How does class extends handle the prototype chain?

javascript
class A {}
class B extends A {}
const b = new B()
console.log(Object.getPrototypeOf(B) === A, Object.getPrototypeOf(B.prototype) === A.prototype)
A
true true — constructors and their prototypes both chain appropriately
B
true false
C
false true
D
false false
55

Question 55

Why prefer class syntax over manual constructor patterns in modern code?

A
Classes provide clearer semantics, built-in super handling, and consistent syntax for inheritance while still using prototypes internally
B
Classes are faster engine features
C
Constructors are deprecated
D
Classes copy all methods per instance
56

Question 56

How does this behave inside prototype methods when detached?

A
If a method is stored and invoked without binding, this becomes undefined in strict mode (or window otherwise)
B
It always stays bound to the instance
C
It points to the prototype
D
It points to the constructor
57

Question 57

Why can storing functions on prototypes that depend on this be problematic in event handlers?

A
Event systems may call them with different this, requiring bind or arrow wrappers
B
They cannot access DOM
C
They become static
D
Prototypes cannot refer to DOM elements
58

Question 58

What does this binding example log?

javascript
function Timer() {
  this.seconds = 0
}
Timer.prototype.tick = function () {
  this.seconds++
}
const t = new Timer()
const tick = t.tick
try {
  tick()
} catch (e) {
  console.log('error')
}
console.log(t.seconds)
A
0 — tick loses this when called standalone, throwing in strict mode or incrementing window.seconds otherwise
B
1 always
C
Throws and stops execution
D
undefined
59

Question 59

How do you permanently bind a prototype method to an instance?

A
this.method = this.method.bind(this) inside the constructor
B
Define method as arrow on prototype
C
Use Object.bindMethod
D
Set prototype to arrow function
60

Question 60

What pitfall arises when assigning mutable objects to prototypes?

A
All instances share the same mutable state, causing unintended cross-instance mutations
B
They become immutable
C
Prototype is removed
D
Performance improves
61

Question 61

What does this shared array example show?

javascript
function Bag() {}
Bag.prototype.items = []
Bag.prototype.add = function (x) {
  this.items.push(x)
}
const b1 = new Bag()
const b2 = new Bag()
b1.add('apple')
console.log(b1.items, b2.items)
A
['apple'] ['apple'] — both instances share the same items array on the prototype
B
['apple'] []
C
[] []
D
Throws
62

Question 62

How do you avoid the Bag shared array issue?

A
Initialize this.items = [] inside the constructor so each instance gets its own array
B
Use Object.freeze
C
Keep prototype array and clone manually each time
D
inherit from Array directly
63

Question 63

What does this instanceof check show?

javascript
function Animal() {}
function Plant() {}
const a = new Animal()
console.log(a instanceof Animal, a instanceof Plant)
A
true false — instanceof examines prototype chain
B
true true
C
false false
D
Throws
64

Question 64

Why can dynamic prototype reassignment break instanceof?

A
Existing instances retain old prototypes, so instanceof comparisons with new ones fail
B
instanceof relies on constructors only
C
instanceof ignores prototypes
D
instanceof is deprecated
65

Question 65

What is a performance consideration for deep prototype chains?

A
Deep chains increase lookup time because engines traverse more objects
B
They are faster than shallow ones
C
They store more memory automatically
D
They eliminate caches
66

Question 66

Suppose root has level 0, mid inherits from root and overrides level to 1, and leaf inherits from mid without redefining level. What levels are observed from leaf, its prototype, and its prototype’s prototype?

A
leaf level is 1 (from mid), mid level 1, root level 0
B
leaf 0, mid 1, root 0
C
All undefined
D
Throws due to conflicting levels
67

Question 67

Why should you avoid using __proto__ directly?

A
It is legacy, slower, and not recommended in modern code compared to Object.getPrototypeOf/ setPrototypeOf
B
It cannot read prototypes
C
It is removed from browsers
D
It copies properties
68

Question 68

What is a pitfall of mixing class syntax with manual prototype mutations?

A
Classes expect stable prototypes; manual changes can break super calls or field initializers
B
Classes disallow prototypes
C
Manual mutations override class keywords
D
No issues
69

Question 69

Which best practice ensures clarity in prototypal design?

A
Separate shared behavior (prototype) from instance state, document the inheritance chain, and avoid unexpected mutation of prototypes
B
Modify prototypes inside loops
C
Use Object.setPrototypeOf for every method call
D
Store data on prototypes by default
70

Question 70

What does this best-practice example show?

javascript
class Queue {
  constructor() {
    this.items = []
  }
  enqueue(value) {
    this.items.push(value)
  }
  dequeue() {
    return this.items.shift()
  }
}
const q1 = new Queue()
const q2 = new Queue()
q1.enqueue('a')
console.log(q1.items, q2.items)
A
['a'] [] — instance state (items) lives on each object, methods on prototype
B
['a'] ['a']
C
[] []
D
Throws due to class
71

Question 71

How can you inspect the entire prototype chain of an object?

A
Iteratively call Object.getPrototypeOf until null, collecting references
B
console.log(obj.prototypeChain)
C
Use JSON.stringify
D
instanceof reveals chain
72

Question 72

What does this detection snippet print?

javascript
function logChain(obj) {
  const chain = []
  let current = obj
  while (current) {
    chain.push(current.constructor && current.constructor.name)
    current = Object.getPrototypeOf(current)
  }
  console.log(chain)
}
logChain(new Date())
A
An array of constructor names like ["Date", "Object", undefined]
B
Throws
C
Only Date
D
null
73

Question 73

What is a common pitfall when mixing Array inheritance with prototypes?

A
Extending Array via prototypes can break length tracking unless using Array subclassing (class extends Array)
B
Arrays cannot be inherited
C
Prototype methods never work on arrays
D
Arrays lack prototypes
74

Question 74

What does this Array subclass output?

javascript
class Stack extends Array {
  top() {
    return this[this.length - 1]
  }
}
const stack = new Stack()
stack.push(1, 2)
console.log(stack.top(), stack instanceof Stack, stack instanceof Array)
A
2 true true — extends Array preserves array behavior with custom methods
B
undefined false true
C
Throws due to extends Array
D
2 true false
75

Question 75

Which statement summarizes best practices for prototypal design?

A
Keep prototypes stable, share methods not state, document inheritance, and prefer class/Object.create syntax that clearly communicates intent
B
Mutate prototypes per instance
C
Use __proto__ for all operations
D
Store data on prototypes by default

QUIZZES IN JavaScript