Nested if Statements FRQ Practice

Responses receive AI feedback against a rubric after submission. Scores are practice estimates.

Question 1 of 5

A cafeteria line should work as follows. If vegetarian is true, print stew when soupLeft > 0 and print salad otherwise. If vegetarian is false, print grill.

(a) Write nested if / if-else statements that produce that behavior. (b) State the displayed text when vegetarian is true and soupLeft is 0. (c) Explain why soupLeft is compared only after vegetarian is true.

Question 2 of 5

Complete the missing table cells for this tide multiway selection.

int tide = /* table value */;
if (tide >= 8)
    System.out.print("high");
else if (tide >= 4)
    System.out.print("mid");
else if (tide >= 1)
    System.out.print("low");
else
    System.out.print("dry");
tide Displayed text
9 high
4
1
0

(a) Fill the empty cells. (b) Explain why tide 4 does not also print low. (c) State how many segments of this if-else-if chain can run for one value of tide.

Question 3 of 5

This code uses dangling-else pairing.

int depth = 3;
if (depth > 5)
    if (depth > 0)
        System.out.print("wet");
else
    System.out.print("dry");

(a) Identify which if the else pairs with. (b) State the displayed text of this code. (c) Rewrite with braces so the else belongs to the outer if, and state the displayed text of that rewrite.

Question 4 of 5

Code T uses if-else-if. Code U uses two separate if statements.

Code T:

int heat = 900;
int stage = 0;
if (heat > 800)
    stage = 3;
else if (heat > 400)
    stage = 2;

Code U:

int heat = 900;
int stage = 0;
if (heat > 800)
    stage = 3;
if (heat > 400)
    stage = 2;

(a) State the value of stage after Code T. (b) State the value of stage after Code U. (c) Explain how if-else-if versus separate ifs produces those results.

Question 5 of 5

Trace this nested selection by hand.

int p = 17;
int q = 5;
if (p / q > 3)
{
    if (p > 10)
        System.out.print("X");
    else
        System.out.print("Y");
}
else
{
    if (q == 5)
        System.out.print("Z");
    else
        System.out.print("W");
}

(a) State the integer value of p / q and which outer branch runs. (b) State the displayed text. (c) Identify which inner condition is evaluated. (d) Explain how truncation chose the outer branch.