Scope and Access

Help Questions

AP Computer Science A › Scope and Access

Questions 1 - 5
1

In a library system, Book stores title and author privately to avoid patron edits. The front desk uses public borrowBook and returnBook to enforce rules. A protected calculateOverdueFee supports internal charges. How does the chosen access modifier affect access to the title field?

private makes title accessible to subclasses only, even outside the package.

private allows any class in the package to read title without methods.

private means title cannot be read even inside Book methods.

private restricts direct access to within Book, supporting information hiding.

Explanation

This question tests AP Computer Science A concepts, specifically understanding of scope and access control in Java classes. Access control in Java is managed through modifiers like public, private, and protected, which determine the visibility and accessibility of classes, fields, and methods. In this scenario, the Book class uses private for the title field to prevent external code from directly accessing or modifying book titles, supporting the principle of information hiding. Choice A is correct because private access restricts direct access to the title field to only within the Book class itself, ensuring that any access to the title must go through appropriate methods, thus supporting proper encapsulation and information hiding. Choice D is incorrect because private fields are fully accessible within their own class's methods - if this were true, the field would be useless as the class couldn't work with its own data. To help students: Emphasize that private means 'class-only access,' not 'no access at all.' Show how getter methods can provide controlled read access to private fields. Watch for: The critical misconception that private fields can't be accessed by their own class's methods.

2

In a banking system, BankAccount uses public deposit and withdraw for safe updates. The balance field stays hidden to prevent bypassing overdraft checks. A protected method computeMonthlyInterest supports internal calculations. What access modifier should be used for the balance field in the given scenario?

default, so balance is hidden from classes in the same package.

protected, so all external classes can view balance but not change it.

public, so any class can read and write balance directly.

private, so only BankAccount controls balance changes through methods.

Explanation

This question tests AP Computer Science A concepts, specifically understanding of scope and access control in Java classes. Access control in Java is managed through modifiers like public, private, and protected, which determine the visibility and accessibility of classes, fields, and methods. In this scenario, the BankAccount class needs to protect the balance field from direct external access to ensure all modifications go through methods that enforce overdraft protection and other business rules. Choice C is correct because marking balance as private ensures that only the BankAccount class's own methods (like deposit and withdraw) can access and modify the balance, maintaining complete control over how the balance changes. Choice B is incorrect because protected would still allow subclasses and package members to directly access balance, and it doesn't prevent changes - access modifiers don't create read-only behavior. To help students: Reinforce that sensitive data like financial information should almost always be private. Practice identifying fields that need protection versus methods that need accessibility. Watch for: Misconceptions that access modifiers can create read-only behavior without additional mechanisms.

3

In a library system, a Book class stores title and author while tracking whether it is checked out. The title and author should not be rewritten by patrons, but borrowBook and returnBook must be callable by the front desk app. A protected method calculateOverdueFee helps internal charge tracking. What access modifier should be used for the borrowBook method in this scenario?

default, so any class in any package can call borrowBook.

protected, so only subclasses can call borrowBook.

public, so outside code can borrow a book through the method.

private, so only Book can call borrowBook.

Explanation

This question tests AP Computer Science A concepts, specifically understanding of scope and access control in Java classes. Access control in Java is managed through modifiers like public, private, and protected, which determine the visibility and accessibility of classes, fields, and methods. In this scenario, the Book class needs its borrowBook method to be accessible by the front desk application, which is external code that must interact with the library system. Choice C is correct because the public access modifier allows the borrowBook method to be called from any class, including the front desk app that operates outside the Book class, enabling the core functionality of the library system. Choice A is incorrect because marking borrowBook as private would restrict it to only the Book class itself, preventing the front desk app from borrowing books, which defeats the purpose of the system. To help students: Emphasize that public methods form the interface through which external code interacts with a class. Practice identifying which methods need external access versus internal-only access. Watch for: Students confusing the need for data protection (private fields) with the need for accessible functionality (public methods).

4

In student records, a StudentRecord class stores studentId and grades to protect sensitive data. The counselor app calls updateGrade to change grades through validation rules. A protected method computeGpa aggregates grades for reports without exposing raw calculations. What is the outcome if grades is changed from private to public?

The field becomes accessible only within the same package, not outside it.

Outside code can modify grades directly, weakening information hiding.

The field becomes immutable, so updateGrade no longer compiles.

Only subclasses can read grades, but no code can modify it.

Explanation

This question tests AP Computer Science A concepts, specifically understanding of scope and access control in Java classes. Access control in Java is managed through modifiers like public, private, and protected, which determine the visibility and accessibility of classes, fields, and methods. In this scenario, changing the grades field from private to public removes the protective encapsulation that ensures grade modifications go through proper validation in the updateGrade method. Choice A is correct because making grades public allows any external code to directly read and modify the grades array, bypassing the validation rules in updateGrade and breaking the information hiding principle that protects data integrity. Choice D is incorrect because changing access modifiers doesn't affect mutability - public fields can still be modified unless they are also marked as final. To help students: Demonstrate the dangers of public fields with examples showing how validation can be bypassed. Emphasize that encapsulation isn't just about hiding data, but ensuring it's modified correctly. Watch for: Students thinking that access modifiers affect mutability or that public somehow provides protection.

5

In an inventory system, InventoryItem stores itemId and quantity to ensure updates follow business rules. The register uses public addStock and removeStock rather than changing quantity directly. A protected calculateRestockLevel supports internal decisions without UI access. Why is the quantity field marked as private?

It ensures only InventoryItem methods change quantity, supporting validation.

It allows subclasses in other packages to edit quantity directly.

It prevents quantity from being stored in memory until addStock runs.

It lets any class in the package update quantity without method calls.

Explanation

This question tests AP Computer Science A concepts, specifically understanding of scope and access control in Java classes. Access control in Java is managed through modifiers like public, private, and protected, which determine the visibility and accessibility of classes, fields, and methods. In this scenario, the InventoryItem class marks quantity as private to ensure all modifications go through the controlled addStock and removeStock methods which can enforce business rules. Choice B is correct because private access ensures that only methods within the InventoryItem class can directly access and modify quantity, forcing all external code to use the public methods that include proper validation and business logic. Choice A is incorrect because private prevents all external access, including from classes in the same package - package access would require default (no modifier) access level. To help students: Emphasize that private fields with public methods is the standard pattern for encapsulation. Practice identifying what validation might be needed and how private fields protect against invalid states. Watch for: Confusion between private and default package access.