250+ TOP MCQs on Classes in JavaScript and Answers

Javascript Multiple Choice Questions on “Classes in JavaScript”.

1. The behaviour of the instances present of a class inside a method is defined by __________
a) Method
b) Classes
c) Interfaces
d) Classes and Interfaces
Answer: b
Clarification: Objects of the class are also known as instances of the class. The behaviour of the instance of a class is defined by the class and is shared by all instances.

2. The keyword or the property that you use to refer to an object through which they were invoked is _________
a) from
b) to
c) this
d) object
Answer: c
Clarification: ‘this’ keyword is used to refer to the object through which the properties or methods were invoked. This use of ‘this’ is a fundamental characteristic of the methods of any class.

3. What will be the output of the following JavaScript code?

var o = new F();
o.constructor === F

a) false
b) true
c) 0
d) 1
Answer: b
Clarification: Constructor is a function property of the class which is used to create objects of that class. In the above code, both statements create an instance of the class.

4. The basic difference between JavaScript and Java is _________
a) There is no difference
b) Functions are considered as fields
c) Variables are specific
d) Functions are values, and there is no hard distinction between methods and fields
Answer: d
Clarification: Java is an OOP programming language while JavaScript is an OOP scripting language. The basic difference between JavaScript and Java is that the functions are values, and there is no hard distinction between methods and fields.

5. The meaning for Augmenting classes is that ___________
a) objects inherit prototype properties even in a dynamic state
b) objects inherit prototype properties only in a dynamic state
c) objects inherit prototype properties in the static state
d) object doesn’t inherit prototype properties in the static state
Answer: a
Clarification: JavaScript’s prototype-based inheritance mechanism is dynamic an object inherits properties from its prototype, even if the prototype changes after the object is created. This means that we can augment JavaScript classes simply by adding new methods to their prototype objects.

6. The property of JSON() method is __________
a) it can be invoked manually as object.JSON()
b) it will be automatically invoked by the compiler
c) it is invoked automatically by the JSON.stringify() method
d) it cannot be invoked in any form
Answer: c
Clarification: A common use of JSON is to exchange data to/from a web server. When sending data to a web server, the data has to be a string. In that case json.strigify() is used to convert a javascript object into a string.

7. When a class B can extend another class A, we say that?
a) A is the superclass and B is the subclass
b) B is the superclass and A is the subclass
c) Both A and B are the superclass
d) Both A and B are the subclass
Answer: a
Clarification: Superclass is the class from which subclasses are defined. Subclasses are also called extensions of superclass.therefore in the above scenario A will be superclass and B will be subclass.

8. If A is the superclass and B is the subclass, then subclass inheriting the superclass can be represented as _________
a) B=inherit(A);
b) B=A.inherit();
c) B.prototype=inherit(A);
d) B.prototype=inherit(A.prototype);
Answer: c
Clarification: inherit() function is a predefined function in javascript which is used to inherit properties of another class. The subclass B inherits the prototype of the class A.

9. The snippet that filters the filtered set is __________
a) var t=new FilteredSet(s, {function(s) {return !(x instanceof Set);});
b) var t=new FilteredSet{function(s) {return !(x instanceof Set);});
c) var t=new FilteredSet(s, {function(s) {return (x instanceof Set);});
d) var t=new FilteredSet(s, {function(s) {return x;});
Answer: a
Clarification: The instanceof operator is used to check the type of an object at run time. The instanceof operator returns a boolean value that indicates if an object is an instance of a particular class.

10. The method that can be used to create new properties and also to modify the attributes of existing properties is _________
a) Object.defineProperty()
b) Object.defineProperties()
c) Both Object.defineProperty() and Object.defineProperties()
d) Object.inherit()
Answer: c
Clarification: The method Object.defineProperty() defines a new property directly on an object, or modifies an existing property on an object, and returns the object.Both Object.defineProperty() and Object.defineProperties() can be used todefine new properties.

11. What will be the output of the following JavaScript code?

const obj1 = 
{  
  	a: 10,  
  	b: 15,  
  	c: 18  
};  
const obj2 = Object.assign({c: 7, d: 1}, obj1);  
console.log(obj2.c, obj2.d);

a) 7,1
b) 18,1
c) Undefined
d) Error
Answer: a
Clarification: The Object.assign() method is used to copy the properties and values of one object to another. Objects are assigned and copied by reference.

12. What will be the output of the following JavaScript code?

function person()
{  
        this.name = 'rahul';  
}  
function obj() 
{  
       obj.call(this)  
}  
obj.prototype = Object.create(person.prototype);  
const app = new obj();  
console.log(app.name);

a) undefined
b) runtime error
c) compilation error
d) rahul
Answer: d
Clarification: Object.create() method is used to create a new object with the specified properties. This is another way of creating a new object using specified object type. The particular function accepts two values as an argument.

13. What will be the output of the following JavaScript code?

const object1 = {};  
Object.defineProperties(object1,
{  
  	property1: 
        {  
             value: 10 
        }  
 });  
console.log(object1.property1);

a) 0
b) 10
c) undefined
d) error
Answer: b
Clarification: Object.defineProperties() is a predefined function in the object library of javascript. The Object.defineProperties() method defines new or modifies existing properties directly on an object and returns the object.

14. What will be the output of the following JavaScript code?

const prototype1 = {};  
const object1 = Object.create(prototype1);  
console.log(Object.getPrototypeOf(object1) === prototype1);

a) true
b) false
c) error
d) 0
Answer: a
Clarification: The Object.getPrototypeOf() method of JavaScript returns the prototype value of the specified object. There are no inherited properties in this object.

15. What will be the output of the following JavaScript code?

const obj1 = {  
  property1: 2  
};  
Object.seal(object1);    
obj1.property1 =4;  
console.log(obj1.property1);  
delete obj1.property1;

a) 2
b) 4
c) Error
d) Undefined
Answer: c
Clarification: The seal property does not allow the object to be deleted. The object to be sealed is passed as an argument, and the method returns the object which has been sealed.

Leave a Reply

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