Nested if Statements FRQ Practice

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

Question 1 of 5

A walk-or-stay sign should work as follows. If rain is true, then print stay when wind > 20 and print walk otherwise. If rain is false, print sun.

(a) Write nested if / if-else statements that produce that behavior. (b) State the displayed text when rain is true and wind is 15. (c) Explain why the wind comparison is evaluated only when rain is true.

Question 2 of 5

Complete the missing table cells for this multiway selection. Then explain why later tests may be skipped.

int score = /* table value */;
if (score >= 90)
    System.out.print("A");
else if (score >= 80)
    System.out.print("B");
else if (score >= 70)
    System.out.print("C");
else
    System.out.print("D");
score Displayed text
95 A
80
70
69

(a) Fill the empty cells. (b) Explain why score 80 does not also print C even though 80 >= 70. (c) Identify the role of the trailing else.

Question 3 of 5

This dangling-else example is indented to look as if the else belongs to the outer if.

int n = 3;
if (n > 5)
    if (n > 1)
        System.out.print("in");
else
    System.out.print("out");

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

Question 4 of 5

Code R uses if-else-if. Code S uses two separate if statements.

Code R:

int n = 11;
int k = 0;
if (n > 10)
    k = 1;
else if (n > 5)
    k = 2;

Code S:

int n = 11;
int k = 0;
if (n > 10)
    k = 1;
if (n > 5)
    k = 2;

(a) State the value of k after Code R. (b) State the value of k after Code S. (c) Explain why the two forms disagree for this n.

Question 5 of 5

Trace this nested selection by hand.

int g = 13;
int h = 4;
if (g / h > 3)
{
    if (h == 4)
        System.out.print("P");
}
else
{
    System.out.print("Q");
    if (g > 10)
        System.out.print("R");
}

(a) State the integer value of g / h and which outer branch runs. (b) State the displayed text. (c) Explain whether the inner h == 4 test is evaluated. (d) Explain how truncation chose the outer branch.