Skip Top Navigation Bar

Relational Operators Practice

Grab a Piece of Paper!

Evaluate each expression.

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

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

Learn more: Learn: Using Boolean Expression and Relational Operators

int a = 5, b = 3, c = 8, d = 2, e = 7;
double x = 6.5, y = 2.0, z = 3.5;
  1. (a + b) > c
  2. (d * b) <= e
  3. (x / y) == z
  4. (c - a) != d
  5. (a % d) < b
  6. (e + d) >= c
  7. (b - d) == (e % a)
  8. (x * y) < a
  9. ((a + c) / b) > d
  10. (e - a * d) <= b

You can use the Java Playground to check your answers.

Practice 1: Determine if highestScore needs to be updated

Write a code segment to set updateScore to whether it should be updated based on the values of score and highestScore. The value of updateScore is true if score is greater than highestScore and false otherwise.

  1. Determine example test values for score and highestScore so that you are testing all three cases:
    • score is greater than highestScore
    • score is equal to highestScore
    • score is less than highestScore
  2. Determine the expected value of updateScore for each of your three 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 based on the budget

Write a code segment to determine whether or not an item can be purchased. The value of canPurchase is true if price is less than or equal to budget and false otherwise.

  1. Determine example test values for price and budget so that you are testing all three cases.
  2. Determine the expected value of canPurchase for each of your three 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 3: Determine if chicken is fully cooked

Write a code segment to determine whether or not an chicken is fully cooked. Chicken is fully cooked if its internal temperature greater than or equal to 165 degrees.

  1. Determine example test values for temperature so that you are testing all three cases.
  2. Determine the expected value of safeToEat for each of your three 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 4: Determine if the class is full

Write a code segment to determine whether or not a class is full. The value of isClassFull is true if students is equal to maxStudents and false otherwise.

  1. Determine example test values for students and maxStudents so that you are testing all cases.
  2. Determine the expected value of isClassFull 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 5: Determine if the id is ok to use

Write a code segment to determine whether or not an id is ok to be used. The value of isIdOK is true if id does not equal invalidId and false otherwise.

  1. Determine example test values for id and invalidId so that you are testing all three cases.
  2. Determine the expected value of isIdOK for each of your three 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.