Challenge - 5 Problems
Nested Conditionals Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nested if-else with integer input
What is the output of the following C code when
num = 7?C
#include <stdio.h> int main() { int num = 7; if (num > 5) { if (num < 10) { printf("A\n"); } else { printf("B\n"); } } else { printf("C\n"); } return 0; }
Attempts:
2 left
💡 Hint
Check the conditions step by step starting from the outer if.
✗ Incorrect
The outer if checks if num > 5, which is true for 7. Then the inner if checks if num < 10, which is also true. So it prints 'A'.
❓ Predict Output
intermediate2:00remaining
Output of nested if-else with character input
What will be printed when the variable
ch = 'b' is used in this code?C
#include <stdio.h> int main() { char ch = 'b'; if (ch == 'a') { printf("Apple\n"); } else { if (ch == 'b') { printf("Banana\n"); } else { printf("Cherry\n"); } } return 0; }
Attempts:
2 left
💡 Hint
Check the first if condition and then the nested else-if.
✗ Incorrect
The first if is false because ch is 'b', not 'a'. The else block contains another if that checks if ch == 'b', which is true, so it prints 'Banana'.
🔧 Debug
advanced2:00remaining
Identify the error in nested if-else code
What error will this code produce when compiled?
C
#include <stdio.h> int main() { int x = 10; if (x > 5) if (x < 15) printf("Inside\n"); else printf("Outside\n"); return 0; }
Attempts:
2 left
💡 Hint
Remember how else matches the nearest if without braces.
✗ Incorrect
The else matches the inner if (x < 15). Since x=10, the condition is true, so it prints "Inside". No syntax or runtime error occurs.
🧠 Conceptual
advanced2:00remaining
Number of times inner block executes
Consider the code below. How many times will the inner printf("Hello\n") execute?
C
#include <stdio.h> int main() { int i; for (i = 0; i < 5; i++) { if (i % 2 == 0) { if (i > 1) { printf("Hello\n"); } } } return 0; }
Attempts:
2 left
💡 Hint
Check values of i that are even and greater than 1.
✗ Incorrect
i takes values 0,1,2,3,4. Even values are 0,2,4. Among these, i > 1 is true for 2 and 4. So the inner printf runs twice.
❓ Predict Output
expert3:00remaining
Output of nested if-else with multiple conditions
What is the output of this code snippet?
C
#include <stdio.h> int main() { int a = 3, b = 7, c = 5; if (a > b) { if (b > c) { printf("X\n"); } else { printf("Y\n"); } } else { if (a > c) { printf("Z\n"); } else { printf("W\n"); } } return 0; }
Attempts:
2 left
💡 Hint
Evaluate conditions stepwise: a > b, then b > c, then a > c.
✗ Incorrect
a=3, b=7, c=5. a > b is false, so else block runs. Then a > c is false (3 > 5 is false), so it prints "W".