0
0
Cprogramming~20 mins

Nested conditional statements - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Nested Conditionals 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 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;
}
AB
BC
CA
DNo output
Attempts:
2 left
💡 Hint
Check the conditions step by step starting from the outer if.
Predict Output
intermediate
2: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;
}
ABanana
BCherry
CApple
DNo output
Attempts:
2 left
💡 Hint
Check the first if condition and then the nested else-if.
🔧 Debug
advanced
2: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;
}
ASyntax error due to missing braces
BRuntime error
CNo error, prints "Outside"
DNo error, prints "Inside"
Attempts:
2 left
💡 Hint
Remember how else matches the nearest if without braces.
🧠 Conceptual
advanced
2: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;
}
A4
B2
C3
D1
Attempts:
2 left
💡 Hint
Check values of i that are even and greater than 1.
Predict Output
expert
3: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;
}
AW
BZ
CX
DY
Attempts:
2 left
💡 Hint
Evaluate conditions stepwise: a > b, then b > c, then a > c.