if Statements FRQ Practice

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

Question 1 of 5

A night-market lamp should print on only when hour is at least 18. No other text should come from that decision.

(a) Write a one-way if that prints on only when hour >= 18. (b) State the displayed text from that if when hour is 17. (c) Explain how adding an else that prints off would change the selection from one-way to two-way.

Question 2 of 5

Complete the missing table cells for this two-way selection. Then explain the zero case.

int n = /* table value */;
if (n > 0)
    System.out.print("pos");
else
    System.out.print("not");
n Condition n > 0 Displayed text
4 true pos
0
-3

(a) Fill the empty cells. (b) Explain why n equal to 0 takes the else branch. (c) Identify this code as one-way or two-way selection.

Question 3 of 5

A packing desk uses this unbraced if.

int bags = 6;
if (bags < 5)
    bags = bags + 10;
    bags = bags - 1;

(a) State the value stored in bags after this code runs. (b) Identify which assignment is not controlled by the if because braces are missing. (c) Rewrite the selection with braces so both assignments run only when bags < 5, and state the value of bags after that rewrite, starting from 6.

Question 4 of 5

Compare one-way selection P with two-way selection Q.

if (seats > 0)
    System.out.print("open");
System.out.print("gate");

Code Q:

if (seats > 0)
    System.out.print("open");
else
    System.out.print("full");

(a) State the displayed text for P and for Q when seats is 3. (b) State the displayed text for P and for Q when seats is 0. (c) Explain how the else in Q changes when the second printed word runs.

Question 5 of 5

Trace this pair of selections by hand.

int a = 14;
int b = 4;
if (a / b > 3)
{
    a = a + 2;
}
else
{
    b = b * 2;
}
if (a > b)
    System.out.print(a);
else
    System.out.print(b);

(a) State the integer value of a / b and whether the first condition is true. (b) State the values of a and b after the first selection. (c) State the displayed text. (d) Explain how integer truncation chose the first branch.