JavaScript Questions and Answers Part-6

1. The unordered collection of properties, each of which has a name and a value is called _________
a) String
b) Object
c) Serialized Object
d) Array

Answer: b
Explanation: Objects in JavaScript may be defined as an unordered collection of related data, of primitive or reference types, in the form of “key:value” pairs. Hence each of the property has a name and value.

2. The object has three object attributes namely ________
a) Class, parameters, object’s extensible flag
b) Prototype, class, objects’ parameters
c) Prototype, class, object’s extensible flag
d) Native object, Classes and Interfaces and Object’s extensible flag

Answer: c
Explanation: Every object has three associated object attributes:
An object’s prototype is a reference to another object from which properties are inherited.
An object’s class is a string that categorizes the type of an object.
An object’s extensible flag specifies whether new properties may be added to the object.

3. What will be the firstname and surname of the following JavaScript code?
var book = {
"main title": "JavaScript",
'sub-title': "The Definitive Guide",
"for": "all audiences",
author: {
firstname: "David",
surname: "Flanagan"
}
};
a) properties
b) property values
c) property names
d) objects

Answer: c
Explanation: The above code snippet contains an object inside another object. firstname and surname are the property names. The value of that particular property is itself an object.

4. A linkage of series of prototype objects is called as ________
a) prototype stack
b) prototype chain
c) prototype class
d) prototypes

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

5. In the following syntax, the data type within the square brackets must be ___________
book[datatype]=assignment_value;
a) An integer
b) A String
c) An object
d) Floating point

Answer: b
Explanation: The value inside the square bracket is used to access that data element or the property of the object. When using square bracket notation, the expression inside the square brackets must evaluate to a string or a value that can be converted to a string.

6. To determine whether one object is the prototype of (or is part of the prototype chain of) another object, one should use the ____________
a) isPrototypeOf() method
b) equals() method
c) === operator
d) ==opertor

Answer: a
Explanation: Prototype is a global property which is available with almost all the objects. To determine whether one object is the prototype of (or is part of the prototype chain of) another object, one should use the isPrototype() method. To find out if p is the prototype of o write p.isPrototypeOf(o).

7. What is the prototype represents in the following JavaScript code snippet?
function f() {};
a) Function f
b) A custom constructor
c) Prototype of a function
d) Not valid

Answer: b
Explanation: All object instances have a constructor property that point to the constructor function that created it. A custom constructor is a constructor which requires no arguments and is created automatically by the compiler at the time of object creation if not created by the user.

8. The purpose of extensible attribute is to __________
a) make all of the own properties of that object non configurable
b) to configure and bring a writable property
c) “lock down” objects into a known state and prevent outside tampering
d) to include new properties into the object

Answer: c
Explanation: Extensible attributes determines whether the object can have new properties or not. Therefore extensible attribute will allow an object to include and write properties into them.

9. Identify the process done in the following JavaScript code snippet?
o = {x:1, y:{z:[false,null,""]}};
s = JSON.stringify(o);
p = JSON.parse(s);
a) Object Encapsulation
b) Object Serialization
c) Object Abstraction
d) Object Encoding

Answer: b
Explanation: Object serialization is the process of converting an object’s state to a string from which it can later be restored. The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string.

10. The basic purpose of the toLocaleString() is to _________
a) return a localised object representation
b) return a parsed string
c) return a local time in the string format
d) return a localized string representation of the object

Answer: d
Explanation: toLocaleString is a predefined function in javascript which is used to return a localized string representation of the object. For example the date.toLocaleString() is an inbuilt function in JavaScript which is used to convert a date and time to a string.