Bird
Raised Fist0
Cprogramming~20 mins

Logical operators - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Logical Operator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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;
}
A0
B1
C3
D5
Attempts:
2 left
💡 Hint
Remember that in C, logical AND (&&) and OR (||) return 1 or 0, not the original values.
❓ Predict Output
intermediate
2: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;
}
A1
B0
C-1
DGarbage value
Attempts:
2 left
💡 Hint
Logical NOT (!) returns 1 if the operand is zero, else 0.
❓ Predict Output
advanced
2: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;
}
A1 5
B0 6
C0 5
D1 6
Attempts:
2 left
💡 Hint
Logical AND stops evaluating if the first operand is false.
❓ Predict Output
advanced
2: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;
}
A1 5
B0 5
C1 6
D0 6
Attempts:
2 left
💡 Hint
Logical OR stops evaluating if the first operand is true.
🧠 Conceptual
expert
3: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?
A0
B2
CCompiler error
D1
Attempts:
2 left
💡 Hint
Remember that && has higher precedence than || in C.

Practice

(1/5)
1. Which logical operator in C means AND and returns true only if both conditions are true?
easy
A. !
B. ||
C. &&
D. &

Solution

  1. Step 1: Understand logical AND operator

    The logical AND operator (&&) returns true only if both conditions it connects are true.
  2. Step 2: Compare with other operators

    The || operator means OR, ! means NOT, and & is bitwise AND, not logical AND.
  3. Final Answer:

    && -> Option C
  4. Quick Check:

    AND = && [OK]
Hint: AND operator is double ampersand && [OK]
Common Mistakes:
  • Confusing & (bitwise) with && (logical)
  • Mixing OR and AND operators
  • Using ! for AND
2. Which of the following is the correct syntax to negate a condition in C?
easy
A. condition !
B. !condition
C. not condition
D. condition != true

Solution

  1. Step 1: Identify negation syntax in C

    The NOT operator in C is a single exclamation mark placed before the condition: !condition.
  2. Step 2: Check other options

    condition ! places ! after condition which is invalid, C uses 'not' which is not C syntax, D is a comparison, not negation.
  3. Final Answer:

    !condition -> Option B
  4. Quick Check:

    NOT = ! before condition [OK]
Hint: Negate condition with ! before it [OK]
Common Mistakes:
  • Putting ! after condition
  • Using 'not' keyword
  • Confusing != with negation
3. What is the output of this C code?
int a = 5, b = 10;
if (a > 3 && b < 15) {
printf("True\n");
} else {
printf("False\n");
}
medium
A. No output
B. False
C. Compilation error
D. True

Solution

  1. Step 1: Evaluate first condition (a > 3)

    a is 5, which is greater than 3, so this condition is true.
  2. Step 2: Evaluate second condition (b < 15)

    b is 10, which is less than 15, so this condition is true.
  3. Step 3: Apply logical AND (&&)

    Both conditions are true, so the combined condition is true, entering the if block.
  4. Final Answer:

    True -> Option D
  5. Quick Check:

    True && True = True [OK]
Hint: Both conditions true means && condition is true [OK]
Common Mistakes:
  • Assuming OR instead of AND
  • Misreading variable values
  • Ignoring logical operator precedence
4. Find the error in this C code snippet:
int x = 0;
if (x = 5 || x == 0) {
printf("Yes\n");
}
medium
A. Using assignment '=' instead of comparison '==' in if condition
B. Missing semicolon after printf statement
C. Incorrect use of logical AND instead of OR
D. Variable x is not declared

Solution

  1. Step 1: Check the if condition syntax

    The condition uses 'x = 5 || x == 0'. The '=' is assignment, not comparison.
  2. Step 2: Understand effect of assignment in condition

    Assignment returns the assigned value (5), which is true, so condition always true, likely a bug.
  3. Final Answer:

    Using assignment '=' instead of comparison '==' in if condition -> Option A
  4. Quick Check:

    Use '==' for comparison, not '=' [OK]
Hint: Use '==' to compare, '=' assigns value [OK]
Common Mistakes:
  • Confusing '=' and '=='
  • Ignoring logical operator precedence
  • Assuming code won't compile
5. Given the code below, what will be printed?
int a = 0, b = 1, c = 2;
if (!(a || b) && c > 1) {
printf("Inside if\n");
} else {
printf("Inside else\n");
}
hard
A. Inside else
B. Inside if
C. Compilation error
D. No output

Solution

  1. Step 1: Evaluate inner OR condition (a || b)

    a is 0 (false), b is 1 (true), so a || b is true.
  2. Step 2: Apply NOT operator !(a || b)

    NOT true is false, so this part is false.
  3. Step 3: Evaluate c > 1

    c is 2, which is greater than 1, so true.
  4. Step 4: Combine with AND: false && true

    False AND anything is false, so condition is false, else block runs.
  5. Final Answer:

    Inside else -> Option A
  6. Quick Check:

    !(true) && true = false [OK]
Hint: NOT true is false; AND with false is false [OK]
Common Mistakes:
  • Ignoring NOT operator effect
  • Misreading OR condition
  • Assuming if block runs when condition false
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard