Compound Boolean Expressions MCQ Practice

Question 1 of 10

A greenhouse misting valve requires both sensors to be true. What text does this Java code display?

boolean humid = true;
boolean shaded = true;
System.out.print(humid && shaded);
  1. A. false
  2. B. true
  3. C. humid
  4. D. 2

Question 2 of 10

A museum pass is inverted before it is printed. What text does this Java code display?

boolean expired = true;
System.out.print(!expired);
  1. A. true
  2. B. false
  3. C. expired
  4. D. 1

Question 3 of 10

A gallery door uses ! before ||. What text does this Java code display?

boolean closed = true;
boolean member = true;
System.out.print(!closed || member);
  1. A. false
  2. B. true
  3. C. closed
  4. D. The code does not compile

Question 4 of 10

A bakery timer uses && and || without parentheses. What text does this Java code display?

boolean p = false;
boolean q = true;
boolean r = true;
System.out.print(p && q || r);
  1. A. false
  2. B. true
  3. C. p
  4. D. The code throws an exception

Question 5 of 10

A plow dispatcher mixes !, &&, and ||. What text does this Java code display?

boolean fog = true;
boolean ice = false;
boolean plow = true;
System.out.print(!fog && ice || plow);
  1. A. true
  2. B. false
  3. C. fog
  4. D. The code does not compile

Question 6 of 10

The method ding prints D when it runs. What text does this Java code display?

public boolean ding()
{
    System.out.print("D");
    return true;
}

boolean fresh = true;
System.out.print(fresh || ding());
  1. A. Dtrue
  2. B. true
  3. C. Dfalse
  4. D. false

Question 7 of 10

A bakery divides loaves among ovens. What text does this Java code display?

int ovens = 0;
int loaves = 24;
boolean busy = (ovens != 0) && (loaves / ovens >= 6);
System.out.print(busy);
  1. A. true
  2. B. The code throws an ArithmeticException
  3. C. false
  4. D. 6

Question 8 of 10

A library desk uses hour bounds and a weekend flag. What text does this Java code display?

int hour = 9;
boolean weekend = false;
boolean open = hour >= 8 && hour < 17 || weekend;
System.out.print(open);
  1. A. false
  2. B. true
  3. C. 9
  4. D. The code does not compile

Question 9 of 10

The method tap prints T each time it runs. What text does this Java code display?

public boolean tap()
{
    System.out.print("T");
    return false;
}

System.out.print(tap() && tap());
  1. A. Tfalse
  2. B. TTfalse
  3. C. TTtrue
  4. D. false

Question 10 of 10

The method buzz prints B when it runs. && is evaluated before ||. What text does this Java code display?

public boolean buzz()
{
    System.out.print("B");
    return true;
}

int slots = 0;
boolean full = false;
System.out.print(!full && buzz() || (slots > 0) && (9 / slots == 3));
  1. A. Btrue
  2. B. true
  3. C. Bfalse
  4. D. The code throws an ArithmeticException