PrepGo

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

According to the provided text, where are local variables declared?

All Questions (9)

According to the provided text, where are local variables declared?

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 programmer declares a variable `int temp = 5;` inside the body of a method. Based on the rules of scope, where can this `temp` variable be used?

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 class has an instance variable named `count`. A method in that class also declares a local variable named `count`. Within the body of this method, what does the identifier `count` refer to?

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...'

Which of the following statements accurately describes the accessibility of a local 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 constructor for a `Student` class is defined as `public Student(String name)`. The class also has an instance variable `String name`. Inside the constructor, what does the identifier `name` refer to?

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.

The concept that defines where variables can be used in code is known as:

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.

Consider a method with a parameter `value` that is intended to update an instance variable also named `value`. If a programmer writes the statement `value = value;` inside this method, what is the most likely outcome?

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.

The phenomenon where a local variable in a method 'hides' an instance variable of the same name is a direct consequence of which principle?

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 variable is declared in the header of a `for` loop, such as `for (int i = 0; ...)`. What is the scope of the variable `i`?

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.