The logical AND operator (&&) returns true only if both conditions on its sides are true. If either condition is false, the result is false.
Click to reveal answer
beginner
Explain the logical OR operator (||) in C#.
The logical OR operator (||) returns true if at least one of the conditions is true. It returns false only if both conditions are false.
Click to reveal answer
beginner
What is the purpose of the logical NOT operator (!) in C#?
The logical NOT operator (!) reverses the value of a boolean expression. If the expression is true, ! makes it false, and vice versa.
Click to reveal answer
intermediate
How does short-circuit evaluation work with && and || operators?
In C#, && and || use short-circuit evaluation. For &&, if the first condition is false, the second is not checked because the result is already false. For ||, if the first condition is true, the second is not checked because the result is already true.
Click to reveal answer
intermediate
What is the difference between & and && in C#?
The & operator is a logical AND that always evaluates both sides, while && is a conditional AND that stops evaluating if the first side is false (short-circuit). & can also be used as a bitwise AND.
Click to reveal answer
What will the expression (true && false) evaluate to in C#?
Atrue
Berror
Cnull
Dfalse
✗ Incorrect
The && operator returns true only if both sides are true. Here, one side is false, so the result is false.
Which operator reverses the boolean value of an expression?
A!
B||
C&&
D&
✗ Incorrect
The ! operator is the logical NOT operator that reverses true to false and false to true.
What does the expression (false || true) return?
Afalse
Btrue
Cnull
Derror
✗ Incorrect
The || operator returns true if at least one side is true. Here, the second side is true, so the result is true.
Which statement about short-circuit evaluation is true?
A&& always evaluates both sides
B|| always evaluates both sides
C&& stops evaluating if the first side is false
D! stops evaluating if the expression is true
✗ Incorrect
&& uses short-circuit evaluation and stops if the first condition is false because the whole expression cannot be true.
What is the difference between & and && in C#?
A& is bitwise AND, && is logical AND with short-circuit
B& is logical OR, && is logical AND
C& and && are the same
D& is logical NOT, && is logical OR
✗ Incorrect
& can be bitwise AND or logical AND without short-circuit; && is logical AND with short-circuit evaluation.
Describe how the logical AND (&&) and OR (||) operators work in C#.
Think about when the evaluation stops early.
You got /3 concepts.
Explain the difference between & and && operators in C#.
Consider evaluation and bitwise use.
You got /3 concepts.
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
Step 1: Understand the meaning of logical AND
The AND operator returns true only if both conditions it connects are true.
Step 2: Identify the C# symbol for AND
In C#, the logical AND operator is written as &&, which checks both sides for true.
Final Answer:
&& -> Option C
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
Step 1: Understand the NOT operator syntax
The NOT operator in C# is ! and it negates a boolean value.
Step 2: Check the correct usage
Using ! before the variable negates it, so isActive = !isActive; is correct.
Final Answer:
isActive = !isActive; -> Option B
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
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.
Step 2: Determine output
The expression evaluates to true, so Console.WriteLine prints True.
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
Step 1: Analyze the if condition syntax
The condition is written as x || && y, which is invalid because && needs two operands.
Step 2: Identify the missing operand
Between || and && operators, there is no variable or expression before &&, causing syntax error.
Final Answer:
Missing variable before && operator -> Option D
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
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.
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.
Final Answer:
if (n >= 10 && n <= 20) -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.