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

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Nested Conditional Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of nested if-else with integer input

What is the output of the following C# code?

C Sharp (C#)
int x = 7;
if (x > 5)
{
    if (x < 10)
    {
        Console.WriteLine("A");
    }
    else
    {
        Console.WriteLine("B");
    }
}
else
{
    Console.WriteLine("C");
}
ANo output
BB
CA
DC
Attempts:
2 left
💡 Hint

Check the conditions step by step starting from the outer if.

Predict Output
intermediate
2:00remaining
Nested if-else with string comparison

What will be printed when this code runs?

C Sharp (C#)
string color = "red";
if (color == "blue")
{
    Console.WriteLine("Blue selected");
}
else
{
    if (color == "red")
    {
        Console.WriteLine("Red selected");
    }
    else
    {
        Console.WriteLine("Other color");
    }
}
ARed selected
BBlue selected
COther color
DNo output
Attempts:
2 left
💡 Hint

Check the value of the variable and the conditions carefully.

🔧 Debug
advanced
2:00remaining
Identify the error in nested if-else code

What error will this C# code produce when compiled?

C Sharp (C#)
int n = 3;
if (n > 0)
    if (n < 5)
        Console.WriteLine("In range");
    else
        Console.WriteLine("Out of range");
else
    Console.WriteLine("Negative");
else
    Console.WriteLine("Zero");
ANo error, prints "In range"
BSyntax error: else without matching if
CRuntime error: ambiguous else
DSyntax error: missing braces
Attempts:
2 left
💡 Hint

Look carefully at the placement of else statements and braces.

🚀 Application
advanced
2:00remaining
Determine output of nested if with multiple conditions

What is the output of this code snippet?

C Sharp (C#)
int score = 85;
if (score >= 90)
{
    Console.WriteLine("Grade A");
}
else
{
    if (score >= 80)
    {
        Console.WriteLine("Grade B");
    }
    else
    {
        Console.WriteLine("Grade C");
    }
}
AGrade A
BNo output
CGrade C
DGrade B
Attempts:
2 left
💡 Hint

Check the score against each condition carefully.

🧠 Conceptual
expert
2:00remaining
Understanding nested if-else flow

Consider this nested if-else structure:

if (A)
{
    if (B)
    {
        Action1;
    }
    else
    {
        Action2;
    }
}
else
{
    Action3;
}

Which statement is true about when Action2 executes?

AWhen A is true and B is false
BWhen A is false and B is true
CWhen A is false and B is false
DWhen B is true regardless of A
Attempts:
2 left
💡 Hint

Trace the conditions that lead to the else inside the first if.