JavaScript MCQs – Object-Oriented Programming

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

class Animal {  
  constructor(name) {  
    this.name = name;  
  }  
  speak() {  
    return `${this.name} makes a noise.`;  
  }  
}  
class Dog extends Animal {  
  speak() {  
    return `${this.name} barks.`;  
  }  
}  
const dog = new Dog("Rex");  
console.log(dog.speak());
A) Rex makes a noise.
B) Error
C) Rex barks.
D) undefined

Answer: Option C

Explanation: The Dog class overrides the speak() method of the Animal class, so dog.speak() outputs “Rex barks.”.

22.) What is the result of this code?

class Alpha {  
  constructor() {  
    this.alpha = "A";  
  }  
}  
class Beta extends Alpha {  
  constructor() {  
    super();  
    this.beta = "B";  
  }  
}  
const obj = new Beta();  
console.log(obj.alpha, obj.beta);
A) undefined undefined
B) A B
C) Error
D) null null

Answer: Option B

Explanation: The Beta class inherits from Alpha, and the super() call in the Beta constructor initializes the alpha property. Both properties are accessible.

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

class Example {  
  static greet() { return "Hello!"; }  
}  
console.log(Example.greet());
A) undefined
B) null
C) Error
D) Hello!

Answer: Option D

Explanation: The static method greet() can be called directly on the class without creating an instance, returning “Hello!”.

24.) What is the result of the following code?

class Parent {  
  constructor() {  
    this.value = 10;  
  }  
  getValue() {  
    return this.value;  
  }  
}  
class Child extends Parent {  
  constructor() {  
    super();  
    this.value = 20;  
  }  
}  
const child = new Child();  
console.log(child.getValue());
A) 10
B) 20
C) Error
D) undefined

Answer: Option B

Explanation: The super() call in the Child constructor initializes the parent class, and the value is overwritten in the Child class, so child.getValue() returns 20.

25.) What is the output of this code?

class Example {  
  constructor() {  
    this.data = 42;  
  }  
}  
Example.prototype.data = 100;  
const ex = new Example();  
delete ex.data;  
console.log(ex.data);
A) 100
B) 42
C) undefined
D) Error

Answer: Option A

Explanation: Deleting the data property from ex removes the instance property, exposing the inherited prototype property, which is 100.

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

class Base {  
  constructor() {  
    this.value = 10;  
  }  
}  
class Derived extends Base {  
  constructor() {  
    super();  
    this.value += 5;  
  }  
}  
const obj = new Derived();  
console.log(obj.value);
A) 10
B) 15
C) Error
D) undefined

Answer: Option B

Explanation: The Derived class constructor calls super() to initialize the value property from the Base class and then increments it by 5.

27.) What does this code output?

class Tool {  
  constructor() {  
    this.name = "tool";  
  }  
  static describe() {  
    return "This is a tool.";  
  }  
}  
const hammer = new Tool();  
console.log(hammer.describe());
A) This is a tool.
B) tool
C) undefined
D) Error

Answer: Option D

Explanation: Static methods like describe() cannot be called on an instance; they can only be called on the class itself.

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

function Person(name) {  
  this.name = name;  
}  
Person.prototype.greet = function () {  
  return `Hello, ${this.name}`;  
};  
const person = new Person("Alice");  
console.log(person.greet());
A) Hello, undefined
B) Hello, Alice
C) Error
D) undefined

Answer: Option B

Explanation: The greet() method is defined on the Person.prototype, so it is available to instances like person.

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

class Counter {  
  constructor() {  
    this.count = 0;  
  }  
  increment() {  
    return ++this.count;  
  }  
}  
const counter1 = new Counter();  
const counter2 = new Counter();  
counter1.increment();  
counter2.increment();  
console.log(counter1.count, counter2.count);
A) 1, 1
B) 2, 1
C) 1, 2
D) 2, 2

Answer: Option A

Explanation: Each Counter instance has its own count property. Incrementing one instance does not affect the other.

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

class Example {  
  static logMessage() {  
    console.log("Static method called!");  
  }  
}  
const ex = new Example();  
ex.logMessage();
A) Static method called!
B) null
C) undefined
D) Error

Answer: Option D

Explanation: Static methods can only be called on the class itself, not on instances. Attempting to call logMessage() on ex results in an error.

Leave a Reply

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