JavaScript MCQs – Object-Oriented Programming

31.) What is the output of the following code?

class Vehicle {  
  constructor() {  
    this.type = "Car";  
  }  
}  
class Bike extends Vehicle {  
  constructor() {  
    super();  
    this.type = "Bike";  
  }  
}  
const bike = new Bike();  
console.log(bike.type);
A) Bike
B) Car
C) Error
D) undefined

Answer: Option A

Explanation: The super() initializes the parent class, but the type property is then overwritten in the Bike constructor.

32.) What is the output of this code?

function Shape() {  
  this.type = "shape";  
}  
Shape.prototype.getType = function () {  
  return this.type;  
};  
const circle = new Shape();  
circle.type = "circle";  
console.log(circle.getType());
A) shape
B) circle
C) Error
D) undefined

Answer: Option B

Explanation: The type property is overridden directly on the circle object, so getType() returns “circle”.

33.) What does the following code output?

class Parent {  
  constructor() {  
    this.name = "Parent";  
  }  
}  
class Child extends Parent {  
  constructor() {  
    super();  
    delete this.name;  
  }  
}  
const child = new Child();  
console.log(child.name);
A) undefined
B) Parent
C) Error
D) null

Answer: Option A

Explanation: The delete keyword removes the name property from the child instance, leaving it undefined.

34.) What is the output of the following code?

class Animal {  
  constructor() {  
    this.legs = 4;  
  }  
}  
Animal.prototype.sound = "roar";  
const cat = new Animal();  
cat.sound = "meow";  
delete cat.sound;  
console.log(cat.sound);
A) roar
B) meow
C) Error
D) undefined

Answer: Option A

Explanation: Deleting the sound property on the cat object restores access to the inherited sound property from the prototype, which is “roar”.

35.) What is the output of this code?

class Gadget {  
  constructor() {  
    this.type = "gadget";  
  }  
}  
Gadget.prototype.showType = function () {  
  return this.type;  
};  
const phone = new Gadget();  
Gadget.prototype.type = "generic gadget";  
console.log(phone.showType());
A) Error
B) generic gadget
C) gadget
D) undefined

Answer: Option C

Explanation: The type property on the phone instance shadows the prototype property, so “gadget” is returned.

Leave a Reply

Your email address will not be published. Required fields are marked *