C Conditional Statements - Tricky MCQ

Previous C Conditional Statements (if, if-else, else-if, nested if) Next

Tricky Questions on Conditional Statements

1

What is the output of: if (0.1) printf("True"); else printf("False");

Correct Answer: A) True

In C, any non-zero value is considered "true" in a conditional context. 0.1 is a non-zero floating-point value, so it evaluates to true. This applies to all numeric types, not just integers.

if (0.1) // Evaluates to true (non-zero) if (0.0) // Evaluates to false (zero) if (-0.1) // Evaluates to true (non-zero)
2

What is the issue with: if (x = 5) { ... }

Correct Answer: D) All of the above

This is a classic C pitfall. = is assignment, not comparison (==). The expression x = 5 assigns 5 to x and returns 5, which is non-zero (true). This changes x unexpectedly and the if-block always executes. Some compilers warn about this (use -Wall flag).

// Wrong: assigns 5 to x, always true if (x = 5) { ... } // Correct: compares x with 5 if (x == 5) { ... } // Intentional assignment in condition (valid but confusing) if ((x = get_value()) != 0) { ... }
3

What is dangling else problem in C?

Correct Answer: B) Ambiguity in nested if-else without braces

The "dangling else" problem occurs when there are nested if statements without else blocks, making it ambiguous which if an else belongs to. C resolves this by associating else with the nearest unmatched if. Always use braces to avoid ambiguity.

// Dangling else problem: if (x > 0) if (y > 0) printf("Both positive"); else printf("What does this else belong to?"); // The else belongs to if(y > 0), not if(x > 0) // Clear with braces: if (x > 0) { if (y > 0) { printf("Both positive"); } } else { printf("x is not positive"); }
4

What is the output: int x = 1; if (x) { x = 0; } else if (x = 0) { x = 2; } printf("%d", x);

Correct Answer: A) 0

Initially x=1, so the first if condition (x) is true (non-zero). It executes x=0. The else-if is not evaluated because the first if was true. So printf prints 0. Note: else if (x = 0) contains an assignment (=), not comparison (==), which would assign 0 to x and evaluate to 0 (false).

int x = 1; if (x) { // true (1), executes x = 0; // x becomes 0 } else if (x = 0) { // NOT evaluated because first if was true x = 2; // Never reached } printf("%d", x); // Output: 0
5

Which is NOT equivalent to if(x) { ... }?

Correct Answer: B) if (x == 1)

if(x) checks if x is non-zero. if(x == 1) only checks if x equals exactly 1. These are different: if x=2, if(x) is true but if(x == 1) is false. if(!!x) is equivalent (double negation converts to 0 or 1). For pointers, if(x != NULL) is equivalent to if(x).

int x = 2; if (x) // true (non-zero) if (x == 1) // false (x is 2, not 1) if (!!x) // true (!!2 → !0 → 1) if (x != 0) // true int *ptr = malloc(10); if (ptr) // true if allocation succeeded if (ptr != NULL) // equivalent
6

What is "short-circuit evaluation" in C conditionals?

Correct Answer: D) All of the above

Short-circuit evaluation means logical operators (&& and ||) evaluate only until the result is determined. For &&: if left operand is false, right isn't evaluated (result is false). For ||: if left operand is true, right isn't evaluated (result is true). This is useful for checking preconditions like if (ptr != NULL && ptr->value > 0).

7

What is the output: int x = 0; if (x = 1, x == 0) printf("A"); else printf("B");

Correct Answer: B) B

The comma operator evaluates expressions left-to-right and returns the value of the rightmost expression. Here: (x = 1, x == 0) first assigns 1 to x, then evaluates x == 0 which is false (1 == 0). So the if condition is false, else executes, printing "B".

8

What is the difference between else-if ladder and nested if-else?

Correct Answer: D) Both A and B are correct

An else-if ladder checks conditions sequentially until one is true, then skips the rest. Nested if-else creates a hierarchy where inner conditions are checked only if outer conditions are true. They have different control flow and performance implications.

9

What is the output: int a = 5, b = 10; if (a = b) printf("Equal"); else printf("Not Equal");

Correct Answer: A) Equal

This is another assignment-in-condition pitfall. a = b assigns b's value (10) to a, and the expression evaluates to 10 (non-zero, true). So "Equal" prints. Both a and b become 10. To compare, use a == b.

10

Which statement about switch is NOT true compared to if-else ladder?

Correct Answer: C) switch cases can have complex conditions

switch cases can only have constant integral expressions, not complex conditions. if-else can handle any boolean expression. switch is limited to char, int, enum (and in C99, bool). Compilers can optimize switch with jump tables for dense cases. Fall-through (executing multiple cases) happens without break.

11

What is "if-else expression equivalence" using ternary operator?

Correct Answer: D) All of the above

The ternary operator ? : is an expression (returns a value) while if-else is a statement (doesn't return a value). This allows ternary to be used in places where if-else can't: initializations, function arguments, return statements, etc. Both expr1 and expr2 must be of compatible types.

12

What happens in: if (x) { } else { } with no code in blocks?

Correct Answer: D) Both B and C

Empty if-else blocks are syntactically valid but useless. With optimization enabled, the compiler will likely remove them entirely since they have no effect. However, the condition x is still evaluated (possible side effects if x is a volatile variable or function call).

13

What is "conditional inclusion" using #if, #ifdef?

Correct Answer: D) All of the above

Preprocessor conditional directives (#if, #ifdef, #ifndef, #elif, #else, #endif) make compile-time decisions about what code to include. They're evaluated before compilation, unlike runtime if statements. Used for platform-specific code, debug builds, feature flags.

14

What is the output: int x = 0; if (++x || ++x) printf("%d", x);

Correct Answer: A) 1 (short-circuit evaluation)

|| uses short-circuit evaluation. ++x increments x to 1 and returns 1 (true). Since the left operand of || is true, the right operand (++x) is NOT evaluated. So x is incremented only once to 1, and the condition is true. If it were &&, both would be evaluated.

15

What is "conditional operator precedence" issue?

Correct Answer: D) All of the above

The ternary operator ?: has very low precedence (just above assignment operators). This often leads to unexpected behavior without parentheses. The expression binds more tightly to operators on the right than on the left. Always use parentheses for clarity in complex expressions.

Previous C Conditional Statements (if, if-else, else-if, nested if) Next