Challenge - 5 Problems
Nested Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nested loops with break
What is the output of the following C# code?
C Sharp (C#)
for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { if (j == 2) break; Console.Write(i * j + " "); } }
Attempts:
2 left
💡 Hint
Remember that 'break' exits the inner loop immediately when j equals 2.
✗ Incorrect
The inner loop breaks when j == 2, so only j == 1 runs each time, printing i*1 (1, then 2, then 3).
❓ Predict Output
intermediate2:00remaining
Counting iterations in nested loops
How many times will the innermost statement execute in this code?
C Sharp (C#)
int count = 0; for (int i = 0; i < 4; i++) { for (int j = i; j < 4; j++) { count++; } } Console.WriteLine(count);
Attempts:
2 left
💡 Hint
Count how many times the inner loop runs for each i.
✗ Incorrect
For i=0, j runs 4 times; i=1, j runs 3 times; i=2, j runs 2 times; i=3, j runs 1 time. Total = 4+3+2+1=10.
🔧 Debug
advanced2:00remaining
Identify the error in nested loops
What error will this code produce when compiled or run?
C Sharp (C#)
for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) Console.WriteLine(i + j); Console.WriteLine("Done");
Attempts:
2 left
💡 Hint
Check how indentation and braces affect which statements belong to loops.
✗ Incorrect
The outer for loop body is the inner for statement (no braces needed for single statement). Inner for body is the WriteLine(i+j). "Done" executes once after both loops. Prints 9 sums then "Done".
📝 Syntax
advanced2:00remaining
Find the syntax error in nested loops
Which option contains the syntax error in nested loops?
C Sharp (C#)
for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) Console.WriteLine(i + j); }
Attempts:
2 left
💡 Hint
Check the syntax of the inner for loop declaration.
✗ Incorrect
Option B misses the opening parenthesis '(' after 'for' in the inner loop: 'for int j' instead of 'for (int j'.
🚀 Application
expert3:00remaining
Calculate sum of products in nested loops
What is the value of 'total' after running this code?
C Sharp (C#)
int total = 0; for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { if ((i + j) % 2 == 0) { total += i * j; } } } Console.WriteLine(total);
Attempts:
2 left
💡 Hint
Add products only when the sum of indices is even.
✗ Incorrect
Pairs where (i+j) even: (1,1):1*1=1, (1,3):1*3=3, (2,2):2*2=4, (3,1):3*1=3, (3,3):3*3=9. Sum=1+3+4+3+9=20.