Code Snippets-based MCQs

Loops

Table of contents
  1. Easy Level MCQs - Single Loop
  2. Slightly harder MCQs - Single Loop
  3. Tricky MCQs - Single Loop

Easy Level MCQs - Single Loop

    1. Given the following Java code snippet, what will be printed to the console?
    for (int i = 1; i <= 5; i++) {
        System.out.print(i + " ");
    }
    
    1 2 3 4 5
    0 1 2 3 4
    5 4 3 2 1
    1 2 3 4
    Answer

    1 2 3 4 5

  1. What will be the output of the following Java code?

    int count = 0;
    while (count < 3) {
        System.out.print("Hi ");
        count++;
    }
    
    Hi Hi Hi
    Hi Hi
    Hi Hi Hi Hi
    No output
    Answer

    Hi Hi Hi

  2. After executing the following Java code, what will be the value of sum?

    int sum = 0;
    for (int i = 1; i <= 4; i++) {
        sum += i;
    }
    
    10
    4
    0
    20
    Answer

    10

  3. What will the following Java code print?

    int i = 5;
    do {
        System.out.print(i + " ");
        i--;
    } while (i > 0);
    
    5 4 3 2 1
    1 2 3 4 5
    5 4 3 2 1 0
    0 1 2 3 4 5
    Answer

    5 4 3 2 1

  4. How many times will “Java” be printed by the following code?

    int i = 0;
    while (i < 5) {
        System.out.println("Java");
        i += 2;
    }
    
    2
    3
    5
    4
    Answer

    3

  5. What is the output of the following Java code?

    for (int i = 1; i <= 3; i++) {
        for (int j = 1; j <= 2; j++) {
            System.out.print(i * j + " ");
        }
    }
    
    1 2 2 4 3 6
    2 4 6
    1 2 3 4 5 6
    Compilation error
    Answer

    1 2 2 4 3 6

  6. Determine the output of this Java code snippet.

    int i = 0;
    do {
        if (i % 2 == 0) {
            System.out.print(i + " ");
        }
        i++;
    } while (i < 5);
    
    0 2 4
    1 3 5
    0 1 2 3 4
    2 4
    Answer

    0 2 4

  7. What will be printed by the following Java code?

    for (int i = 3; i > 0; i--) {
        System.out.print(i + " ");
    }
    
    3 2 1
    1 2 3
    0 1 2 3
    3 2 1 0
    Answer

    3 2 1

  8. After executing the following Java code, what is the value of product?

    int product = 1;
    for (int i = 1; i <= 3; i++) {
        product *= i;
    }
    
    6
    3
    0
    9
    Answer

    6

  9. What is the output of this Java code?

    int i = 1;
    while (i <= 3) {
        System.out.print("Number: " + i + " ");
        i++;
    }
    
    Number: 1 Number: 2 Number: 3
    Number: 1 Number: 2
    Number: 2 Number: 3 Number: 4
    Number: 0 Number: 1 Number: 2
    Answer

    Number: 1 Number: 2 Number: 3

  10. How many times will “Loop” be printed by this Java code?

    int i = 0;
    do {
        System.out.println("Loop");
        i++;
    } while (i < 2);
    
    2
    1
    0
    Infinite times
    Answer

    2

  11. Determine the output of the following Java code.

    for (int i = 0; i < 5; i += 2) {
        System.out.print(i + " ");
    }
    
    0 2 4
    0 1 2 3 4
    1 3 5
    2 4
    Answer

    0 2 4

  12. What will be printed by this Java code?

    int sum = 0;
    int i = 1;
    while (i <= 3) {
        sum += i;
        i++;
    }
    System.out.println(sum);
    
    6
    3
    0
    9
    Answer

    6

  13. After executing the following Java code, what will be the output?

    int i = 5;
    do {
        System.out.print(i + " ");
        i -= 2;
    } while (i > 0);
    
    5 3 1
    5 4 3 2 1
    5 3
    5 2
    Answer

    5 3 1

  14. What will be the output of this Java code?

    for (int i = 1; i <= 3; i++) {
        for (int j = 1; j <= i; j++) {
            System.out.print("*");
        }
        System.out.println();
    }
    

    *

    *

    *

    *

    *
    *
    *
    No output
    Answer

    *

    *

  15. How many times will “Hello” be printed in the following Java code?

    int i = 0;
    while (i < 4) {
        System.out.println("Hello");
        i++;
    }
    
    4
    5
    3
    Infinite times
    Answer

    4

  16. Determine the output of this Java code snippet.

    int total = 0;
    for (int i = 1; i <= 5; i++) {
        if (i % 2 == 0) {
            continue;
        }
        total += i;
    }
    System.out.println(total);
    
    9
    15
    6
    0
    Answer

    9

  17. What will the following Java code print?

    int i = 1;
    do {
        System.out.print(i + " ");
        i *= 2;
    } while (i < 10);
    
    1 2 4 8
    1 3 5 7 9
    1 2 3 4 5
    2 4 8
    Answer

    1 2 4 8

  18. After executing this Java code, what will be the value of count?

    int count = 0;
    for (int i = 0; i < 5; i++) {
        if (i % 2 == 0) {
            count++;
        }
    }
    
    3
    2
    5
    0
    Answer

    3

  19. What will be printed by the following Java code?

    for (int i = 1; i <= 3; i++) {
        for (int j = 3; j >= i; j--) {
            System.out.print(j + " ");
        }
        System.out.println();
    }
    

    3 2 1
    3 2
    3

    3 2 1
    2 1
    1

    3 2 1
    3 2 1
    3 2 1
    Compilation error
    Answer

    3 2 1
    3 2
    3

  20. How many times will “Test” be printed in this Java code?

    int i = 0;
    do {
        System.out.println("Test");
        i++;
    } while (i < 0);
    
    1
    0
    Infinite times
    2
    Answer

    1

  21. Determine the output of this Java code.

    for (int i = 1; i <= 5; i++) {
        if (i == 3) {
            break;
        }
        System.out.print(i + " ");
    }
    
    1 2
    1 2 3 4 5
    3 4 5
    1 2 3
    Answer

    1 2

  22. What will the following Java code output?

    int i = 10;
    while (i > 0) {
        System.out.print(i + " ");
        i -= 3;
    }
    
    10 7 4 1
    10 9 8 7 6 5 4 3 2 1
    10 7 4
    10 7 4 1 -2
    Answer

    10 7 4 1

  23. How many times will the inner loop execute in total in the following Java code?

    for (int i = 1; i <= 2; i++) {
        for (int j = 1; j <= 3; j++) {
            System.out.println("Inner Loop");
        }
    }
    
    6
    5
    3
    2
    Answer

    6

  24. What will be printed by this Java code?

    int i = 1;
    do {
        if (i == 3) {
            i++;
            continue;
        }
        System.out.print(i + " ");
        i++;
    } while (i <= 5);
    
    1 2 4 5
    1 2 3 4 5
    1 2 3 5
    1 2 3 4
    Answer

    1 2 4 5

  25. Determine the output of this Java code snippet.

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            System.out.print(i + j + " ");
        }
    }
    
    0 1 1 2
    0 1 0 1
    1 2 3 4
    0 1 2 3
    Answer

    0 1 1 2

  26. What will be the final value of result after executing this Java code?

    int result = 1;
    int i = 1;
    while (i <= 3) {
        result *= i;
        i++;
    }
    
    6
    3
    0
    9
    Answer

    6

  27. How many times will “Executing” be printed by the following Java code?

    int i = 0;
    while (true) {
        System.out.println("Executing");
        if (i == 2) {
            break;
        }
        i++;
    }
    
    3
    2
    1
    Infinite times
    Answer

    3

  28. What will be the output of this Java code?

    for (int i = 1; i <= 3; i++) {
        System.out.print("Outer" + i + " ");
        for (int j = 1; j <= 2; j++) {
            System.out.print("Inner" + j + " ");
        }
    }
    
    Outer1 Inner1 Inner2 Outer2 Inner1 Inner2 Outer3 Inner1 Inner2
    Inner1 Inner2 Outer1 Inner1 Inner2 Outer2 Inner1 Inner2 Outer3
    Outer1 Outer2 Outer3 Inner1 Inner2
    Outer1 Outer2 Outer3 Inner1 Inner2 Inner1 Inner2 Inner1 Inner2
    Answer

    Outer1 Inner1 Inner2 Outer2 Inner1 Inner2 Outer3 Inner1 Inner2

  29. Determine the output of the following Java code.

    int i = 0;
    do {
        System.out.print(i + " ");
        i++;
    } while (i < 3);
    
    0 1 2
    1 2 3
    0 1 2 3
    0 1
    Answer

    0 1 2

