Challenge - 5 Problems
Logical Operator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of combined logical AND and OR
What is the output of this C code snippet?
C
#include <stdio.h> int main() { int a = 5, b = 0, c = 3; int result = a && b || c; printf("%d", result); return 0; }
Attempts:
2 left
💡 Hint
Remember that in C, logical AND (&&) and OR (||) return 1 or 0, not the original values.
✗ Incorrect
The expression a && b evaluates to 0 because b is 0 (false). Then 0 || c evaluates to 1 because c is non-zero (true). So the final result is 1.
❓ Predict Output
intermediate2:00remaining
Logical NOT operator effect
What will this C program print?
C
#include <stdio.h> int main() { int x = 0; printf("%d", !x); return 0; }
Attempts:
2 left
💡 Hint
Logical NOT (!) returns 1 if the operand is zero, else 0.
✗ Incorrect
Since x is 0 (false), !x is 1 (true). So the program prints 1.
❓ Predict Output
advanced2:00remaining
Short-circuit evaluation in logical AND
What is the output of this C code?
C
#include <stdio.h> int main() { int a = 0, b = 5; int result = a && (++b); printf("%d %d", result, b); return 0; }
Attempts:
2 left
💡 Hint
Logical AND stops evaluating if the first operand is false.
✗ Incorrect
Since a is 0 (false), ++b is not evaluated due to short-circuit. So result is 0 and b remains 5.
❓ Predict Output
advanced2:00remaining
Short-circuit evaluation in logical OR
What will this program print?
C
#include <stdio.h> int main() { int a = 1, b = 5; int result = a || (++b); printf("%d %d", result, b); return 0; }
Attempts:
2 left
💡 Hint
Logical OR stops evaluating if the first operand is true.
✗ Incorrect
Since a is 1 (true), ++b is not evaluated due to short-circuit. So result is 1 and b remains 5.
🧠 Conceptual
expert3:00remaining
Understanding logical operator precedence
Given the expression in C: int x = 0 || 1 && 0 || 1;
What is the value of x after this line executes?
Attempts:
2 left
💡 Hint
Remember that && has higher precedence than || in C.
✗ Incorrect
The expression is evaluated as 0 || (1 && 0) || 1 = 0 || 0 || 1 = 1.