AP Computer Science A • Unit 3.8

Scope and Access

Detailed answer explanations for the Unit 3.8 multiple-choice practice questions.

Q1B
Q2C
Q3A
Q4C
Q5A
Q6B
Q7C
Q8D
Q9B
Q10D
Q11C
Q12B
Q13A
Q14C
How to use this page: First try each question without looking at the explanation. Then compare your answer with the key and read why the other choices are not correct.

Answer Explanations

Q1

What does the scope of a variable determine?

AHow many values the variable may store
BThe region of code where its name may be used
CWhich constructor creates the variable
DWhether the variable must contain an integer
Correct answer: B. Scope is about where a name is recognized, not about what the variable holds.
Explanation

Every declaration creates a name and places it in a region of the program. Outside that region the compiler does not recognize the name at all, which is why scope errors are caught before the program runs.

  • A is not correct: a variable stores one value at a time regardless of where it is declared.
  • C is not correct: constructors initialize an object's state; they do not define where a name is recognized.
  • D is not correct: what a variable may contain is settled by its type, which is a separate part of the declaration.
Q2

A variable declared directly inside a method body is what kind of variable?

AA class header
BA constructor name
CA local variable
DA package name
Correct answer: C. A variable declared inside a method is local to that method.
Explanation

Where a declaration sits is what gives a variable its kind. Inside a method it is local; inside the class body but outside every method it is an instance or class variable.

  • A is not correct: a class header is the line that names the class, not a variable.
  • B is not correct: a constructor name matches the class name and identifies a constructor rather than a variable.
  • D is not correct: a package name organizes classes and has nothing to do with declarations inside a method.
Q3

Which declaration is invalid inside a method body?

Aprivate int count = 0;
Bint count = 0;
Cint count = 3 + 1;
Dint count;
Correct answer: A. A local variable cannot carry an access modifier, because its scope is already limited to the method.
Explanation

Access modifiers answer the question of which classes may reach a member. A local variable is never reachable from another class in the first place, so the question does not arise and the modifier is rejected.

  • B is not correct: this is the ordinary form of a local declaration with a starting value.
  • C is not correct: a starting value may be any expression of the right type, including one that must be evaluated.
  • D is not correct: a local variable may be declared without a starting value and assigned later.
Q4

An instance variable seats is 12. What does capacity(18) return?

public int capacity(int seats) {
    return seats;
}
A0
B12
C18
DA compile-time error
Correct answer: C. The parameter shares the instance variable's name, and inside the method the parameter is the one the name refers to.
Explanation

When two declarations share a name, the one with the narrower scope wins inside that scope. The parameter's scope is this method body, so seats here means the value passed in and not the object's stored value.

  • A is not correct: nothing in the method sets the value to zero.
  • B is not correct: 12 is the instance variable, which the parameter hides for the length of this method.
  • D is not correct: reusing a name in a narrower scope is legal, so the method compiles.
Q5

A variable is declared inside a for loop block. Where may it be used?

AOnly inside that loop block
BAnywhere in the class
COnly in constructors
DOnly before the loop starts
Correct answer: A. A declaration inside a block is recognized only within that block.
Explanation

Braces mark the boundaries of a scope. A name introduced between a matching pair of braces stops being recognized at the closing brace, which is why a value that must survive the loop has to be declared before it.

  • B is not correct: reaching the whole class describes an instance or class variable, which is declared in the class body.
  • C is not correct: constructors are ordinary members and have no special access to another block's local names.
  • D is not correct: a name cannot be used before the declaration that introduces it has been reached.
Q6

Where is a method parameter in scope?

AIn every method of the class
BThroughout the body of that method
COnly on the method-header line
DAfter the method's closing brace
Correct answer: B. A parameter behaves like a local variable of the method it belongs to.
Explanation

The parameter is declared in the header and is available from the first statement of the body to the last. Its scope ends with the method, which is why a change made to a parameter does not outlive the call.

  • A is not correct: each method declares its own parameters, and they are not visible to one another.
  • C is not correct: a parameter that existed only on the header line could never be used, which would defeat its purpose.
  • D is not correct: the closing brace is where the parameter's scope ends, not where it begins.
Q7

Which line causes a scope-related compile-time error?

if (open) {
    int n = 6;
    System.out.println(n);
}
System.out.println(n);
AThe if header
BThe print statement inside the if block
CThe print statement after the if block
DNo line; the code compiles
Correct answer: C. n is declared inside the if block, so its name is no longer recognized after the closing brace.
Explanation

The same name appears in two print statements, and only one of them is inside the block where n was declared. Moving the declaration above the if would put both statements inside its scope.

  • A is not correct: the header tests a condition and introduces no name of its own.
  • B is not correct: this statement sits inside the block where n is declared, so the name is recognized there.
  • D is not correct: the final statement uses a name that is out of scope, so the code does not compile.
Q8

Which must be declared before its name can be used?

AThe keyword class
BThe keyword if
CThe operator +
DA local variable
Correct answer: D. Names the programmer introduces must be declared; the words and symbols built into the language do not.
Explanation

