JavaScript Questions & Answers – Augmentation of Classes

This set of Javascript Multiple Choice Questions & Answers (MCQs) focuses on “Augmentation of Classes”.

1. The four kinds of class members are ________
a) Instance methods, Instance fields, Static method, Dynamic method
b) Instance fields, Instance methods, Class fields, Class methods
c) Instance fields, Non-instance fields, Dynamic methods, Global methods
d) Global methods, Local methods, Dynamic methods, Static methods
View Answer

Answer: b
Explanation: A class mainly contains data members and associated member functions. Therefore over all different kinds of class members are instance field, instance method, class fields and class methods.

2. Different kinds of the object involved in a class definition are ________
a) Public object, Private object, Protected object
b) Constructor object, Function object, Destructor object
c) Constructor object, Prototype object, Instance object
d) Instance method, Static object, Dynamic object
View Answer

Answer: c
Explanation: In JavaScript, there are three different objects involved in any class definition, and the properties of these three objects act like different kinds of class members namely, Constructor object, Prototype object, and Instance object.

3. The object whose properties are inherited by all instances of the class, and properties whose values are functions behaving like instance methods of the class, is ________
a) Instance object
b) Constructor object
c) Destructor object
d) Prototype object
View Answer

Answer: d
Explanation: The properties of the prototype object are inherited by all instances of the class, and properties whose values are functions behave like instance methods of the class.
advertisement
advertisement

4. Which variables are used internally in object methods and are also globally visible?
a) Object properties
b) Variable properties
c) Method properties
d) Internal properties
View Answer

Answer: b
Explanation: The variable properties are usually variables that are used internally in the object’s methods, but can also be globally visible variables that are used through the page.

5. The class that represents the regular expressions is ________
a) RegExpObj
b) RegExpClass
c) RegExp
d) StringExp
View Answer

Answer: c
Explanation: Regular expression is an object that describes a pattern of characters. The JavaScript RegExp class represents regular expressions, and both string and RegExp define methods that use regular expressions.

6. The different variant of Date() constructor to create date object is/are ___________
i. new Date(date)
ii. new Date(milliseconds)
iii. new Date(date string)
iv. new Date(year, month, date[hour, minute, second, millisecond])
a) i, ii and iii only
b) ii, iii and iv only
c) i, ii and iv only
d) i, ii, iii and iv
View Answer

Answer: b
Explanation: Date() is a predefined object type in javascript. There are 4 ways to create a new date object:new Date(), new Date(year, month, day, hours, minutes, seconds, milliseconds), new Date(milliseconds), new Date(date string).

7. Which is the correct code that returns a complex number that is the complex conjugate of this one?
a) Complex.prototype.conj = function() { return new Complex(this.r, -this.i); };
b) Complex.prototype.conj = function() { return Complex(this.r, -this.i); };
c) Complex.prototype.conj = function() { return (this.r, -this.i); };
d) Complex.prototype.conj = function() { new Complex(this.r, -this.i); };
View Answer

Answer: a
Explanation: Object.prototype.constructor specifies the function that creates the object prototype. The above code snippet returns a complex number that is the complex conjugate of this one.
advertisement

8. How can we make methods available on all objects?
a) Object.add(methods)
b) Object.methods(add)
c) Object.add.methods(…)
d) Object.prototype
View Answer

Answer: d
Explanation: It is possible to add methods to Object.prototype, making them available on all objects. This is not recommended, however, because prior to ECMAScript5, there is no way to make these add-on methods non enumerable, and if you add properties to Object.prototype, those properties will be reported by all for/in loops.

9. What is the procedure to add methods to HTMLElement so that they will be inherited by the objects that represent the HTML tags in the current document?
a) HTMLElement.prototype(…)
b) HTMLElement.prototype
c) HTML.addmethods()
d) HTML.elements(add)
View Answer

Answer: b
Explanation: It is implementation-dependent whether classes defined by the host environment (such as the web browser) can be augmented using Object.prototype. In many web browsers, for example, you can add methods to HTMLElement.prototype and those methods will be inherited by the objects that represent the HTML tags in the current document.
advertisement

10. You can refresh the webpage in JavaScript by using ________
a) window.reload
b) location.reload
c) window.refresh
d) page.refresh
View Answer

Answer: b
Explanation: The reload method is used to reload the current file. User can reload the page from the server through location. Reload.

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

   emp={id:102,name:"Shyam Kumar",salary:40000}  
   document.write(emp.id+" "+emp.name+" "+emp.salary);

a) 102 4000 Shyam Kumar
b) 102 4000
c) 102 Shyam Kumar 40000
d) 102 shyam kumar 40000
View Answer

Answer: c
Explanation: In this method, the object is created using literals. Property and Value are separated by colon.

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

function emp(id,name)
{  
    this.id=id;  
    this.name=name;    
}  
e=new emp(103,"Vimal Jaiswal");  
 
document.write(e.id+" "+e.name");

a) 103 vimal jaiswal
b) 103
c) 103 Vimal Jaiswal
d) Vimal jaiswal
View Answer

Answer: c
Explanation: Object in the above code is created by passing arguments to the function. “this” keyword is used for defining the values.

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

var emp=new Object();  
emp.name="Ravi Malik";  
emp.salary=50000;  
document.write("emp.name+" "+emp.salary);

a) Ravi malik 5000
b) Ravi Malik 50000
c) Ravi malik
d) 50000
View Answer

Answer: b
Explanation: In the above code an object of class emp is made using the new operator. The above code prints the value of name and salary as defined in the object of the class.

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

function emp(name,salary)
{  
     this.name=name;  
     this.salary=salary;  
 
     this.changeSalary=changeSalary;  
     function changeSalary(otherSalary)
     {  
         this.salary=otherSalary;  
     }  
}  
e=new emp("Rahul",30000);  
e.changeSalary(45000);  
document.write("e.name+" "+e.salary);

a) Rahul 30000
b) Rahul
c) Rahul 45000
d) 45000
View Answer

Answer: c
Explanation: The above code defines an additional method in JavaScript object. But before defining method, the property should be added in the function with the same name as method.

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

const obj = { 10: 'arry', 21: 'barry', 23: 'carry' };  
console.log(Object.entries(obj)[2]);

a) [“arry”, “10”]
b) [“10”,”arry”]
c) [“21”,barry”]
d) [“23”,”carry”]
View Answer

Answer: d
Explanation: Object.entries() method is used to return an array of a given object’s own [key, value] pairs. The ordering of the properties is the same as that given by looping over the property values of the object manually.

Sanfoundry Global Education & Learning Series – Javascript Programming.

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.