Bird
Raised Fist0
Javaprogramming~20 mins

Logical operators in Java - 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 Java code snippet?
Java
public class Test {
    public static void main(String[] args) {
        boolean a = true;
        boolean b = false;
        boolean c = true;
        if (a && b || c) {
            System.out.println("YES");
        } else {
            System.out.println("NO");
        }
    }
}
ACompilation error
BNO
CYES
DRuntime exception
Attempts:
2 left
💡 Hint
Remember the precedence of && and || operators in Java.
❓ Predict Output
intermediate
2:00remaining
Logical operator short-circuit behavior
What will be the output of this Java program?
Java
public class Test {
    public static void main(String[] args) {
        int x = 0;
        if (false && (++x > 0)) {
            System.out.println("Inside if");
        }
        System.out.println(x);
    }
}
A1
BRuntime exception
CCompilation error
D0
Attempts:
2 left
💡 Hint
Consider if the increment happens when the first condition is false in &&.
❓ Predict Output
advanced
2:00remaining
Output with mixed logical operators and precedence
What is the output of this Java code?
Java
public class Test {
    public static void main(String[] args) {
        boolean a = false;
        boolean b = true;
        boolean c = false;
        if (a || b && c) {
            System.out.println("TRUE");
        } else {
            System.out.println("FALSE");
        }
    }
}
AFALSE
BTRUE
CCompilation error
DRuntime exception
Attempts:
2 left
💡 Hint
Remember that && has higher precedence than ||.
❓ Predict Output
advanced
2:00remaining
Logical NOT operator effect
What will be printed by this Java program?
Java
public class Test {
    public static void main(String[] args) {
        boolean flag = true;
        if (!flag || flag && false) {
            System.out.println("YES");
        } else {
            System.out.println("NO");
        }
    }
}
ANO
BYES
CCompilation error
DRuntime exception
Attempts:
2 left
💡 Hint
Evaluate the expression step by step, considering operator precedence and NOT operator.
❓ Predict Output
expert
3:00remaining
Complex logical expression evaluation
What is the output of this Java program?
Java
public class Test {
    public static void main(String[] args) {
        boolean a = true;
        boolean b = false;
        boolean c = true;
        boolean d = false;
        if ((a || b) && !(c && d) || b && !a) {
            System.out.println("PASS");
        } else {
            System.out.println("FAIL");
        }
    }
}
ARuntime exception
BPASS
CCompilation error
DFAIL
Attempts:
2 left
💡 Hint
Break down the expression into parts and evaluate carefully.

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

  1. Step 1: Understand the meaning of logical AND

    The operator && means both conditions must be true for the whole expression to be true.
  2. Step 2: Compare with other operators

    || means one or both true, ! reverses true/false, ^ means exclusive or (one true only).
  3. Final Answer:

    && (AND) -> Option A
  4. 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

  1. Step 1: Recall the NOT operator syntax

    In Java, the NOT operator is ! and placed before the boolean variable to reverse its value.
  2. 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.
  3. Final Answer:

    flag = !flag; -> Option B
  4. 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

  1. Step 1: Evaluate the expression step-by-step

    a && b is true && false = false. !b is !false = true.
  2. Step 2: Apply OR operator

    Now false || true = true.
  3. Final Answer:

    true -> Option C
  4. 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

  1. Step 1: Check the if condition syntax

    The condition uses '& |' which is not a valid operator combination in Java.
  2. Step 2: Understand valid logical operators

    Valid operators are &&, ||, &, | but not combined as '& |'. This causes a syntax error.
  3. Final Answer:

    Invalid use of & | operators together -> Option A
  4. 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

  1. 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.
  2. 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 <.
  3. Final Answer:

    if (n >= 10 && n <= 20) -> Option D
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard