This quiz covers key concepts of OOP in JavaScript to help you master its principles.
Question 1
What is a class in JavaScript?
A built-in object for working with arrays
A blueprint for creating objects
A method for inheritance
A library for JavaScript functions
Question 2
What is the purpose of the constructor method in a JavaScript class?
Initializes the class
Defines private methods
Creates a new instance of the class
Sets up properties for the class object
Question 3
How do you create an instance of a class in JavaScript?
class Person {
constructor(name) {
this.name = name;
}
}
const p = new Person('Ajay');
const p = Person('Ajay');
const p = createInstance(Person, 'Ajay');
const p = Object.create(Person);
Question 4
What does the extends keyword do in JavaScript?
Allows one class to inherit from another class
Creates a new instance of a class
Binds a method to a class
Defines static methods in a class
Question 5
What is encapsulation in JavaScript OOP?
Using a constructor to initialize properties
Inheriting properties from a parent class
Combining multiple classes into one
Restricting direct access to object properties and methods
Question 6
What will the following code log?
class Animal {
makeSound() {
console.log("Generic sound");
}
}
class Dog extends Animal {
makeSound() {
console.log("Bark");
}
}
const pet = new Dog();
pet.makeSound();
Generic sound
Bark
Error
Undefined
Question 7
What is polymorphism in OOP?
A way to define private properties
Combining classes into a single object
Using the same method name but with different implementations in child classes
Sharing methods between unrelated objects
Question 8
What are static methods in JavaScript classes?
Methods only accessible to instances of the class
Methods accessible only on the class itself
Methods that are inherited by subclasses
Methods that override the constructor
Question 9
What will the following code log?
class Vehicle {
static start() {
console.log("Starting vehicle...");
}
}
Vehicle.start();
Starting vehicle...
Error
Undefined
Starting
Question 10
Which of the following best describes inheritance in JavaScript?
Creating a new class from a string
Sharing properties and methods between objects of the same type
Deriving a new class from an existing class to reuse code
Encapsulating object properties
There are 10 questions to complete.