Bird
Raised Fist0
C Sharp (C#)programming~20 mins

Logical operators in C Sharp (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 snippet?
C Sharp (C#)
bool a = true;
bool b = false;
bool c = true;
Console.WriteLine(a && b || c);
ATrue
BFalse
CCompilation error
DRuntime exception
Attempts:
2 left
💡 Hint
Remember the precedence of && and || operators in C#.
❓ Predict Output
intermediate
2:00remaining
Result of negation and OR operator
What will be printed by this C# code?
C Sharp (C#)
bool x = false;
bool y = true;
Console.WriteLine(!x || y);
AFalse
BRuntime exception
CCompilation error
DTrue
Attempts:
2 left
💡 Hint
Check how the NOT operator affects the value of x.
❓ Predict Output
advanced
2:00remaining
Output with short-circuit evaluation
What is the output of this C# code snippet?
C Sharp (C#)
int i = 0;
bool result = false && (++i > 0);
Console.WriteLine(i);
ACompilation error
B1
C0
DRuntime exception
Attempts:
2 left
💡 Hint
Consider how the && operator short-circuits evaluation.
❓ Predict Output
advanced
2:00remaining
Value of variable after complex logical expression
What is the value of variable flag after running this code?
C Sharp (C#)
bool flag = true;
flag = flag && false || !flag && true;
Console.WriteLine(flag);
ACompilation error
BFalse
CTrue
DRuntime exception
Attempts:
2 left
💡 Hint
Break down the expression into parts and evaluate step by step.
🧠 Conceptual
expert
2:00remaining
Understanding logical operator precedence and evaluation
Given the expression true || false && false in C#, which statement is correct about its evaluation?
AThe expression evaluates to true because && has higher precedence than ||, so false && false is evaluated first.
BThe expression evaluates to false because || has higher precedence than &&, so true || false is evaluated first.
CThe expression causes a runtime error due to ambiguous operator precedence.
DThe expression evaluates to false because both operators have the same precedence and are evaluated left to right.
Attempts:
2 left
💡 Hint
Recall operator precedence rules in C# for logical operators.

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 the meaning of logical AND

    The AND operator returns true only if both conditions it connects are true.
  2. Step 2: Identify the C# symbol for AND

    In C#, the logical AND operator is written as &&, which checks both sides for true.
  3. Final Answer:

    && -> Option C
  4. Quick Check:

    AND operator = && [OK]
Hint: AND is &&, OR is ||, NOT is ! [OK]
Common Mistakes:
  • Confusing & (bitwise AND) with && (logical AND)
  • Mixing OR (||) with AND (&&)
  • Using ! as AND operator
2. Which of the following is the correct syntax to negate a boolean variable isActive in C#?
easy
A. isActive = !!isActive;
B. isActive = !isActive;
C. isActive = ~isActive;
D. isActive = isActive!;

Solution

  1. Step 1: Understand the NOT operator syntax

    The NOT operator in C# is ! and it negates a boolean value.
  2. Step 2: Check the correct usage

    Using ! before the variable negates it, so isActive = !isActive; is correct.
  3. Final Answer:

    isActive = !isActive; -> Option B
  4. Quick Check:

    NOT operator = ! [OK]
Hint: Use ! before variable to negate boolean [OK]
Common Mistakes:
  • Using double !! which is invalid in C#
  • Using bitwise NOT ~ on booleans
  • Placing ! after the variable
3. What is the output of this C# code?
bool a = true;
bool b = false;
Console.WriteLine(a || b && !a);
medium
A. True
B. False
C. Compilation error
D. Null

Solution

  1. Step 1: Evaluate the expression inside WriteLine

    Operator precedence: ! has highest, then &&, then ||.
    Calculate !a = !true = false.
    Then b && !a = false && false = false.
    Finally a || (b && !a) = true || false = true.
  2. Step 2: Determine output

    The expression evaluates to true, so Console.WriteLine prints True.
  3. Final Answer:

    True -> Option A
  4. Quick Check:

    Operator precedence: ! > && > ||, result = True [OK]
Hint: Remember ! > && > || precedence [OK]
Common Mistakes:
  • Ignoring operator precedence
  • Evaluating && before !
  • Assuming output is false due to b being false
4. Find the error in this C# code snippet:
bool x = true;
bool y = false;
if (x || && y)
{
Console.WriteLine("Yes");
}
medium
A. No error, code is correct
B. Incorrect use of || operator
C. Missing parentheses around conditions
D. Missing variable before && operator

Solution

  1. Step 1: Analyze the if condition syntax

    The condition is written as x || && y, which is invalid because && needs two operands.
  2. Step 2: Identify the missing operand

    Between || and && operators, there is no variable or expression before &&, causing syntax error.
  3. Final Answer:

    Missing variable before && operator -> Option D
  4. Quick Check:

    Logical operators need operands on both sides [OK]
Hint: Each operator needs two conditions [OK]
Common Mistakes:
  • Writing operators without operands
  • Assuming || and && can be chained without variables
  • Missing parentheses but that is not the main error here
5. You want to check if a number n is between 10 and 20 inclusive in C#. Which condition correctly uses logical operators?
hard
A. if (n >= 10 && n <= 20)
B. if (n >= 10 || n <= 20)
C. if (n > 10 && n < 20)
D. if (!(n < 10 && n > 20))

Solution

  1. Step 1: Understand the range condition

    To check if n is between 10 and 20 inclusive, n must be greater or equal to 10 AND less or equal to 20.
  2. Step 2: Evaluate each option

    if (n > 10 && n < 20) excludes 10 and 20 because it uses > and <.
    if (n >= 10 || n <= 20) uses OR, which is true for almost all numbers.
    if (n >= 10 && n <= 20) correctly uses >= and <= with AND.
    if (!(n < 10 && n > 20)) negates an impossible condition and is incorrect.
  3. Final Answer:

    if (n >= 10 && n <= 20) -> Option A
  4. Quick Check:

    Inclusive range needs >= and <= with AND [OK]
Hint: Use && with >= and <= for inclusive range [OK]
Common Mistakes:
  • Using || instead of && for range checks
  • Using > and < instead of >= and <=
  • Negating wrong conditions
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