Javascript | Control Flow

Last Updated :
Discuss
Comments

Question 1

What is the role of break keyword inside a loop?

  • A

    Restart the loop

  • B

    Move out of the loop

  • C

    Ignore the rest of the statements below it and continue the loop

  • D

    None of the above

Question 2

What will be the output of the following code snippet?

JavaScript
let data = 7
while(data>=0){
    if (data<=5){
        data--;
        continue;
    }
    console.log(data);
    data--;
}



  • A

    7 6 5 4 3 2 1 0

  • B

    7 6 5

  • C

    7 6

  • D

    0 1 2 3 4 5

Question 3

What will be the output for the following code snippet?

JavaScript
let i = 5;
while (true) {
    console.log(i);
    i += 5;
    if (i > 30) {
        break;
    }
}
  • A

    Infinite loop

  • B

    5 10 15 20 25 30

  • C

    5 10 15 20 25

  • D

    5 10 15 20 25 30 35

Question 4

Which of the following is NOT a valid conditional statement in JavaScript?

  • A

    if

  • B

    else if

  • C

    switch

  • D

    elseif

Question 5

Which loop always executes at least once, even if the condition is false?

  • A

    for

  • B

    while

  • C

    do...while

  • D

    for...in

Question 6

What will the following code output?

JavaScript
for (let i = 0; i < 3; i++) {
    if (i === 1) break;
    console.log(i);
}


  • A

    0, 1, 2

  • B

    0

  • C

    1

  • D

    0, 1

Question 7

When is a switch statement generally preferred over an if-else statement in JavaScript?

  • A

    When checking for a single condition

  • B

    When comparing a single variable against multiple specific values

  • C

    When performing complex logical operations

  • D

    When iterating over arrays

Question 8

What is the output of the following code?

JavaScript
let age = 20;
let status = age >= 18 ? "Adult" : "Minor";
console.log(status);
  • A

    Adult

  • B

    Minor

  • C

    undefined

  • D

    Error

Question 9

Which statement is used to throw a custom error in JavaScript?

  • A

    throw

  • B

    error

  • C

    catch

  • D

    raise

Question 10

What does this code output?

JavaScript
let score = 85;
let grade = score >= 90 ? "A" : score >= 80 ? "B" : "C";
console.log(grade);
  • A

    A

  • B

    B

  • C

    C

  • D

    Error

There are 10 questions to complete.

Take a part in the ongoing discussion