JavaScript Questions and Answers Part-7

1. What will be the output of the following JavaScript code?
const object1 = {};
a = Symbol('a');
b = Symbol.for('b');
object1[a] = 'harry';
object1[b] = 'derry';
const objectSymbols = Object.getOwnPropertySymbols(object1);
console.log(objectSymbols.length);
a) 0
b) 2
c) 1
d) error

Answer: b
Explanation: The Object.getOwnPropertySymbols() method returns an array of all symbol properties found directly on an object. This method returns an empty array unless you have set symbol properties on your object.

2. What will be the output of the following JavaScript code?
const obj1 =
{
property1: 21
}
const descriptor1 = Object.getOwnPropertyDescriptor(obj1, 'property1');
console.log(descriptor1.configurable);
console.log(descriptor1.enumerable);
a) true 21
b) true false
c) true true
d) false false

Answer: c
Explanation: The Object.getOwnPropertyDescriptor method allows to query the full information about a property. The method returns a property descriptor for an own property that is one directly present on an object and not in the object’s prototype of a given object.

3. What will be the output of the following JavaScript code?
const obj1 = { property1: '10'};
const obj2 = Object.freeze(obj1);
obj2.property1 = '20';
console.log(obj2.property1);
a) 10
b) 20
c) Runtime error
d) Compilation error

Answer: a
Explanation: The Object.freeze() method “freezes” the properties of object and prevents new properties from being added to it. This method prevents the modification of existing property, attributes, and values.

4. What will be the output of the following JavaScript code?
const object1 = {
property1: 20
};
console.log(Object.is(object1));
a) 20
b) true
c) false
d) error

Answer: c
Explanation: The Object.is() method of JavaScript is used to determine whether two values are the same value. There is a special built-in method that compares values. This method returns a Boolean indicating whether or not the two arguments are the same value.

5. What will be the output of the following JavaScript code?
const obj = {prop: 12};
Object.preventExtensions(obj);
console.log( Object.isExtensible(obj));
a) 12
b) false
c) true
d) error

Answer: b
Explanation: The Object.preventExtensions() only prevents the addition of new properties from ever being added to an object. This change is a permanent that means once an object has been made non-extensible, it cannot be made extensible again.

6. What is the observation made in the following JavaScript code?
var count = [1,,3];
a) The omitted value takes “undefined”
b) This results in an error
c) This results in an exception
d) The omitted value takes an integer value

Answer: a
Explanation: Array is defined with a null value when no value is mentioned. If you omit a value from an array literal, the omitted element is given an undefined value.

7. What will be the output of the following JavaScript code?
var a1 = [,,,];
var a2 = new Array(3);
0 in a1
0 in a2
a) true false
b) false true
c) true true
d) false true

Answer: a
Explanation: Array a1 is defined with null values. Therefore we can access the indexes 0, 1 and 2. But array a2 is only defined not declared. Therefore we cannot access index 0.

8. The pop() method of the array does which of the following task?
a) decrements the total length by 1
b) increments the total length by 1
c) prints the first element but no effect on the length
d) updates the element

Answer: a
Explanation: pop() function pops out that is delete the last element from the array. Hence pop() method (it works with push()) reduces the length of an array by 1.

9. What is the observation made in the following JavaScript code?
if (!a[i]) continue;
a) Skips the defined elements
b) Skips the existent elements
c) Skips the null elements
d) Skips the defined & existent elements

Answer: c
Explanation: The if loop in the above code checks whether the value of a[i] exists or not. For undefined, non existent and null values the if loop returns true.

10. What will happen if reverse() and join() methods are used simultaneously?
a) Reverses and stores in the same array
b) Reverses and concatenates the elements of the array
c) Reverses
d) Stores the elements of an array in normal order

Answer: a
Explanation: The array.join() method is an inbuilt function in JavaScript which is used to join the elements of an array into a string. The reverse() followed by a join() will reverse the respective array and will store the reversed array in the memory.