Code Snippets-based MCQs

Nested Loops

Table of contents
  1. Easy Level MCQs
  2. MCQs on Common Problems
  3. Tricky MCQs

Easy Level MCQs - Nested Loops

Below are 30 simple questions about nested loops in Java:

  1. Given the following code snippet, what will be the output?
     for (int i = 0; i < 2; i++) {
         for (int j = 0; j < 2; j++) {
             System.out.println("i=" + i + ", j=" + j);
         }
     }
    
    i=0, j=0 and i=0, j=1
    i=0, j=0; i=0, j=1; i=1, j=0; i=1, j=1
    i=1, j=1 only
    No output
    Answer

    i=0, j=0; i=0, j=1; i=1, j=0; i=1, j=1

  2. Given the following code snippet, what is printed to the console?
     int count = 0;
     for (int i = 0; i < 2; i++) {
         for (int j = 0; j < 3; j++) {
             count++;
         }
     }
     System.out.println(count);
    
    3
    6
    2
    5
    Answer

    6
    (Loop runs 2 times in the outer loop and 3 times in the inner loop: 2 * 3 = 6)

  3. What will be the output of the following code snippet?
     int sum = 0;
     for (int i = 1; i <= 2; i++) {
         for (int j = 1; j <= 2; j++) {
             sum += (i + j);
         }
     }
     System.out.println(sum);
    
    4
    8
    12
    16
    Answer

    12
    (Expanding: When i=1, j=1 → sum += 2; i=1, j=2 → sum += 3; i=2, j=1 → sum += 3; i=2, j=2 → sum += 4; total = 2+3+3+4 = 12)

  4. Given the following nested loop, what gets printed?
     for (int i = 1; i <= 2; i++) {
         for (int j = 1; j <= 2; j++) {
             System.out.print(i * j + " ");
         }
     }
    
    1 2 2 4
    1 1 2 2
    1 2
    2 4
    Answer

    1 2 2 4
    (i=1, j=1 → 1; i=1, j=2 → 2; i=2, j=1 → 2; i=2, j=2 → 4)

  5. What will be the output of the following code?
     int total = 0;
     for (int i = 0; i < 3; i++) {
         for (int j = i; j < 3; j++) {
             total++;
         }
     }
     System.out.println(total);
    
    9
    6
    3
    0
    Answer

    6
    (Explanation: i=0 → j runs 0 to 2 (3 times), i=1 → j runs 1 to 2 (2 times), i=2 → j runs 2 to 2 (1 time). Total = 3+2+1 = 6)

  6. What will be printed to the console?
     for (int i = 1; i <= 2; i++) {
         for (int j = 3; j >= 2; j--) {
             System.out.print((i + j) + " ");
         }
     }
    
    4 3 5 4
    4 5 3 4
    3 2 4 3
    4 3 4 3
    Answer

    4 3 5 4
    (When i=1: j=3 → 4, j=2 → 3; when i=2: j=3 → 5, j=2 → 4)

  7. Given the following code, how many stars (*) get printed?
     int count = 0;
     for (int i = 0; i < 2; i++) {
         for (int j = 0; j < 3; j++) {
             System.out.print("*");
             count++;
         }
     }
     System.out.println();
     System.out.println(count);
    
    3 stars, then 3 printed as count
    4 stars, then 4 printed as count
    6 stars, then 6 printed as count
    2 stars, then 2 printed as count
    Answer

    6 stars, then 6 as count
    (Because 2 (outer loop) × 3 (inner loop) = 6 total iterations)

  8. What is the printed result?
     for (int i = 0; i < 2; i++) {
         for (int j = 0; j < 2; j++) {
             if (j == 1) {
                 System.out.print("X ");
             } else {
                 System.out.print("O ");
             }
         }
     }
    
    O O O O
    X X X X
    O X O X
    X O X O
    Answer

    O X O X
    (When i=0: j=0 → O, j=1 → X; when i=1: j=0 → O, j=1 → X)

  9. How many times does the string “Hello” print?
     int count = 0;
     for (int i = 0; i < 3; i++) {
         for (int j = 0; j < i; j++) {
             System.out.println("Hello");
             count++;
         }
     }
     System.out.println(count);
    
    0 times
    1 time
    3 times
    6 times
    Answer

    3 times
    (Explanation: i=0 → inner loop runs 0 times, i=1 → inner loop runs 1 time, i=2 → inner loop runs 2 times. Total = 0+1+2 = 3)

  10. What does the following code print out?
    for (int i = 1; i <= 2; i++) {
        for (int j = 2; j <= 3; j++) {
            System.out.print(i + j + " ");
        }
    }
    
    2 3 3 4
    3 4 3 4
    2 3 4 5
    3 4 4 5
    Answer

    3 4 4 5
    (First iteration i=1: j=2 → 3, j=3 → 4; second iteration i=2: j=2 → 4, j=3 → 5)

  11. How many times does the string "Hello" print?
    int count = 0;
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            System.out.println("Hello");
            count++;
        }
    }
    System.out.println(count);
    
    2 times
    3 times
    4 times
    1 time
    Answer

    4 times
    (Explanation: Outer loop runs 2 times; inner loop runs 2 times for each outer iteration. Total = 2 × 2 = 4)

  12. What is printed by this code?
    for (int i = 1; i <= 2; i++) {
        for (int j = 1; j <= 2; j++) {
            System.out.print(i + j + " ");
        }
    }
    
    2 3 3 4
    2 3 4 5
    1 2 3 4
    3 4 3 4
    Answer

    2 3 3 4
    (First iteration: i=1, j=1→2, j=2→3; second iteration: i=2, j=1→3, j=2→4)

  13. How many times will the inner loop run in total?
    int totalIterations = 0;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 2; j++) {
            totalIterations++;
        }
    }
    System.out.println(totalIterations);
    
    2
    3
    5
    6
    Answer

    6
    (Outer loop: 3 iterations; inner loop: 2 iterations per outer loop. 3 × 2 = 6)

  14. What does this code print?
    for (int i = 0; i < 2; i++) {
        for (int j = i; j < 2; j++) {
            System.out.print("X ");
        }
        System.out.println();
    }
    

    X X
    X X

    X X
    X

    X
    X

    (No output)
    Answer
      X X
      X
    

    (Explanation: For i=0: j=0 to 1 → prints “X X”. For i=1: j=1 → prints “X”.)

  15. Which statement best describes the value of count?
    int count = 0;
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j <= i; j++) {
            count++;
        }
    }
    System.out.println(count);
    
    1
    2
    3
    4
    Answer

    3
    (Explanation: i=0 → inner loop runs 1 time; i=1 → inner loop runs 2 times. Total = 1+2 = 3)

  16. What output is produced?
    for (int i = 1; i <= 2; i++) {
        for (int j = 1; j <= 3; j++) {
            System.out.print((i * j) + " ");
        }
        System.out.println();
    }
    

    1 2 3
    2 4 6

    1 2 3
    4 5 6

    2 3 4
    4 6 8

    1 2 3
    3 6 9
    Answer
      1 2 3
      2 4 6
    

    (First row: i=1 → 1×1, 1×2, 1×3; second row: i=2 → 2×1, 2×2, 2×3)

  17. What is the final value of result?
    int result = 1;
    for (int i = 1; i <= 2; i++) {
        for (int j = 1; j <= 2; j++) {
            result += (i + j);
        }
    }
    System.out.println(result);
    
    5
    9
    11
    13
    Answer

    13
    Explanation: Loops run 4 times total. Each iteration adds (i+j). Iterations:
    Iteration 1: result=1,i=1,j=1 → result+=1+1=3 Iteration 2: result=3,i=1,j=2 → result+=1+2=6 Iteration 3: result=6,i=2,j=1 → result+=2+1=9 Iteration 4: result=9,i=2,j=2 → result+=2+2=13

  18. What does this nested loop print on each line?
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 2; j++) {
            System.out.print(i + "," + j + " ");
        }
        System.out.println();
    }
    

    0,0 0,1
    1,0 1,1
    2,0 2,1

    0,0
    0,1
    1,0
    1,1
    2,0
    2,1

    i=0 j=0
    i=0 j=1
    i=1 j=0
    i=1 j=1
    i=2 j=0
    i=2 j=1
    No output
    Answer
      0,0 0,1
      1,0 1,1
      2,0 2,1
    
  19. How many times does the inner statement execute?
    int times = 0;
    for (int i = 1; i <= 3; i++) {
        for (int j = 1; j <= 3; j++) {
            if (i == j) {
                times++;
            }
        }
    }
    System.out.println(times);
    
    0
    1
    3
    9
    Answer

    3
    (Checks when i == j for i,j in {1,2,3}, which happens at (1,1), (2,2), (3,3))

  20. What is the output of this nested loop?
    for (int i = 1; i < 3; i++) {
        for (int j = 1; j <= i; j++) {
            System.out.print("*");
        }
        System.out.println();
    }
    

    *


    *
    *


    *
    (No output)
    Answer
      *
          
    
  21. Which numbers are printed?
    for (int i = 0; i < 2; i++) {
        for (int j = 2; j > 0; j--) {
            System.out.print(i + j + " ");
        }
        System.out.println();
    }
    

    2 1
    3 2

    2 3
    1 2

    i=0 j=2 i=0 j=1
    i=1 j=2 i=1 j=1

    2 1 3 2
    (Single line)
    Answer
      2 1
      3 2
    

    (When i=0, j=2→2, j=1→1; When i=1, j=2→3, j=1→2)

  22. What does this code output?
    for (int i = 1; i <= 3; i++) {
        for (int j = 3; j >= i; j--) {
            System.out.print("#");
        }
        System.out.println();
    }
    

    ###
    ##
    #

    #
    ##
    ###

    #
    #
    #
    (No output)
    Answer
      ###
      ##
      #
    
  23. What is printed?
    int i = 0;
    while (i < 2) {
        int j = 0;
        while (j < 2) {
            System.out.print(i + j + " ");
            j++;
        }
        i++;
    }
    
    0 1 1 2
    0 1 2 3
    1 2 1 2
    No output
    Answer

    0 1 1 2
    (Explanation: i=0 → j=0→0, j=1→1; i=1 → j=0→1, j=1→2)

  24. How many lines are printed in total?
    int lines = 0;
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            System.out.println("Line");
            lines++;
        }
    }
    System.out.println(lines);
    
    2
    4
    8
    0
    Answer

    4
    (2 × 2 = 4 total prints in nested loops)

  25. What is printed?
    for (int i = 1; i <= 2; i++) {
        for (int j = i; j <= 2; j++) {
            System.out.print((i + j) + " ");
        }
        System.out.println();
    }
    

    2 3
    3 4

    2 3
    4

    1 2
    1

    2 3 4
    (Single line)
    Answer
      2 3
      4
    

    Explanation: i=1→ j=1..2→ sums=2,3; i=2→ j=2..2→ sum=4 (i+j=2+2=4) but note j=2 only one iteration => 3 + 4? We need to be precise.
    Iteration 1: For i=1: j=1..2 => i+j = 2, 3 → prints “2 3”
    Iteration 2: For i=2: j=2..2 => i+j = 4 → prints “4”
    Actually that prints:
    First line: 2 3
    Second line: 4
    So it’s 2 3 on the first line, 4 on the second line.

  26. Which of the following is the correct final output?
    int sum = 0;
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            sum += j;
        }
    }
    System.out.println(sum);
    
    3
    6
    9
    0
    Answer

    6
    (Inner loop j runs 3 times: j=0,1,2 → sum of j = 3. Outer loop runs 2 times, total sum = 3+3 = 6)

  27. What gets printed here?
    for (int i = 2; i > 0; i--) {
        for (int j = 0; j < i; j++) {
            System.out.print("*");
        }
        System.out.println();
    }
    


    *

    *



    *
    (No output)
    Answer
          
      *
    
  28. How many times does the if condition succeed?
    int matches = 0;
    for (int i = 1; i <= 2; i++) {
        for (int j = 1; j <= 2; j++) {
            if ((i + j) % 2 == 0) {
                matches++;
            }
        }
    }
    System.out.println(matches);
    
    1
    2
    3
    4
    Answer

    2
    (All possible sums of (i+j) with i,j ∈ {1,2} are 2,3,3,4. Even sums are 2 and 4 → 2 matches.)

  29. What is printed by this nested while loop?
    int i = 0;
    while (i < 2) {
        int j = 2;
        while (j > 0) {
            System.out.print((i + j) + " ");
            j--;
        }
        i++;
        System.out.println();
    }
    

    0 1
    1 2

    2 1
    3 2

    2 3
    1 2
    (No output)
    Answer
      2 1
      3 2
    

    (Explanation: i=0 → j=2..1 => sums=2,1; i=1 → j=2..1 => sums=3,2)

  30. What does the following code produce?
    for (int i = 0; i < 3; i++) {
        for (int j = i; j < 3; j++) {
            System.out.print("*");
        }
        System.out.println();
    }
    

    *

    *

    *

    *

    *
    *
    *

    *

    *
    (No extra line)
    Answer
      *
          
      *
    

    (Explanation: i=0→ j=0..2 => 3 stars; i=1→ j=1..2 => 2 stars; i=2→ j=2..2 => 1 star)