Keywords and operators are part of Java itself and always mean the same thing. A variable name means whatever its declaration says it means, which is why the declaration has to come first.

  • A is not correct: class is a keyword built into the language.
  • B is not correct: if is likewise a keyword and needs no declaration.
  • C is not correct: + is an operator provided by the language.
Q9

Which statement about a constructor parameter is true?

AIt is automatically an instance variable.
BIt is local throughout the constructor body.
CIt must be declared private.
DIt may be used by every method in the class.
Correct answer: B. A constructor parameter follows the same scope rule as any other parameter: it exists only within its own body.
Explanation

This is why a constructor normally assigns its parameter to an instance variable. Without that assignment the value arrives, is used inside the constructor, and disappears when the constructor ends.

  • A is not correct: an instance variable exists only where the class declares one; a parameter does not become one on its own.
  • C is not correct: access modifiers apply to members, not to parameters.
  • D is not correct: other methods cannot see the constructor's parameter; they can see the instance variable it was stored in.
Q10

Which variable can usually be used by multiple instance methods in one class?

AA local variable declared inside one method
BA loop control variable
CA parameter of one constructor
DAn instance variable declared in the class
Correct answer: D. An instance variable is declared in the class body, so every instance method of that class can use it.
Explanation

The three wrong options are all declared inside something narrower than the class, so each one disappears when its own region ends. Only a declaration in the class body has a scope wide enough to be shared.

  • A is not correct: a local variable is confined to the method that declares it.
  • B is not correct: a loop control variable declared in the loop header is confined to the loop.
  • C is not correct: a constructor parameter is confined to the constructor body.
Q11

Examine the following method. Which statement causes a compile-time error?

public int calc(boolean b) {
    int total = 2;
    if (b) {
        int add = 3;
        total += add;
    }
    return add;
}
Aint total = 2;
Btotal += add;
Creturn add;
DNo statement; the method compiles.
Correct answer: C. add is declared inside the if block, so its name is out of scope by the time the return is reached.
Explanation

The variable is used twice, once inside the block and once outside it, and only the second use is a problem. Note that this is not about whether the if condition is true: the compiler rejects the name regardless of what happens at run time.

  • A is not correct: total is declared directly in the method body, which is an ordinary local declaration.
  • B is not correct: this statement is inside the block where add is declared, so both names are in scope there.
  • D is not correct: the return statement uses a name that is no longer recognized, so the method does not compile.
Q12

Examine the following class. What happens when this class is compiled?

public class Tag {
    public Tag(String id) {
        System.out.println(id);
    }
    public void show() {
        System.out.println(id);
    }
}
AIt compiles because id is a constructor parameter.
BA compile-time error occurs because id is out of scope in show.
CA compile-time error occurs because constructors cannot have parameters.
DIt compiles and show prints null.
Correct answer: B. id exists only inside the constructor, so show cannot use that name.
Explanation

This is the mistake the previous question's rule predicts. The class declares no instance variable at all, so the value arriving through the constructor is never stored anywhere that show could reach. Adding a private instance variable and assigning the parameter to it is the usual repair.

  • A is not correct: being a constructor parameter is exactly why the name is unavailable elsewhere in the class.
  • C is not correct: constructors take parameters routinely, and that is how an object receives its starting values.
  • D is not correct: the error is caught at compile time, so the class never runs to print anything.
Q13

Examine the following class. What is printed?

public class Gauge {
    private int mark = 68;
    public int read(int mark) {
        return mark;
    }
    public int stored() {
        return mark;
    }
}
Gauge g = new Gauge();
System.out.println(g.read(72) + " " + g.stored());
A72 68
B68 68
C72 72
D68 72
Correct answer: A. In read the parameter hides the instance variable, while stored has no parameter and so reports the object's value.
Explanation

The same name means different things in the two methods, and the difference is decided entirely by what each method declares. Because read declares a parameter called mark, that narrower declaration wins inside its body; stored declares nothing, so the name still refers to the instance variable.

  • B is not correct: this treats read as returning the instance variable, but its parameter hides that variable.
  • C is not correct: this treats the argument as having changed the object's stored value, which no assignment does.
  • D is not correct: the two values are swapped.
Q14

Examine the following class. What is printed?

public class Purse {
    private int cash = 30;
    public void set(int cash) {
        cash = cash;
    }
    public int stored() {
        return cash;
    }
}
Purse p = new Purse();
p.set(45);
System.out.println(p.stored());
A45
B0
C30
DNothing is printed; the code does not compile.
Correct answer: C. Both names in cash = cash; refer to the parameter, so the instance variable is never touched.
Explanation

This is the quiet version of the shadowing trap. The statement is legal and the program runs, but it assigns the parameter to itself and leaves the object exactly as it was. A mutator written this way appears to work while changing nothing.

  • A is not correct: 45 is the argument, which never reaches the instance variable.
  • B is not correct: the instance variable was given a starting value in its declaration, so it is not zero.
  • D is not correct: assigning a variable to itself is legal, so the class compiles and runs.