if Statements MCQ Practice

Question 1 of 10

A fairground kiosk prints a ride slip when a guest is tall enough. What text does this Java code display?

int height = 48;
if (height >= 48)
    System.out.print("ride");
System.out.print("ok");
  1. A. ok
  2. B. ride
  3. C. rideok
  4. D. nothing

Question 2 of 10

The same kiosk checks a shorter guest. What text does this Java code display?

int height = 40;
if (height >= 48)
    System.out.print("ride");
System.out.print("ok");
  1. A. ok
  2. B. rideok
  3. C. ride
  4. D. nothing

Question 3 of 10

A vending cart uses two-way selection for a coin count. What text does this Java code display?

int coins = 7;
if (coins < 5)
    System.out.print("low");
else
    System.out.print("ok");
  1. A. low
  2. B. lowok
  3. C. nothing
  4. D. ok

Question 4 of 10

A shed door stores whether it is unlocked. What text does this Java code display?

boolean open = true;
if (open)
    System.out.print("in");
else
    System.out.print("out");
  1. A. out
  2. B. in
  3. C. inout
  4. D. true

Question 5 of 10

A clerk meant both stock updates to run only when the shelf is already high. What value does this Java code display?

int stock = 4;
if (stock > 10)
    stock = stock + 5;
    stock = stock - 2;
System.out.print(stock);
  1. A. 4
  2. B. 9
  3. C. 2
  4. D. 7

Question 6 of 10

The clerk runs the same unbraced updates with a larger stock. What value does this Java code display?

int stock = 12;
if (stock > 10)
    stock = stock + 5;
    stock = stock - 2;
System.out.print(stock);
  1. A. 15
  2. B. 17
  3. C. 12
  4. D. 10

Question 7 of 10

Ribbon is grouped using integer division before a two-way decision. What text does this Java code display?

int ribbon = 23;
if (ribbon / 6 > 3)
    System.out.print("fit");
else
    System.out.print("left");
  1. A. fit
  2. B. left
  3. C. 3
  4. D. fitleft

Question 8 of 10

A booth updates two counters after a truncated comparison. What text does this Java code display?

int red = 11;
int blue = 3;
if (red / blue > 3)
{
    red = red - 4;
    System.out.print(red);
}
else
{
    blue = blue + 1;
    System.out.print(blue);
}
System.out.print(red);
  1. A. 77
  2. B. 4
  3. C. 711
  4. D. 411

Question 9 of 10

A tarp check uses this two-way selection.

if (wet)
    System.out.print("cover");
else
    System.out.print("open");

Which statement about this code is correct?

  1. A. Exactly one of the two print statements runs.
  2. B. Both print statements run when wet is true.
  3. C. The else body runs when wet is true.
  4. D. Neither print statement can run.

Question 10 of 10

A heater uses two separate one-way if statements. What value does this Java code display?

int heat = 18;
int mode = 0;
if (heat >= 15)
    mode = 2;
if (heat >= 10)
    mode = 1;
System.out.print(mode);
  1. A. 2
  2. B. 1
  3. C. 0
  4. D. 3