Back to Top

Slightly harder MCQs - Single Loop

  1. What will be the output of the following code?

    int sum = 0;
    for (int i = 1; i <= 5; i++) {
        sum += i;
    }
    System.out.println(sum);
    
    10
    15
    5
    Compilation error
    Answer

    15

  2. What will be the output of the following code?

    int i = 0;
    while (i < 3) {
        i++;
    }
    System.out.println(i);
    
    2
    3
    4
    Compilation error
    Answer

    3

  3. What will be the output of the following code?

    int i = 0;
    do {
        i++;
    } while (i < 0);
    System.out.println(i);
    
    0
    1
    2
    Compilation error
    Answer

    1

  4. What will be the output of the following code?

    int product = 1;
    for (int i = 1; i <= 4; i++) {
        product *= i;
    }
    System.out.println(product);
    
    24
    16
    4
    10
    Answer

    24

  5. What will be the output of the following code?

    int i = 5;
    while (i > 0) {
        i--;
    }
    System.out.println(i);
    
    0
    1
    -1
    Compilation error
    Answer

    0

  6. What will be the output of the following code?

    int i = 1;
    do {
        i *= 2;
    } while (i < 10);
    System.out.println(i);
    
    8
    10
    16
    Compilation error
    Answer

    16

  7. What will be the output of the following code?

    int i = 1;
    for (; i <= 3; i++) {
        System.out.print(i + " ");
    }
    
    1 2 3
    1 2 3 4
    0 1 2 3
    Compilation error
    Answer

    1 2 3

  8. What will be the output of the following code?

    int count = 0;
    while (count < 2) {
        System.out.print(count + " ");
        count++;
    }
    
    0 1
    1 2
    0 1 2
    Compilation error
    Answer

    0 1

  9. What will be the output of the following code?

    int sum = 0;
    for (int i = 0; i < 4; i++) {
        sum += i;
    }
    System.out.println(sum);
    
    6
    4
    10
    Compilation error
    Answer

    6

  10. What will be the output of the following code?

    int i = 3;
    while (i <= 3) {
       i++;
    }
    System.out.println(i);
    
    3
    4
    5
    Compilation error
    Answer

    4

  11. What will be the output of the following code?

    int i = 1;
    do {
       i++;
    } while (i < 2);
    System.out.println(i);
    
    0
    1
    2
    3
    Answer

    2

  12. What will be the output of the following code?

    int i = 0;
    for (i = 1; i <= 5; i++) {
       if (i == 3) {
           continue;
       }
       System.out.print(i + " ");
    }
    
    1 2 3 4 5
    1 2 4 5
    1 2 3 5
    Compilation error
    Answer

    1 2 4 5

  13. What will be the output of the following code?

    int i = 0;
    do {
       i++;
       if (i == 2) {
           break;
       }
    } while (i < 5);
    System.out.println(i);
    
    1
    2
    3
    Compilation error
    Answer

    2

  14. What will be the output of the following code?

    int i = 1;
    for (i = 1; i <= 3; i++) {
       if (i % 2 == 0) {
           continue;
       }
       System.out.print(i + " ");
    }
    
    1 2 3
    1 3
    2
    Compilation error
    Answer

    1 3

  15. What will be the output of the following code?

    int i = 10;
    while (i > 5) {
       i -= 2;
    }
    System.out.println(i);
    
    6
    8
    4
    2
    Answer

    4

  16. What will be the output of the following code?

    int sum = 0;
    for (int i = 1; i <= 3; i++) {
       sum += i;
    }
    sum *= 2;
    System.out.println(sum);
    
    6
    12
    9
    18
    Answer

    12

  17. What will be the output of the following code?

    int i = 1;
    do {
       i += 3;
    } while (i < 5);
    System.out.println(i);
    
    4
    7
    1
    3
    Answer

    4

  18. What will be the output of the following code?

    int i = 2;
    while (i < 10) {
       i *= 2;
    }
    System.out.println(i);
    
    8
    16
    10
    2
    Answer

    16

  19. What will be the output of the following code?

    int i = 5;
    for (int j = 0; j < 3; j++) {
       i++;
    }
    System.out.println(i);
    
    6
    7
    8
    9
    Answer

    8

  20. What will be the output of the following code?

    int i = 0;
    while (i < 4) {
       i += 2;
    }
    System.out.println(i);
    
    2
    4
    6
    0
    Answer

    4

  21. What will be the output of the following code?

    int sum = 0;
    for (int i = 2; i <= 6; i += 2) {
       sum += i;
    }
    System.out.println(sum);
    
    8
    12
    14
    20
    Answer

    12

  22. What will be the output of the following code?

    int i = 1;
    do {
       i++;
       if (i == 3) {
           continue;
       }
    } while (i < 5);
    System.out.println(i);
    
    2
    4
    5
    3
    Answer

    5

  23. What will be the output of the following code?

    int i = 4;
    while (i > 1) {
       i--;
    }
    System.out.println(i);
    
    1
    0
    2
    4
    Answer

    1

  24. What will be the output of the following code?

    int i = 3;
    for (; i <= 6; i += 2) {
       System.out.print(i + " ");
    }
    
    3 5
    3 5 7
    3 4 5 6
    3 5 6
    Answer

    3 5

  25. What will be the output of the following code?

    int sum = 0;
    for (int i = 0; i < 3; i++) {
       sum += 2;
    }
    System.out.println(sum);
    
    3
    4
    6
    8
    Answer

    6

  26. What will be the output of the following code?

    int i = 2;
    do {
       i += 3;
    } while (i < 10);
    System.out.println(i);
    
    5
    11
    2
    5
    Answer

    11

  27. What will be the output of the following code?

    int sum = 1;
    for (int i = 1; i <= 3; i++) {
       sum *= i;
    }
    System.out.println(sum);
    
    6
    3
    9
    1
    Answer

    6

  28. What will be the output of the following code?

    int i = 1;
    while (i <= 3) {
       System.out.print(i + " ");
       i++;
    }
    
    1 2
    1 2 3
    1
    1 3
    Answer

    1 2 3

  29. What will be the output of the following code?

    int i = 2;
    do {
       System.out.print(i + " ");
       i++;
    } while (i < 4);
    
    2
    2 3
    2 3 4
    3 4
    Answer

    2 3

  30. What will be the output of the following code?

    int i = 0;
    for (; i < 3; i++) {
       System.out.print(i + " ");
    }
    
    0 1 2
    1 2 3
    1 2
    0 1
    Answer

    0 1 2

