Code Snippets-based MCQs
Nested Loops
Table of contents
Easy Level MCQs - Nested Loops
Below are 30 simple questions about nested loops in Java:
- 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); } }
Answer
i=0, j=0; i=0, j=1; i=1, j=0; i=1, j=1
- 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);
Answer
6
(Loop runs 2 times in the outer loop and 3 times in the inner loop: 2 * 3 = 6) - 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);
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) - 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 + " "); } }
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) - 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);
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) - 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) + " "); } }
Answer
4 3 5 4
(When i=1: j=3 → 4, j=2 → 3; when i=2: j=3 → 5, j=2 → 4) - 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);
Answer
6 stars, then 6 as count
(Because 2 (outer loop) × 3 (inner loop) = 6 total iterations) - 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 "); } } }
Answer
O X O X
(When i=0: j=0 → O, j=1 → X; when i=1: j=0 → O, j=1 → X) - 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);
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) - 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 + " "); } }
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) - 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);
Answer
4 times
(Explanation: Outer loop runs 2 times; inner loop runs 2 times for each outer iteration. Total = 2 × 2 = 4) - 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 + " "); } }
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) - 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);
Answer
6
(Outer loop: 3 iterations; inner loop: 2 iterations per outer loop. 3 × 2 = 6) - 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(); }
Answer
X X X
(Explanation: For i=0: j=0 to 1 → prints “X X”. For i=1: j=1 → prints “X”.)
- 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);
Answer
3
(Explanation: i=0 → inner loop runs 1 time; i=1 → inner loop runs 2 times. Total = 1+2 = 3) - 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(); }
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)
- 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);
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 - 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(); }
Answer
0,0 0,1 1,0 1,1 2,0 2,1
- 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);
Answer
3
(Checks when i == j for i,j in {1,2,3}, which happens at (1,1), (2,2), (3,3)) - 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(); }
Answer
*
- 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(); }
Answer
2 1 3 2
(When i=0, j=2→2, j=1→1; When i=1, j=2→3, j=1→2)
- 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(); }
Answer
### ## #
- What is printed?
int i = 0; while (i < 2) { int j = 0; while (j < 2) { System.out.print(i + j + " "); j++; } i++; }
Answer
0 1 1 2
(Explanation: i=0 → j=0→0, j=1→1; i=1 → j=0→1, j=1→2) - 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);
Answer
4
(2 × 2 = 4 total prints in nested loops) - 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(); }
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’s2 3
on the first line,4
on the second line. - 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);
Answer
6
(Inner loopj
runs 3 times: j=0,1,2 → sum of j = 3. Outer loop runs 2 times, total sum = 3+3 = 6) - What gets printed here?
for (int i = 2; i > 0; i--) { for (int j = 0; j < i; j++) { System.out.print("*"); } System.out.println(); }
Answer
*
- 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);
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.) - 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(); }
Answer
2 1 3 2
(Explanation: i=0 → j=2..1 => sums=2,1; i=1 → j=2..1 => sums=3,2)
- 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(); }
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.
- 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);
Answer
6
(Explanation: Digits are 3, 2, and 1. Sum = 3 + 2 + 1 = 6) - 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 + " "); } }
Answer
4 primes
(They are 2, 3, 5, 7) - 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);
Answer
24
(Explanation: 4! = 4 × 3 × 2 × 1 = 24) - 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; }
Answer
0 1 1 2 3
(First five Fibonacci terms starting with 0, 1) - 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);
Answer
32
(Leading zeros are dropped in integer operations) - 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; }
Answer
1 time
(Digits are 9 (odd), 2 (even), 1 (odd). Only2
triggers the print) - 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(); }
Answer
A A B A B C
- 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"); }
Answer
Palindrome
- 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 + " "); } }
Answer
8
(The primes up to 20 are 2,3,5,7,11,13,17,19) - 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(); }
Answer
1 12 123
- 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);
Answer
3
(Digits are 7, 0, 4 in the loop) - What does this code snippet print?
int num = 999; int sum = 0; for (; num > 0; num /= 10) { sum += (num % 10); } System.out.println(sum);
Answer
27
(9 + 9 + 9 = 27) - What is the outpu?
for (int i = 1; i <= 10; i++) { if (i % 3 == 0) { System.out.print(i + " "); } }
Answer
3 6 9
- 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 + " "); } }
Answer
11 13
(8, 9, 10, 12 are not prime) - 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(); }
Answer
*** ** *
- Which lines will be printed?
int number = 145; while (number > 0) { int digit = number % 10; System.out.println(digit * 2); number /= 10; }
Answer
10, 8, 2 (in that order)
(Digits: 5→10, 4→8, 1→2) - 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);
Answer
26
(A through Z is 26 letters) - 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"); }
Answer
Prime
- 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(); }
Answer
* *
(Explanation: For row i, spaces are printed for j < i, then
*
for j ≥ i.) - 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; } }
Answer
2 3
(Prime factors of 6 are 2 and 3)