JavaScript Classes MCQ

1. What is a class in JavaScript?

a) A blueprint for creating objects
b) A special type of function
c) A method used for object inheritance
d) A constant variable type

Answer:

a) A blueprint for creating objects

Explanation:

In JavaScript, a class is a blueprint for creating objects with pre-defined properties and methods.

2. How do you define a class in JavaScript?

a) class MyClass {…}
b) function MyClass() {…}
c) MyClass = {…}
d) var MyClass = class {…}

Answer:

a) class MyClass {…}

Explanation:

A class in JavaScript is defined using the class keyword followed by the class name and a set of curly braces containing its body.

3. What is a constructor in a JavaScript class?

a) A method for cloning objects
b) A special method for initializing new objects
c) A function that returns the class instance
d) The first method in a class

Answer:

b) A special method for initializing new objects

Explanation:

The constructor is a special method within a class that is called when a new instance of the class is created, used for initializing properties of the object.

4. How do you create an instance of a class in JavaScript?

a) new MyClass()
b) MyClass.new()
c) MyClass()
d) create MyClass()

Answer:

a) new MyClass()

Explanation:

An instance of a class is created using the new keyword followed by the class name and parentheses.

5. What is inheritance in the context of JavaScript classes?

a) Copying properties between objects
b) A class receiving properties and methods from another class
c) Sharing methods between two instances
d) Overriding properties in a class

Answer:

b) A class receiving properties and methods from another class

Explanation:

Inheritance in JavaScript classes is a mechanism where one class (child class) can inherit properties and methods from another class (parent class).

6. How do you specify a class as a child of another class in JavaScript?

a) childClass extends parentClass
b) childClass inherits parentClass
c) childClass = parentClass
d) childClass(child of parentClass)

Answer:

a) childClass extends parentClass

Explanation:

The extends keyword is used in class declarations to create a class as a child of another class.

7. What is a method in a JavaScript class?

a) A global function
b) A function defined inside a class
c) A static property
d) A constructor parameter

Answer:

b) A function defined inside a class

Explanation:

A method in a JavaScript class is a function defined inside the class that describes the behaviors of the class's instances.

8. Can classes in JavaScript have private properties or methods?

a) Yes, using the # symbol
b) No, all properties and methods are public
c) Only properties can be private
d) Only methods can be private

Answer:

a) Yes, using the # symbol

Explanation:

JavaScript classes can have private properties and methods, which are defined using the # symbol in front of their names. These are only accessible within the class itself.

9. What is the use of the static keyword in a JavaScript class?

a) To make properties constant
b) To create a singleton class
c) To create a static (shared) method or property
d) To declare static typing

Answer:

c) To create a static (shared) method or property

Explanation:

The static keyword is used to define static methods or properties in a class. Static methods/properties are called without instantiating the class and cannot be called through a class instance.

10. How do you call a parent class's method from within a child class's method?

a) parent.method()
b) super.method()
c) this.method()
d) base.method()

Answer:

b) super.method()

Explanation:

The super keyword is used within a child class to call methods or the constructor of the parent class.

11. What is encapsulation in the context of JavaScript classes?

a) Combining related data and methods into a single class
b) Hiding private data from external access
c) Preventing classes from inheriting properties
d) Encrypting class data

Answer:

a) Combining related data and methods into a single class

Explanation:

Encapsulation in JavaScript classes refers to the bundling of data (properties) and the methods that operate on that data into a single unit or class.

12. How can you override a method in a child class in JavaScript?

a) By renaming the method in the child class
b) By calling super.method()
c) By redeclaring the method in the child class
d) By using the override keyword

Answer:

c) By redeclaring the method in the child class

Explanation:

Overriding a method in a child class is done by redeclaring the method in the child class. This new method will replace the inherited method from the parent class.

13. What is polymorphism in the context of JavaScript classes?

a) Changing the type of a class at runtime
b) A class having multiple constructors
c) Child classes having methods with the same name but different implementations
d) Creating multiple instances of a class

Answer:

c) Child classes having methods with the same name but different implementations

Explanation:

Polymorphism in object-oriented programming, including JavaScript classes, is the ability of different classes to provide different implementations of methods that are called through the same name.

14. Can you declare variables inside a class in JavaScript without a method?

a) Yes, anywhere in the class
b) No, variables must be declared within methods
c) Yes, but only if they are static
d) No, only methods are allowed inside classes

Answer:

b) No, variables must be declared within methods

Explanation:

In JavaScript classes, variables cannot be declared directly inside a class but must be declared within methods or as part of the class's properties.

15. What is the result of instantiating an abstract class in JavaScript?

a) It creates a new instance successfully
b) It throws an error
c) It creates a generic object
d) JavaScript does not support abstract classes

Answer:

d) JavaScript does not support abstract classes

Explanation:

JavaScript does not have native support for abstract classes. Attempting to instantiate a class that is intended to be abstract will not throw an error but will not enforce abstract behavior.

Leave a Comment

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

Scroll to Top