JavaScript MCQs – Object-Oriented Programming

JavaScript MCQs – Object-oriented programming is essential for mastering advanced programming concepts. Topics like classes, inheritance, encapsulation, polymorphism, and prototypes are fundamental in creating reusable, maintainable code. Understanding this, constructors, method overriding, and static methods is vital for interviews. Practicing MCQs helps in reinforcing concepts and prepares you to handle scenario-based questions confidently.

1.) What is an object in JavaScript?

A) A collection of key-value pairs
B) A special type of function
C) A built-in data type
D) None of the above

Answer: Option A

Explanation: An object in JavaScript is a collection of properties (key-value pairs) that represent data and methods.

2.) How do you create an object using the constructor function?

A) let obj = {};
B) let obj = []
C) let obj = Object.create();
D) let obj = new Object();

Answer: Option D

Explanation: The new Object() syntax creates an object using the built-in Object constructor.

3.) What does the this keyword represent inside an object method?

A) The global object
B) The object that owns the method
C) The parent object
D) Undefined

Answer: Option B

Explanation: In an object method, this refers to the object that owns the method being called.

4.) What is a class in JavaScript?

A) A template for creating objects
B) A function that returns an object
C) A built-in data type
D) An array-like structure

Answer: Option A

Explanation: A class in JavaScript serves as a blueprint for creating objects with shared properties and methods.

5.) Which keyword is used to define a class in JavaScript?

A) define
B) new
C) class
D) object

Answer: Option C

Explanation: The class keyword is used to define a class in JavaScript.

6.) What is the purpose of Object.getPrototypeOf()?

A) To create a prototype for an object
B) To retrieve the prototype of an object
C) To modify an object’s prototype
D) To seal an object

Answer: Option B

Explanation: Object.getPrototypeOf() retrieves the prototype of a given object.

7.) What is the purpose of a constructor in a class?

A) To define methods
B) To destroy an object
C) To inherit another class
D) To initialize properties when an object is created

Answer: Option D

Explanation: A constructor is a special method in a class used for initializing object properties during object creation.

8.) What is inheritance in JavaScript?

A) Copying one object to another
B) Using multiple objects together
C) Defining methods in an object
D) A class receiving properties and methods from another class

Answer: Option D

Explanation: Inheritance allows a class to use properties and methods of another class using the extends keyword.

9.) How do you call the parent class constructor in a subclass?

A) super();
B) parent();
C) constructor();
D) base();

Answer: Option A

Explanation: The super() keyword is used to call the constructor of the parent class in a subclass.

10.) What is the prototype in JavaScript?

A) A method for copying objects
B) A mechanism to inherit properties and methods
C) A way to destroy objects
D) A keyword for creating objects

Answer: Option B

Explanation: The prototype is an object from which other objects inherit properties and methods.

Leave a Reply

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