AP Computer Science A Practice Quiz: Scope and Access
Written by AP Content Team, Verified for 2026 AP Exams, Last updated: May 2026
Test your understanding with short quizzes. This quiz has 9 questions to check your progress.
Question 1 of 9
All Questions (9)
A) Only outside of all methods and constructors.
B) Only in the headers of blocks of code.
C) In the headers or bodies of blocks of code.
D) Only in the bodies of blocks of code.
Correct Answer: C
The content explicitly states, 'Local variables are variables declared in the headers or bodies of blocks of code.'
A) In any method within the same class.
B) Anywhere in the program after its declaration.
C) Only within the block of the method where it was declared.
D) In the method where it was declared and any methods that it calls.
Correct Answer: C
The provided content specifies that 'Local variables can only be accessed in the block in which they are declared.' A method's body is a block.
A) The instance variable.
B) The local variable.
C) It is ambiguous and will result in a compilation error.
D) Whichever variable was declared first.
Correct Answer: B
The content states, 'When there is a local variable or parameter with the same name as an instance variable, the variable name will refer to the local variable instead of the instance variable...'
A) It is accessible globally throughout the entire class.
B) It is confined to the block in which it is defined.
C) It can be accessed by other classes in the same package.
D) It is accessible only before the block in which it is declared.
Correct Answer: B
This is a direct application of the rule: 'Local variables can only be accessed in the block in which they are declared.'
A) The instance variable `name`.
B) The constructor parameter `name`.
C) This usage is illegal and will cause a compiler error.
D) It refers to a new, uninitialized variable.
Correct Answer: B
According to the provided text, a parameter with the same name as an instance variable will take precedence. The identifier 'name' will refer to the local parameter within the constructor's body.
A) Encapsulation
B) Inheritance
C) Scope
D) Polymorphism
Correct Answer: C
The first point of the provided content is to 'Explain where variables can be used in the code,' which is the definition of variable scope.
A) The instance variable is correctly updated with the parameter's value.
B) The code fails to compile due to the duplicate name.
C) The parameter `value` is assigned its own value, leaving the instance variable unchanged.
D) The instance variable's value is assigned to the local parameter.
Correct Answer: C
Due to variable shadowing, both instances of `value` in the assignment refer to the local parameter. The statement effectively does nothing to the instance variable, as it assigns the parameter's value to itself.
A) A variable name must be unique within an entire class.
B) A local name reference takes precedence over an instance variable name within the local scope.
C) Instance variables are always private and cannot be accessed directly.
D) Local variables are automatically initialized to a default value.
Correct Answer: B
The content explicitly states that 'the variable name will refer to the local variable instead of the instance variable' when names conflict within a method's body. This is the principle of local precedence.
A) It is accessible throughout the entire method containing the loop.
B) It is only accessible after the `for` loop has completed execution.
C) It is accessible within the header and the body of the `for` loop only.
D) It is accessible to all other loops within the same method.
Correct Answer: C
The `for` loop, including its header and body, constitutes a single block. As a local variable declared in the header of this block, `i` can only be accessed within that block.