JavaScript Course | Practice Quiz-3


1
Question 1
What will be the output of the following code?
<script>
let result = 0;
for (let i = 0; i < 5; i++) {
  result += i;
}
document.write(result);
</script>
A
5
B
0
C
10
D
None of the above
JavaScript Course Quiz 3    
Discuss it


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.
A
True
B
False
C
Depends on the Object type
D
None of the above
JavaScript Course Quiz 3    
Discuss it


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?
A
Object
B
Array
C
Function
D
None of the above
JavaScript Course Quiz 3    
Discuss it


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>
A
I like peanuts
B
undefined
C
nothing will be printed
D
None of the above
JavaScript Course Quiz 3    
Discuss it


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>
A
NaN
B
5
C
4
D
1
JavaScript Course Quiz 3    
Discuss it


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> 
A
true
B
false
C
1
D
0
JavaScript Course Quiz 3    
Discuss it


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>
A
true
B
false
C
1
D
0
JavaScript Course Quiz 3    
Discuss it


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>
A
true
B
false
C
1
D
0
JavaScript Course Quiz 3    
Discuss it


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>
A
true
B
false
C
1
D
None of the above
JavaScript Course Quiz 3    
Discuss it


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>
A
true
B
false
C
1
D
0
JavaScript Course Quiz 3    
Discuss it


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.
1


My Personal Notes arrow_drop_up


Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.