JavaScript Questions and Answers Part-16

1. What will be the output of the following JavaScript code?
var set = new Set();
set.add("one");
set.add("two");
for (let elements of set)
{
document.writeln(elements+" ");
}
a) one
b) two
c) one two
d) undefined

Answer: c
Explanation: The JavaScript Set add() method is used to add an element to Set object with a specified value. Each element must have a unique value.

2. What will be the output of the following JavaScript code?
set.add("AngularJS");
set.add("Bootstrap");
set.delete("Bootstrap");
document.writeln(set.size);
a) 2
b) 1
c) 0
d) undefined

Answer: b
Explanation: The delete() method is used to remove all the elements from the Set object which are passed as an argument to the delete function. Since the delete function is called once the size will be reduced to 1.

3. What will be the output of the following JavaScript code?
set.add("one");
set.add("two");
set.add("three");
set.clear();
document.writeln(set.size);
a) one
b) 1
c) 2
d) 0

Answer: d
Explanation: JavaScript Set clear() method is used to remove all the elements from Set object. The clear method will remove all the entries and hence the size of the set will be zero.

4. What will be the output of the following JavaScript code?
set.add("one");
set.add("two”);
var itr=set.values();
document.writeln(itr.next().value);
a) one
b) Two
c) error
d) undefined

Answer: a
Explanation: values() method returns an object of new Set iterator. This object contains the value for each element. It maintains insertion order.

5. What will be the output of the following JavaScript code?
set.add("1");
set.add("2");
document.writeln(set.has("3"));
a) 3
b) True
c) false
d) 2

Answer: c
Explanation: The Set has() method indicates whether the Set object contains the specified value. It returns true if the specified value is present, otherwise false.

6. What will be the output of the following JavaScript code?
const pi=3.14;
var pi=4;
console.log(pi);
a) This will flash an error
b) Prints 4
c) Prints 3.14
d) Ambiguity

Answer: a
Explanation: Const keyword fixes the value of the variable. Const keyword can not be redefined. Therefore attempts to alter the value or re-declaration causes errors.

7. The "let" keyword cannot be used ___________
a) as a substitute of var
b) as a block statement to define new variables
c) to define variables that are scoped to a single expression
d) in a else if loop, as a substitute for var

Answer: d
Explanation: The let keyword can be used in four ways :
1. as a variable declaration like var;
2. in a for or for/in loop, as a substitute for var;
3. as a block statement, to define new variables and explicitly delimit their scope; and
4. to define variables that are scoped to a single expression.

8. The main difference between the variables declared with var and with let is __________
a) var is confined to a particular function but let is not
b) let is confined to a particular function but var is not
c) var defines values based on conditions but let does not
d) let doesn’t let you change the value of the variable

Answer: b
Explanation: Variables declared with var have global scope whereas variable declared with let have block scope. Variables declared with let are defined only within the closest enclosing block (and any blocks nested within it, of course).

9. What will be the output of the following JavaScript code if oddsums(5); is executed after the following code snippet?
function oddsums(n)
{
let total = 0, result=[];
for(let x = 1; x <= n; x++)
{
let odd = 2*x-1;
total += odd;
result.push(total);
}
return result;
}
a) Returns [1,4,9,16,25]
b) Returns [1,2,3,4,5]
c) Returns [3,6,9,12,15]
d) Returns [1,3,5,7,9]

Answer: a
Explanation: Let keyword has block scope thus in the above code snippet the total variable will be defined inside the for loop. The above code returns 1, 4, 9, 16, 25 which is the square of the first five natural numbers.

10. What would be the result or type of error if p is not defined in the following JavaScript code snippet?
console.log(p)
a) zero
b) null
c) Reference Error
d) Value not found Error

Answer: c
Explanation: Console.log() is a predefined function in javascript which is used to print data or message on the console. If the argument of the console.log is not defined it will give a reference error.