JavaScript Questions and Answers Part-11

1. 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
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.

2. 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
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.

3. 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
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.

4. 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
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.

5. 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
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.

6. 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
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.

7. 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
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.

8. 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
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.

9. 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
Explanation: The Object.getPrototypeOf() method of JavaScript returns the prototype value of the specified object. There are no inherited properties in this object.

10. 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
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.