Javascript (JS) Custom Objects MCQ
This section focuses on the "Javascript Custom Objects" of the Javascript. These Multiple Choice Questions (mcq) should be practiced to improve the Javascript skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.
1. What will be the output of the below code?
class Rect {
constructor(h, w) {
this.height = h;
this.width = w;
}
get foo() {
return this.foo();
}
foo() {
return this.height * this.width;
}
}
const sq = new Rect(5, 20);
console.log(sq.foo());
A. this.height * this.width
B. 100
C. Reference Error
D. 5*20
View Answer
Ans : B
Explanation: This option is correct because, square is an object of class Rectangle and the foo function will do the operation (5*20) thus the result will be 100.
2. Choose the correct syntax in order to access property of the object created?
function Ticket(from,to,pName){
this.from=from;
this.to=to;
this.pName= pName;
}
var ticket=new Ticket( "Udaipur","Pune","Raj" );
A. ticket.from & ticket[to]
B. ticket[to] & ticket[from]
C. ticket {to} & ticket.to
D. ticket[to] & ticket{from}
View Answer
Ans : A
Explanation: we can access the property of object by using "." Operator (ObjectName.PropertyName) and by using the syntax ObjectName[PropertyName].
3. Identify the correct syntax to create a JavaScript object for Employee with property empid, empName?
function Ticket(from,to,pName){
this.from=from;
this.to=to;
this.pName= pName;
}
var ticket=new Ticket( "Udaipur","Pune","Raj" );
A. var employee=new Object(); employee.empid=20; employee.empName="John";
B. var employee ={id:2001,empName:"John"};
C. var employee=Employee(); employee.empid=20; employee.empName="John";
D. Both A and C
View Answer
Ans : A
Explanation: This option is correct because, we can create a new object by using new Object() and we can access the object by using it's name.
4. Consider the below code:
Choose the appropriate statement to be inserted at line 2, in order to create a JavaScript object named student?
var json= '{ "sId":2001,"sName":"John" }';
//line 2
console.log(student.sId+" "+student.sName);
A. var student=json.parse(json);
B. var student=JSON.stringify(json);
C. var student=JSON.parse(json);
D. var student=json.stringify(json);
View Answer
Ans : C
Explanation: This option is correct because, the variable json is of string type we have to parse it to get the property from JSON object.
5. What will be the output of the following JavaScript code?
var obj = new Fun();
obj.constructor === Fun
A. true
B. false
C. 0
D. 1
View Answer
Ans : 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.
6. If X is the superclass and Y is the subclass, then subclass inheriting the superclass can be represented as _________
A. Y=inherit(X);
B. Y=X.inherit();
C. Y.prototype=inherit(X);
D. Y.prototype=inherit(X.prototype);
View Answer
Ans : 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.
7. What will be the output of the following JavaScript code?
const ob1 =
{
a: 5,
b: 10,
c: 15
};
const ob2 = Object.assign({c: 8, d: 3}, ob1);
console.log(ob2.c, ob2.d);
A. 8,3
B. 15,3
C. undefined
D. error
View Answer
Ans : 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.
8. What will be the output of the following JavaScript code?
const pt1 = {};
const ob1 = Object.create(pt1);
console.log( Object.getPrototypeOf(ob1) === pt1 );
A. true
B. false
C. errot
D. 0
View Answer
Ans : A
Explanation: The Object.getPrototypeOf() method of JavaScript returns the prototype value of the specified object. There are no inherited properties in this object.
9. A linkage of series of prototype objects is called as :
A. prototype stack
B. prototype chain
C. prototype class
D. prototypes
View Answer
Ans : B
Explanation: Consider an example, Date.prototype inherits properties from Object.prototype, so a Date object created by new Date() inherits properties from both Date.prototype and Object.prototype. This linked series of prototype objects is known as prototype chain.
10. Which is an equivalent code to invoke a function m of class o that expects two arguments x and y?
A. o(x,y);
B. o.m(x) && o.m(y);
C. m(x,y);
D. o.m(x,y);
View Answer
Ans : D
Explanation: The code above is an invocation expression: it includes a function expression o.m and two argument expressions, x and y.
Also check :
Discussion