Challenge - 5 Problems
If–else Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nested if–else
What is the output of this C code snippet?
C
int main() { int x = 5, y = 10; if (x > y) { printf("A\n"); } else if (x == y) { printf("B\n"); } else { printf("C\n"); } return 0; }
Attempts:
2 left
💡 Hint
Compare the values of x and y carefully.
✗ Incorrect
Since x (5) is less than y (10), the else block runs, printing 'C'.
❓ Predict Output
intermediate2:00remaining
If–else with logical operators
What will this program print?
C
int main() { int a = 3, b = 7; if (a > 0 && b < 5) { printf("X\n"); } else { printf("Y\n"); } return 0; }
Attempts:
2 left
💡 Hint
Check both conditions in the if statement.
✗ Incorrect
a > 0 is true but b < 5 is false (b=7), so the if condition is false and else runs.
❓ Predict Output
advanced2:00remaining
If–else with assignment inside condition
What is the output of this code?
C
int main() { int x = 0; if (x = 5) { printf("Hello\n"); } else { printf("World\n"); } return 0; }
Attempts:
2 left
💡 Hint
Remember that assignment inside if condition returns the assigned value.
✗ Incorrect
The assignment x = 5 returns 5, which is true, so the if block runs printing 'Hello'.
❓ Predict Output
advanced2:00remaining
If–else with multiple conditions and else
What will this program print?
C
int main() { int n = 0; if (n > 0) { printf("Positive\n"); } else if (n < 0) { printf("Negative\n"); } else { printf("Zero\n"); } return 0; }
Attempts:
2 left
💡 Hint
Check the value of n and which condition matches.
✗ Incorrect
Since n is 0, neither n > 0 nor n < 0 is true, so else runs printing 'Zero'.
❓ Predict Output
expert2:00remaining
If–else with uninitialized variable
What error or output does this code produce?
C
int main() { int x; if (x > 0) { printf("Positive\n"); } else { printf("Non-positive\n"); } return 0; }
Attempts:
2 left
💡 Hint
Consider what happens when you use a variable without setting it first.
✗ Incorrect
Variable x is uninitialized, so its value is garbage. Using it in condition causes undefined behavior.