Bird
Raised Fist0
C++programming~20 mins

Logical operators in C++ - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Logical Operators Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of combined logical operators
What is the output of the following C++ code?
C++
#include <iostream>
int main() {
    bool a = true, b = false, c = true;
    std::cout << (a && b || c) << std::endl;
    return 0;
}
A0
B1
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint
Remember the precedence of && and || operators.
❓ Predict Output
intermediate
2:00remaining
Logical NOT operator effect
What will be printed by this C++ program?
C++
#include <iostream>
int main() {
    bool x = false;
    std::cout << (!x && x) << std::endl;
    return 0;
}
A1
BRuntime error
CCompilation error
D0
Attempts:
2 left
💡 Hint
Check how !x and x combine with &&.
❓ Predict Output
advanced
2:00remaining
Short-circuit evaluation in logical AND
What is the output of this C++ code snippet?
C++
#include <iostream>
int main() {
    int i = 0;
    bool result = (false && (++i > 0));
    std::cout << i << std::endl;
    return 0;
}
A0
B1
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint
Does ++i get executed when the first operand is false in &&?
❓ Predict Output
advanced
2:00remaining
Logical OR with side effects
What will be the output of this C++ program?
C++
#include <iostream>
int main() {
    int x = 5;
    bool val = (x > 10 || ++x > 5);
    std::cout << x << std::endl;
    return 0;
}
A6
BCompilation error
C5
DRuntime error
Attempts:
2 left
💡 Hint
Check if ++x is evaluated when x > 10 is false.
🧠 Conceptual
expert
2:00remaining
Understanding logical operator precedence
Given the expression: true || false && false, which part is evaluated first in C++?
Atrue || false && false (evaluated left to right)
Btrue || false
Cfalse && false
DThe expression causes a syntax error
Attempts:
2 left
💡 Hint
Remember operator precedence: && has higher precedence than ||.

Practice

(1/5)
1. Which logical operator in C++ means "both conditions must be true"?
easy
A. &&
B. ||
C. !
D. &

Solution

  1. Step 1: Understand the meaning of logical AND

    The operator that requires both conditions to be true is called logical AND.
  2. Step 2: Identify the C++ symbol for logical AND

    In C++, logical AND is represented by &&.
  3. Final Answer:

    && -> Option A
  4. Quick Check:

    Both true = true means && [OK]
Hint: && means both true; || means one true [OK]
Common Mistakes:
  • Confusing & (bitwise AND) with && (logical AND)
  • Thinking || means both true
  • Mixing ! with AND or OR
2. Which of the following is the correct syntax to negate a boolean variable flag in C++?
easy
A. !flag = flag;
B. flag = flag!;
C. flag != flag;
D. flag = !flag;

Solution

  1. Step 1: Recall the negation operator syntax

    The negation operator ! is placed before the variable to flip its value.
  2. Step 2: Check correct assignment syntax

    To update flag to its negation, write flag = !flag; which is valid C++ syntax.
  3. Final Answer:

    flag = !flag; -> Option D
  4. Quick Check:

    Negate with ! before variable [OK]
Hint: Put ! before variable to negate [OK]
Common Mistakes:
  • Placing ! after variable (invalid syntax)
  • Using != which means not equal
  • Trying to assign to !flag (not a variable)
3. What is the output of this C++ code?
bool a = true;
bool b = false;
if (a || b) {
std::cout << "Yes";
} else {
std::cout << "No";
}
medium
A. Yes
B. No
C. true
D. false

Solution

  1. Step 1: Evaluate the logical OR condition

    a is true, b is false, so a || b is true because at least one is true.
  2. Step 2: Determine which branch runs

    Since the condition is true, the if block runs and prints "Yes".
  3. Final Answer:

    Yes -> Option A
  4. Quick Check:

    true || false = true [OK]
Hint: || is true if any side is true [OK]
Common Mistakes:
  • Thinking both must be true for ||
  • Confusing output with boolean literals
  • Ignoring that std::cout prints strings
4. Find the error in this C++ code snippet:
bool x = true;
bool y = false;
if (x & y) {
std::cout << "True";
} else {
std::cout << "False";
}
medium
A. Incorrect use of std::cout
B. Missing semicolon after bool declarations
C. Using & instead of && in if condition
D. Variables x and y not initialized

Solution

  1. Step 1: Identify the operator used in condition

    The code uses & which is bitwise AND, not logical AND.
  2. Step 2: Explain why && is needed

    For boolean logic in conditions, && should be used to combine true/false values.
  3. Final Answer:

    Using & instead of && in if condition -> Option C
  4. Quick Check:

    Logical AND needs && not & [OK]
Hint: Use && for logic, & is bitwise [OK]
Common Mistakes:
  • Confusing bitwise & with logical &&
  • Assuming & works like && in conditions
  • Ignoring operator precedence differences
5. Given int a = 5; and int b = 10;, which condition correctly checks if a is less than 10 AND b is greater than 5?
hard
A. if (a < 10 || b > 5)
B. if (a < 10 && b > 5)
C. if (!(a < 10) && !(b > 5))
D. if (a > 10 && b < 5)

Solution

  1. Step 1: Understand the condition requirements

    We want both: a less than 10 AND b greater than 5.
  2. Step 2: Match condition with logical AND

    Logical AND (&&) combines both conditions to be true simultaneously.
  3. Final Answer:

    if (a < 10 && b > 5) -> Option B
  4. Quick Check:

    Both true means && [OK]
Hint: Use && to require both conditions true [OK]
Common Mistakes:
  • Using || which means either condition true
  • Negating conditions incorrectly
  • Swapping comparison operators
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