Scope and Access FRQ Practice

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

Question 1 of 5

A method contains this fragment.

int total = 1;
if (total > 0)
{
    int chip = 4;
    total += chip;
}

(a) Identify the block where chip can be used. (b) State the value of total after the if. (c) Explain what happens if a later statement in the same method prints chip.

Question 2 of 5

A helper is declared as public int wrap(int n).

(a) Identify whether n is local and whether n may be declared private. (b) If wrap contains n = n + 2; and a caller has int k = 5; wrap(k); explain whether k changes. (c) Write one sentence stating the scope of n.

Question 3 of 5

A sign class shadows width.

public class Sign
{
    private int width = 10;
    public int stretch(int extra)
    {
        int width = 4;
        return width + extra;
    }
    public int getWidth()
    {
        return width;
    }
}

(a) State the value returned by stretch(2). (b) State getWidth() after that call. (c) Explain which width each simple name refers to.

Question 4 of 5

Complete the scope table, then answer the questions.

int sum = 0;
for (int n = 1; n <= 3; n++)
{
    int step = n * 2;
    sum += step;
}
Name Declared in Usable after the loop?
sum
n
step

(a) Fill each row. (b) State the value of sum after the loop. (c) Explain why n and step cannot be printed after the loop.

Question 5 of 5

A gate method uses a parameter with the same name as an instance variable.

public class Gate
{
    private int bolts = 6;
    public void setBolts(int bolts)
    {
        bolts = bolts;
    }
    public int getBolts()
    {
        return bolts;
    }
}

Gate g = new Gate();
g.setBolts(2);
System.out.print(g.getBolts());

(a) State the displayed text and explain why. (b) Explain whether the parameter bolts is local. (c) Explain why the assignment in setBolts does not change the instance variable.