JavaScript Questions & Answers – Classes in JavaScript

This set of Javascript Multiple Choice Questions & Answers (MCQs) focuses 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: c
Explanation: ‘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?

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

a) false
b) true
c) 0
d) 1
View Answer

Answer: b
Explanation: 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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: a
Explanation: 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.
advertisement

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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: a
Explanation: 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.
advertisement

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);
View Answer

Answer: c
Explanation: 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;});
View Answer

Answer: a
Explanation: 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()
View Answer

Answer: c
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: c
Explanation: 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.

Sanfoundry Global Education & Learning Series – Javascript Programming.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.