JavaScript MCQs – Object-Oriented Programming

11.) What does Object.create() do?

A) Copies an object
B) Creates a deep clone of an object
C) Creates a new object with the specified prototype
D) Returns a stringified version of an object

Answer: Option C

Explanation: Object.create() creates a new object with a specified prototype object.

12.) What does encapsulation mean in JavaScript?

A) Hiding data and exposing only the necessary parts of an object
B) Combining multiple objects into one
C) Creating a copy of an object
D) Removing methods from an object

Answer: Option A

Explanation: Encapsulation is the practice of bundling data and methods together while restricting direct access to some components.

13.) What does the static keyword do in a class?

A) Defines a method accessible only on the class itself, not instances
B) Makes a property immutable
C) Creates a constant variable
D) Creates an instance of a class

Answer: Option A

Explanation: Static methods are defined on the class itself and cannot be called on class instances.

14.) How do you define a private field in a class?

A) Using _ as a prefix
B) Using the private keyword
C) Using # as a prefix
D) Using const

Answer: Option C

Explanation: Private fields in ES6+ classes are denoted using the # prefix, making them inaccessible outside the class.

15.) What is method overriding in JavaScript?

A) Defining multiple methods with the same name
B) A subclass providing its own implementation of a method inherited from a parent class
C) Changing the return type of a method
D) Calling a method multiple times

Answer: Option B

Explanation: Method overriding occurs when a subclass defines a method with the same name as one in its parent class, replacing the parent’s implementation.

16.) What is the purpose of Object.freeze()?

A) To make an object immutable
B) To create a prototype
C) To inherit properties
D) To delete an object

Answer: Option A

Explanation: Object.freeze() makes an object immutable, preventing new properties from being added or existing properties from being modified or deleted.

17.) What does the instanceof operator do in JavaScript?

A) Defines a property in a class
B) Creates an instance of a class
C) Checks if a class has been inherited
D) Checks if an object was created by a specific class

Answer: Option D

Explanation: The instanceof operator checks if an object is an instance of a specific class or its subclasses.

18.) What is polymorphism in JavaScript?

A) Having multiple methods with the same name but different implementations
B) Creating multiple instances of a class
C) Inheriting properties from multiple classes
D) None of the above

Answer: Option A

Explanation: Polymorphism allows objects to be treated as instances of their parent class, enabling methods with the same name to behave differently.

19.) How do you check if a property exists in an object?

A) obj.hasOwnProperty(property)
B) obj.property
C) property in obj
D) Both A and C

Answer: Option D

Explanation: Both hasOwnProperty() and the in operator can be used to check if a property exists in an object.

20.) What is the difference between call() and apply() methods in JavaScript?

A) call() accepts arguments as an array, apply() as separate arguments.
B) apply() accepts arguments as an array, call() as separate arguments.
C) Both are used to create new objects.
D) Both are the same in functionality.

Answer: Option B

Explanation: The apply() method accepts arguments as an array, while call() accepts them as a comma-separated list.

Leave a Reply

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