JavaScript Course | Practice Quiz-3

Question 1

What will be the output of the following code? 

JavaScript

<script>
let result = 0;
for (let i = 0; i < 5; i++) {
  result += i;
}
document.write(result);
</script>
Cross

5

Cross

0

Tick

10

Cross

None of the above



Question 1-Explanation: 

The loop will run from 0 to 4, thus adding 0+1+2+3+4 = 10. So, the answer will be 10.

Question 2
True or false: All keys in an object are strings.
Tick
True
Cross
False
Cross
Depends on the Object type
Cross
None of the above


Question 2-Explanation: 
keys can only be strings, and numeric keys such as those used in Arrays are coerced and stored as strings.
Question 3
Given a collection of artists and lists of their songs, would you store the artist-song-list pairs in an Object or an Array?
Tick
Object
Cross
Array
Cross
Function
Cross
None of the above


Question 3-Explanation: 
If you want to store a bunch of numbers or a list of objects of the same type, use an array. But here we have to store list of their strongs as per their name so we will use Object.
Question 4
What will be the output of the following code?
 <script>
 if (5) { 
  document.write("I like peanuts"); 
  }
  </script>
Tick
I like peanuts
Cross
undefined
Cross
nothing will be printed
Cross
None of the above


Question 4-Explanation: 
In Javascript, Any natural number other than 0 is taken as true, therefore "I like peanuts" will be printed.
Question 5
What will be the output of the following code?
<script>
let bar  = 1;
foo = {};
foo: {
     bar : 2;
     baz : ++bar;
};
document.write(foo.baz + foo.bar + bar);
</script>
Tick
NaN
Cross
5
Cross
4
Cross
1


Question 5-Explanation: 
It's not actually altering the variable foo. Here the text foo is part of a label, and the object that follows is called a javascript block and it contains two labeled statements, not variable assignments. That leaves us with two undefined variables added to 1. Which is NaN. If the variable bar was not declared higher in the code this object would kick out a syntax error as bar would be undefined.
Question 6

What will be the output of the following code? 

< script>

document.write( 10 > 9 > 8 === true );

</ script> 

Cross

true

Tick

false

Cross

1

Cross

0



Question 6-Explanation: 

As humans we read the above something like, "ten is greater than 9 which is greater than 8", or: (10 > 9) && (9>8) Javascript, however, is beholden to its order of operations implementation. Which causes the line to be evaluated as: ((10>9)>8) or (true>8) or (1>8) which is false.

Question 7
What will be the output of the following code?
<script>
  document.write(String('Hello') == 'Hello');
</script>
Tick
true
Cross
false
Cross
1
Cross
0


Question 7-Explanation: 
They are equal because both of them are strings.
Question 8
What will be the output of the following code?
<script>
document.write(( true + false ) > 2 + true );
</script>
Cross
true
Tick
false
Cross
1
Cross
0


Question 8-Explanation: 
Here, True will change to 1, and False to 0. Hence, (1+0)>(2+1) => 1>3 is False.
Question 9
What will be the output of the following code?
<script>
document.write(Number('1') - 1 == 0);
</script>
Tick
true
Cross
false
Cross
1
Cross
None of the above


Question 9-Explanation: 
You're casting the string 1 as a number, and subtracting it from the number 1. This equals 0.
Question 10
What will be the output of the following code?
<script>
 document.write(NaN == NaN);
</script>
Cross
true
Tick
false
Cross
1
Cross
0


Question 10-Explanation: 
Equality operator (== and ===) cannot be used to test a value against NaN. Use Number.isNaN() or isNaN() instead. Hence, it is not equal.
There are 10 questions to complete.

  • Last Updated : 27 Sep, 2023

Similar Reads