Q
What does the following JavaScript code snippet do? Code: let num = 5; while (num > 0) { console.log(num); num--; }

Answer & Solution

Answer: Option B
Solution:
The code snippet uses a while loop to execute a block of code repeatedly until the condition num > 0 becomes false. It starts with num being 5 and prints the value of num to the console in each iteration, then decrements num by 1. This process continues until num becomes 0, at which point the loop stops. Therefore, the output will be: 5 4 3 2 1
Related Questions on Average

What will be the output of the following JavaScript code? Code: let nums = [1, 2, 3, 4, 5]; for (let n of nums) { if (n % 2 === 0) { console.log(n); } }

A). 1 3 5

B). 2 4 6

C). 1 2 3 4 5

D). 2 4

What does the following JavaScript code snippet do? Code: let result = x > 0 ? 'Positive' : (x < 0 ? 'Negative' : 'Zero');

A). Assigns 'Positive' to result if x is greater than 0, 'Negative' if x is less than 0, otherwise 'Zero'

B). Checks if x is greater than 0 and assigns 'Positive', otherwise checks if x is less than 0 and assigns 'Negative', otherwise assigns 'Zero'

C). Assigns 'Positive' to result if x is less than 0, 'Negative' if x is greater than 0, otherwise 'Zero'

D). None of the above

Which of the following JavaScript statements is used to execute a block of code repeatedly until a specified condition becomes false?

A). Function declaration statement

B). Conditional statement (if statement)

C). Assignment statement

D). Looping statement (do-while loop)

What is the purpose of the following JavaScript statement? break;

A). Assigns a value to a variable

B). Exits the current loop or switch statement

C). Skips the current iteration in a loop

D). Continues to the next iteration in a loop

What will be the output of the following JavaScript code? Code: let i = 1; do { console.log(i); i++; } while (i < 3);

A). 1 2 3

B). 1 2 3 4

C). 2 3 4

D). Infinite loop

What will be the output of the following JavaScript code? Code: let x = 10; while (x > 0) { console.log(x); x -= 2; }

A). 10 8 6 4 2 0

B). 10 8 6 4 2

C). 8 6 4 2 0

D). 9 7 5 3 1 -1

What is the purpose of the following JavaScript statement? let result = x > 0 ? 'Positive' : 'Non-positive';

A). Assigns the value 'Positive' to result if x is greater than 0, otherwise assigns 'Non-positive'

B). Checks if x is greater than 0

C). Prints 'Positive' if x is greater than 0, otherwise prints 'Non-positive'

D). None of the above

What will be the output of the following JavaScript code? Code: let count = 0; do { console.log(count); count++; } while (count < 3);

A). 0 1 2 3

B). 0 1 2

C). 1 2 3

D). Infinite loop

What will be the output of the following JavaScript code? Code: let i = 0; for (; i < 3; i++) { console.log(i); }

A). 0 1 2 3

B). 0 1 2

C). 1 2 3

D). 1 2 3 4

What is the purpose of the following JavaScript statement? continue;

A). Exits the current loop or switch statement

B). Skips the current iteration in a loop

C). Assigns a value to a variable

D). Continues to the next iteration in a loop