Skip Top Navigation Bar

Logic Operators Practice

Evaluate Expressions

Grab a Piece of Paper!

Evaluate each expression.

Verify your answer by entering the code in the Java Playground.

Identify any places where short-circuiting occurs.

Use the variable declarations to evaluate the boolean expressions that use the relational operators and logic operators.

Learn more:

int a = 6, b = 3, c = 9, d = 2, e = 8;
double x = 7.5, y = 2.5, z = 3.0, w = 0.0;
boolean p = true, q = false;
  1. ((a + b) > c) && (d != b)
  2. ((c - a) <= b) || (e % d == 0)
  3. !((a * d) < c) && (x >= y)
  4. (b + d == a) || (c / b > d)
  5. ((e - b) > (a * d)) && p
  6. ((int)(x / y) == a / b) || q
  7. ((c % a) <= d) && (w == 0.0)
  8. (a + e > c) || (b * d < a)
  9. ((a - b) >= d) && (!q)
  10. ((z + w) < x) && ((a % d) == 0)

You can use the Java Playground to check your answers.

Write Code Snippets

Practice 1: Determine if eligible for a reward

Write a code segment to determine if a student is eligible to receive a reward. Students are eligible if they have an average above an 89 and an attendance rating greater than or equal to 80.

  1. Determine example test values for average and attendance so that you are testing all cases.
  2. Determine the expected value of isEligible for each of your test cases.
  3. Complete the code segment below.
  4. Test your code segment with each test case. If it doesn't yield your expected outcome, make necessary changes to your code.

Practice 2: Determine if you can purchase an item

Write a code segment to determine whether or not an item can be purchased. The value of canPurchase is true if the itemPrice is less than or equal to maxPrice or the item is on sale.

  1. Determine example test values for itemPrice, price and onSale so that you are testing all cases.
  2. Determine the expected value of canPurchase for each of your test cases.
  3. Complete the code segment below.
  4. Test your code segment with each test case. If it doesn't yield your expected outcome, make necessary changes to your code.

Write a code segment to determine whether or not a movie should be recommended. Movies are not recommended if it has a rating less than or equal to 5 and there are at least 9 reviews.

  1. Determine example test values for rating and reviews so that you are testing all cases.
  2. Determine the expected value of isRecommended for each of your test cases.
  3. Complete the code segment below.
  4. Test your code segment with each test case. If it doesn't yield your expected outcome, make necessary changes to your code.

Truth Tables

Grab a Piece of Paper!

Determine the possible outcomes for each expression by creating a truth table.

Verify your answer by entering the code in the Java Playground.

In these expressions, A and B are boolean variables, x, y are int variables.

  1. (A && B)
  2. !(A || B)
  3. (A && !B) || (!A && B)
  4. (A || B) && !C
  5. (x < y) && !(x >= y)

You can use the Java Playground to check your answers.