Preview

20 - Equivalent Boolean Expressions

 1. The "less than or equal to" comparison operator in Java is __________.

  !=

  <<

  <=

  <

 2. The equal comparison operator in Java is __________.

  !=

  Double equals sign, ==

  Single equals sign: =

  <>

 3. What is 1 + 1 + 1 + 1 + 1 == 5?

  TRUE

  0

  FALSE

  Error

 4. What is 1 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1 == 0.5?

  Error

  TRUE

  FALSE

  Floating poing numbers are approximations so there is no guarantee of this being true.

 5. In Java, the word "true" is ________.

  same as value 0

  same as value 1

  a Boolean literal

  a Java keyword

 6. Which of the following code displays the area of a circle if the radius is positive?

  if (radius >= 0) System.out.println(radius * radius * 3.14159);

  if (radius > 0) System.out.println(radius * radius * 3.14159);

  if (radius != 0) System.out.println(radius * radius * 3.14159);

  if (radius <= 0) System.out.println(radius * radius * 3.14159);

 7. What is the output of the following code?
int x = 0;
if (x < 4) {
  x = x + 1;
}
System.out.println("x is " + x);

  x is 0

  x is 2

  x is 3

  x is 1

 8. If the income is 4001, what is the output of the following code?
if (income > 3000) {
  System.out.println("Income is greater than 3000");
}
else if (income > 4000) {
  System.out.println("Income is greater than 4000");
}

  Income is greater than 3000 followed by income is greater than 4000

  Income is greater than 3000

  Income is greater than 4000

  No output

 9. Suppose isPrime is a boolean variable, which of the following is the correct and best statement for testing if isPrime is true?

  if (!isPrime = false)

  if (isPrime)

  if (isPrime = true)

  if (isPrime == true)

 10. Analyse the following code and select the statement that is correct.
boolean even = false;
if (even) {
  System.out.println("It is even!");
}

  The code displays nothing

  The code is wrong. You should replace if (even) with if (even == true).

  The code, when executed, will produce a run-time error

  The code displays "It is even!"