Back to Easy-level MCQs - Nested Loops Back to Top

MCQs on Common Problems

Below are 20 questions, each featuring a Java code snippet relevant to common problems such as digit-wise calculations, character pattern printing, prime series, etc.

  1. What is the final value of sum?
    int n = 123;
    int sum = 0;
    while (n > 0) {
        int digit = n % 10;
        sum += digit;
        n /= 10;
    }
    System.out.println(sum);
    
    1
    3
    6
    123
    Answer

    6
    (Explanation: Digits are 3, 2, and 1. Sum = 3 + 2 + 1 = 6)

  2. What is the output of this code?
    for (int num = 1; num <= 10; num++) {
        boolean isPrime = true;
        if (num <= 1) {
            isPrime = false;
        }
        for (int i = 2; i <= num / 2; i++) {
            if (num % i == 0) {
                isPrime = false;
                break;
            }
        }
        if (isPrime) {
            System.out.print(num + " ");
        }
    }
    
    2 primes
    3 primes
    4 primes
    5 primes
    Answer

    4 primes
    (They are 2, 3, 5, 7)

  3. What does the following code snippet print?
    int n = 4;
    long fact = 1;
    for (int i = 1; i <= n; i++) {
        fact = fact * i;
    }
    System.out.println(fact);
    
    24
    16
    4
    1
    Answer

    24
    (Explanation: 4! = 4 × 3 × 2 × 1 = 24)

  4. What is the output of this code snippet?
    int a = 0, b = 1;
    System.out.print(a + " " + b + " ");
    for(int i = 3; i <= 5; i++){
        int c = a + b;
        System.out.print(c + " ");
        a = b;
        b = c;
    }
    
    0 1 1 2 3
    1 1 2 3 5
    0 1 2 3 5
    0 1 1 3 4
    Answer

    0 1 1 2 3
    (First five Fibonacci terms starting with 0, 1)

  5. What does this code print?
    int num = 230;
    int rev = 0;
    while (num > 0) {
        int digit = num % 10;
        rev = rev * 10 + digit;
        num /= 10;
    }
    System.out.println(rev);
    
    32
    23
    032
    0
    Answer

    32
    (Leading zeros are dropped in integer operations)

  6. How many times does "Java" print?
    int number = 129;
    while (number > 0) {
        int digit = number % 10;
        if (digit % 2 == 0) {
            System.out.println("Java");
        }
        number /= 10;
    }
    
    1 time
    2 times
    3 times
    0 times
    Answer

    1 time
    (Digits are 9 (odd), 2 (even), 1 (odd). Only 2 triggers the print)

  7. What character pattern is printed by this code?
    for (char c = 'A'; c <= 'C'; c++) {
        for (char d = 'A'; d <= c; d++) {
            System.out.print(d + " ");
        }
        System.out.println();
    }
    

    A
    A B
    A B C

    A B C
    A B C
    A B C

    A A B A B C
    (No output)
    Answer
      A
      A B
      A B C
    
  8. What is the output?
    int num = 151;
    int original = num;
    int reverse = 0;
    while (num > 0) {
        int digit = num % 10;
        reverse = reverse * 10 + digit;
        num /= 10;
    }
    if (reverse == original) {
        System.out.println("Palindrome");
    } else {
        System.out.println("Not Palindrome");
    }
    
    Palindrome
    Not Palindrome
    151
    0
    Answer

    Palindrome

  9. How many prime numbers will be printed up to 20?
    for (int num = 2; num <= 20; num++) {
        boolean isPrime = true;
        for (int i = 2; i <= Math.sqrt(num); i++) {
            if (num % i == 0) {
                isPrime = false;
                break;
            }
        }
        if (isPrime) {
            System.out.print(num + " ");
        }
    }
    
    6
    8
    9
    10
    Answer

    8
    (The primes up to 20 are 2,3,5,7,11,13,17,19)

  10. What is printed by this numeric pattern code?
    for (int i = 1; i <= 3; i++) {
        for (int j = 1; j <= i; j++) {
            System.out.print(j);
        }
        System.out.println();
    }
    

    1
    12
    123

    123
    123
    123

    1
    1
    1
    (No output)
    Answer
      1
      12
      123
    
  11. How many digits of 407 does the code consider to check if it’s an Armstrong number?
    int n = 407;
    int temp = n;
    int sumOfCubes = 0;
    while (temp > 0) {
        int digit = temp % 10;
        sumOfCubes += digit * digit * digit;
        temp /= 10;
    }
    System.out.println(sumOfCubes == n);
    
    1
    2
    3
    4
    Answer

    3
    (Digits are 7, 0, 4 in the loop)

  12. What does this code snippet print?
    int num = 999;
    int sum = 0;
    for (; num > 0; num /= 10) {
        sum += (num % 10);
    }
    System.out.println(sum);
    
    9
    18
    27
    999
    Answer

    27
    (9 + 9 + 9 = 27)

  13. What is the outpu?
    for (int i = 1; i <= 10; i++) {
        if (i % 3 == 0) {
            System.out.print(i + " ");
        }
    }
    
    3 6
    3 6 9
    3 9
    6 9
    Answer

    3 6 9

  14. Which numbers are printed?
    for (int num = 8; num <= 13; num++) {
        boolean prime = true;
        for (int i = 2; i < num; i++) {
            if (num % i == 0) {
                prime = false;
                break;
            }
        }
        if (prime && num > 1) {
            System.out.print(num + " ");
        }
    }
    
    8 9 10
    11 13
    9 11 13
    8 9 10 11 12 13
    Answer

    11 13
    (8, 9, 10, 12 are not prime)

  15. What pattern of asterisks is printed?
    for (int i = 3; i > 0; i--) {
        for (int j = 0; j < i; j++) {
            System.out.print("*");
        }
        System.out.println();
    }
    

    ***

    *

    ***
    **
    *
    3 times no stars
    No output
    Answer
      ***
      **
      *
    
  16. Which lines will be printed?
    int number = 145;
    while (number > 0) {
        int digit = number % 10;
        System.out.println(digit * 2);
        number /= 10;
    }
    
    10, 8, 2 (in that order)
    2, 8, 10 (in that order)
    10, 4, 2 (in that order)
    2, 4, 10 (in that order)
    Answer

    10, 8, 2 (in that order)
    (Digits: 5→10, 4→8, 1→2)

  17. How many characters are printed here?
    int count = 0;
    for (char c = 'A'; c <= 'Z'; c++) {
        System.out.print(c + " ");
        count++;
    }
    System.out.println("\n" + count);
    
    26
    25
    27
    0
    Answer

    26
    (A through Z is 26 letters)

  18. What does the code print for the number 5?
    int x = 5;
    boolean prime = true;
    for (int i = 2; i < x; i++) {
        if (x % i == 0) {
            prime = false;
            break;
        }
    }
    if (prime && x > 1) {
        System.out.println("Prime");
    } else {
        System.out.println("Not Prime");
    }
    
    Prime
    Not Prime
    5
    0
    Answer

    Prime

  19. What pattern is printed?
    for (int i = 1; i <= 3; i++) {
        for (int j = 1; j <= 3; j++) {
            if (j < i) {
                System.out.print(" ");
            } else {
                System.out.print("*");
            }
        }
        System.out.println();
    }
    

    *
    *
    *

    *

    *

    * *
    * *
    * *
    (No output)
    Answer
      *
           
        *
    

    (Explanation: For row i, spaces are printed for j < i, then * for j ≥ i.)

  20. What does this code output for n=6 (printing prime factors)?
    int n = 6;
    for (int i = 2; i <= n; i++) {
        while (n % i == 0) {
            System.out.print(i + " ");
            n /= i;
        }
    }
    
    2 3
    3 2
    6
    No output
    Answer

    2 3
    (Prime factors of 6 are 2 and 3)

Back to Top

Tricky MCQs