0
0
C Sharp (C#)programming~20 mins

Nested loop execution in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Nested Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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 + " ");
    }
}
A1 2 3 4 5 6
B1 3 1 3 1 3
C1 2 3
D1 2 2 4 3 6
Attempts:
2 left
💡 Hint
Remember that 'break' exits the inner loop immediately when j equals 2.
Predict Output
intermediate
2: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);
A8
B16
C6
D10
Attempts:
2 left
💡 Hint
Count how many times the inner loop runs for each i.
🔧 Debug
advanced
2: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");
APrints sums nine times and then "Done" once
BCompilation error due to missing braces
CRuntime error: NullReferenceException
DNo error, prints sums and then "Done" three times
Attempts:
2 left
💡 Hint
Check how indentation and braces affect which statements belong to loops.
📝 Syntax
advanced
2: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);
}
A
for (int i = 0; i &lt; 2; i++)
    for (int j = 0; j &lt; 2; j++)
        Console.WriteLine(i + j);
B
for (int i = 0; i &lt; 2; i++) {
    for int j = 0; j &lt; 2; j++) {
        Console.WriteLine(i + j);
    }
}
C
for (int i = 0; i &lt; 2; i++) {
    for (int j = 0; j &lt; 2; j++) {
        Console.WriteLine(i + j);
    }
}
D
for (int i = 0; i &lt; 2; i++) {
    for (int j = 0; j &lt; 2; j++)
        Console.WriteLine(i + j);
}
Attempts:
2 left
💡 Hint
Check the syntax of the inner for loop declaration.
🚀 Application
expert
3: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);
A20
B25
C36
D30
Attempts:
2 left
💡 Hint
Add products only when the sum of indices is even.