Back to Top

Tricky MCQs - Single Loop

  1. What will be the output of the following code?

    int i = 0;
    while (i < 5) {
        i++;
        if (i % 2 == 0) {
            i++;
        }
        System.out.print(i + " ");
    }
    
    1 3 5 7
    2 4 6
    2 3 5
    1 2 4 5
    Answer

    2 3 5 7

  2. What will be the output of the following code?

    int i = 1;
    do {
        i *= 2;
        if (i > 4) {
            break;
        }
        i++;
    } while (i < 8);
    System.out.println(i);
    
    2
    3
    4
    5
    Answer

    5

  3. What will be the output of the following code?

    int i = 10;
    for (int j = 0; j < 3; j++) {
        i -= j;
    }
    System.out.println(i);
    
    9
    8
    7
    6
    Answer

    7

  4. What will be the output of the following code?

    int i = 1;
    while (i <= 3) {
        System.out.print(i + " ");
        i++;
        if (i == 2) {
            i += 2;
        }
    }
    
    1 3
    1 2 4
    1 4
    1 2 3
    Answer

    1 4

  5. What will be the output of the following code?

    int i = 0;
    do {
        i++;
        if (i % 3 == 0) {
            continue;
        }
        System.out.print(i + " ");
    } while (i < 5);
    
    1 2 4 5
    1 2 3 4
    2 4 5
    1 2 4
    Answer

    1 2 4 5

  6. What will be the output of the following code?

    int i = 1;
    for (int j = 0; j < 5; j++) {
        if (j % 2 == 0) {
            i += j;
        } else {
            i -= j;
        }
    }
    System.out.println(i);
    
    0
    1
    2
    3
    Answer

    1

  7. What will be the output of the following code?

    int i = 1;
    while (i < 4) {
        i += i;
        System.out.print(i + " ");
    }
    
    1 3
    2 4
    2 4 8
    2 6
    Answer

    2 4 8

  8. What will be the output of the following code?

    int i = 2;
    do {
        i *= 2;
    } while (i < 16);
    System.out.println(i);
    
    8
    16
    32
    64
    Answer

    32

  9. What will be the output of the following code?

    int i = 1;
    for (int j = 5; j > 1; j--) {
        i += j;
    }
    System.out.println(i);
    
    15
    14
    16
    11
    Answer

    15

  10. What will be the output of the following code?

    int i = 1;
    while (i < 5) {
       if (i == 3) {
           break;
       }
       i++;
    }
    System.out.println(i);
    
    2
    3
    4
    5
    Answer

    3

Back to Top