What are the three main logical operators in Java?
The three main logical operators in Java are: 1. AND (&&) 2. OR (||) 3. NOT (!)
Click to reveal answer
beginner
What does the AND (&&) operator do in Java?
The AND (&&) operator returns true only if both conditions on its sides are true. If either condition is false, it returns false.
Click to reveal answer
beginner
Explain the OR (||) operator in Java.
The 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 does the NOT (!) operator do in Java?
The NOT (!) operator reverses the value of a boolean expression. If the expression is true, NOT makes it false, and vice versa.
Click to reveal answer
intermediate
What is the difference between & and && in Java?
&& is a short-circuit AND operator: it stops checking if the first condition is false. & is a bitwise AND operator but can also be used as a logical AND without short-circuiting (checks both conditions always).
Click to reveal answer
Which logical operator returns true only if both conditions are true?
A&&
B||
C!
D&
✗ Incorrect
&& is the logical AND operator that returns true only if both sides are true.
What does the expression !(true) evaluate to?
Atrue
Bfalse
Cnull
Derror
✗ Incorrect
The NOT operator (!) reverses true to false.
Which operator returns true if at least one condition is true?
A&&
B!
C||
D^
✗ Incorrect
|| is the logical OR operator that returns true if either condition is true.
What is the result of true && false?
Atrue
Berror
Cnull
Dfalse
✗ Incorrect
AND (&&) returns false if any condition is false.
Which operator always evaluates both sides, even if the first is false?
A&
B||
C&&
D!
✗ Incorrect
& is the non-short-circuit AND operator that evaluates both sides.
Explain how the logical AND (&&) and OR (||) operators work in Java with examples.
Think about when you want both conditions to be true versus when only one needs to be true.
You got /3 concepts.
Describe the difference between the short-circuit AND (&&) and the bitwise AND (&) operators in Java.
Consider efficiency and when both conditions must be evaluated.
You got /3 concepts.
Practice
(1/5)
1. Which logical operator in Java means "both conditions must be true"?
easy
A. && (AND)
B. || (OR)
C. ! (NOT)
D. ^ (XOR)
Solution
Step 1: Understand the meaning of logical AND
The operator && means both conditions must be true for the whole expression to be true.
Step 2: Compare with other operators
|| means one or both true, ! reverses true/false, ^ means exclusive or (one true only).
Final Answer:
&& (AND) -> Option A
Quick Check:
Both true = && [OK]
Hint: AND means both true, use && [OK]
Common Mistakes:
Confusing || (OR) with && (AND)
Thinking ! means AND
Mixing up ^ with &&
2. Which of the following is the correct syntax to negate a boolean variable flag in Java?
easy
A. flag = not flag;
B. flag = !flag;
C. flag = ~flag;
D. flag = flag !=;
Solution
Step 1: Recall the NOT operator syntax
In Java, the NOT operator is ! and placed before the boolean variable to reverse its value.
Step 2: Check each option
flag = !flag; uses ! correctly. flag = not flag; uses 'not' which is invalid in Java. flag = ~flag; uses ~ which is bitwise NOT, not for booleans. flag = flag !=; is invalid syntax.
Final Answer:
flag = !flag; -> Option B
Quick Check:
Negate boolean with ! [OK]
Hint: Use ! before boolean to negate it [OK]
Common Mistakes:
Using 'not' instead of !
Using bitwise ~ on booleans
Incorrect syntax like 'flag !=;'
3. What is the output of the following Java code?
boolean a = true; boolean b = false; System.out.println(a && b || !b);
medium
A. false
B. Compilation error
C. true
D. Runtime exception
Solution
Step 1: Evaluate the expression step-by-step
a && b is true && false = false. !b is !false = true.
Step 2: Apply OR operator
Now false || true = true.
Final Answer:
true -> Option C
Quick Check:
(true && false) || !false = true [OK]
Hint: Evaluate && first, then ||, then ! [OK]
Common Mistakes:
Ignoring operator precedence
Evaluating || before &&
Confusing !b value
4. Identify the error in this Java code snippet:
boolean x = true; boolean y = false; if (x & | y) { System.out.println("Yes"); }
medium
A. Invalid use of & | operators together
B. Missing semicolon after if condition
C. Boolean variables cannot be used in if
D. No error, code runs fine
Solution
Step 1: Check the if condition syntax
The condition uses '& |' which is not a valid operator combination in Java.
Step 2: Understand valid logical operators
Valid operators are &&, ||, &, | but not combined as '& |'. This causes a syntax error.
Final Answer:
Invalid use of & | operators together -> Option A
Quick Check:
Operators & and | cannot be combined like '& |' [OK]
Hint: Use && or ||, not mixed & | [OK]
Common Mistakes:
Confusing bitwise & with logical &&
Combining & and | without operand
Thinking no error occurs
5. You want to check if a number n is between 10 and 20 inclusive in Java. 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, both conditions n >= 10 and n <= 20 must be true.
Step 2: Analyze each option
if (n >= 10 || n <= 20) uses || which means either condition true, so it allows numbers outside the range. if (n >= 10 && n <= 20) uses && which correctly requires both conditions true. if (!(n < 10 && n > 20)) negates an impossible condition and is incorrect. if (n > 10 && n < 20) excludes 10 and 20 by using > and <.
Final Answer:
if (n >= 10 && n <= 20) -> Option D
Quick Check:
Both limits true = && [OK]
Hint: Use && to require both conditions true [OK]
Common Mistakes:
Using || instead of && for range
Excluding boundary values by using > or <
Misusing negation for range check
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.