Abstraction and Program Design

Help Questions

AP Computer Science A › Abstraction and Program Design

Questions 1 - 7
1

Based on the class design for the online store, how does encapsulation benefit the design of the Product class with private price and stockQuantity fields?

It requires Product to be declared static.

It allows direct field access from any class.

It prevents using constructors in Product.

It hides fields and enforces valid updates.

Explanation

This question tests AP Computer Science A skills, specifically abstraction and program design through understanding encapsulation benefits in e-commerce contexts. Encapsulation with private fields ensures that critical business data like price and stock quantity can only be modified through validated methods, preventing data corruption. Choice B is correct because encapsulation hides the price and stockQuantity fields (making them private) while enforcing valid updates through public methods that can validate changes, such as preventing negative prices or stock. Choice A is incorrect because encapsulation requires fields to be private, not public - direct field access is exactly what encapsulation prevents to maintain data integrity. To help students: Emphasize that encapsulation protects critical business data by hiding fields and validating all changes through methods. Practice identifying scenarios where data validation is crucial for maintaining system integrity. Watch for: misunderstanding that encapsulation promotes field hiding, not field exposure.

2

Based on the class design for the weather station, how does encapsulation benefit the design of the WeatherData class storing temperature, humidity, and windSpeed?

It allows controlled updates via public methods.

It prevents methods from updating readings.

It requires WeatherData to extend ArrayList.

It forces all fields to be public.

Explanation

This question tests AP Computer Science A skills, specifically abstraction and program design through understanding encapsulation benefits. Encapsulation involves making fields private and providing public methods to control access, ensuring data integrity and hiding implementation details. Choice C is correct because encapsulation allows the WeatherData class to provide public methods (like updateReadings) that validate and control how temperature, humidity, and windSpeed are modified, preventing invalid data. Choice A is incorrect because encapsulation actually requires fields to be private, not public, which is the opposite of what this choice states. To help students: Emphasize that encapsulation protects data by hiding it (private fields) while providing controlled access (public methods). Practice identifying the benefits of data hiding and controlled access. Watch for: confusion between making fields public (breaks encapsulation) versus providing public methods (proper encapsulation).

3

In the library system, checkAvailability returns whether a Book can be lent; method must not expose fields directly. Which method signature correctly implements checkAvailability?

public void checkAvailability()

public boolean checkAvailability()

public boolean checkAvailability(boolean available)

public static boolean checkAvailability()

Explanation

This question tests AP Computer Science A skills, specifically abstraction and program design through implementing getter methods that maintain encapsulation. The checkAvailability method should return the book's availability status without exposing the actual field directly, following encapsulation principles. Choice A is correct because it has public visibility (allowing external code to check availability) and returns boolean without parameters, properly implementing a getter that returns the availability status. Choice C is incorrect because it takes a boolean parameter, which doesn't make sense for a method that should simply return the current availability status - it's mixing getter and setter concepts. To help students: Emphasize that getter methods return information without parameters, while setters accept parameters to update state. Practice distinguishing between getters (no parameters, return value) and setters (parameters, usually void). Watch for: adding unnecessary parameters to getter methods or confusing getter/setter patterns.

4

Based on the class design for the weather station, updateReadings sets temperature, humidity, and windSpeed from parameters. Which method signature correctly implements updateReadings for WeatherData?

private void updateReadings(double t, double h, double w)

public void updateReadings(double t, double h, double w)

public void updateReadings(int t, int h, int w)

public double updateReadings(double t, double h, double w)

Explanation

This question tests AP Computer Science A skills, specifically abstraction and program design through implementing setter methods. The updateReadings method should accept three double parameters (temperature, humidity, windSpeed) and update the corresponding fields without returning a value. Choice A is correct because it has public visibility (allowing external sensor updates), accepts three double parameters matching the weather data types, and returns void since it's a setter method that updates state. Choice C is incorrect because it uses int parameters instead of double, which would lose precision for weather measurements like 72.5°F or 65.3% humidity. To help students: Emphasize that setter methods typically return void and use parameter types that match the field types. Practice writing setter methods with appropriate parameter types and visibility. Watch for: using incorrect parameter types that lose precision or returning values from setter methods.

5

Based on the class design for the online store, which method signature correctly implements calculating a percentage discount on a Product price?

public void applyDiscount(int percent)

public int applyDiscount(double percent)

private double applyDiscount(double percent)

public double applyDiscount(double percent)

Explanation

This question tests AP Computer Science A skills, specifically abstraction and program design through selecting appropriate method signatures. The applyDiscount method should calculate a percentage discount on a product price, requiring it to accept a percentage parameter and return the discounted price as a double. Choice A is correct because it has public visibility (allowing external access), accepts a double percent parameter (supporting decimal percentages like 15.5%), and returns a double (the calculated discounted price). Choice B is incorrect because it returns void, preventing the method from returning the calculated discount amount, and uses int for percent which limits precision. To help students: Emphasize matching return types to the method's purpose - calculations typically return values. Practice identifying when methods should return values versus void. Watch for: confusion between void and value-returning methods, and inappropriate parameter types.

6

A student record console app computes GPA from a list of grades; calculateGpa returns a double. Which method signature correctly implements calculateGpa for the Student class?

private double calculateGpa()

public double calculateGpa()

public void calculateGpa(double gpa)

public int calculateGpa()

Explanation

This question tests AP Computer Science A skills, specifically abstraction and program design through selecting appropriate method signatures for calculations. The calculateGpa method computes GPA from grades and returns it as a double, which is the standard data type for GPA values that include decimals (like 3.75). Choice B is correct because it has public visibility (allowing external access to this important calculation) and returns double, which properly represents GPA values with decimal precision. Choice A is incorrect because it returns int, which would truncate GPA values like 3.75 to 3, losing important precision in academic calculations. To help students: Emphasize choosing appropriate return types based on the nature of the data - GPAs need decimal precision. Practice identifying when to use int versus double for different types of calculations. Watch for: using integer types for values that require decimal precision.

7

Based on the class design for the online store, Product has name, price, stockQuantity; addToCart reduces stock by one when possible. Identify the correct implementation of the addToCart method based on the scenario.

private boolean addToCart(){ stockQuantity--; return true; }

public int addToCart(){ stockQuantity--; return stockQuantity; }

public boolean addToCart(){ if(stockQuantity<=0) return false; stockQuantity--; return true; }

public void addToCart(){ stockQuantity++; }

Explanation

This question tests AP Computer Science A skills, specifically abstraction and program design through implementing methods that manage inventory. The addToCart method should check stock availability, reduce stock by one if possible, and return a boolean indicating success. Choice A is correct because it checks if stockQuantity is zero or less, returns false if no stock, otherwise decrements stockQuantity and returns true, properly implementing inventory management. Choice B is incorrect because it increments stock (stockQuantity++) instead of decrementing it, which is the opposite of removing an item from stock for the cart. To help students: Emphasize the importance of checking preconditions (stock > 0) before modifying state and using appropriate operators (-- for decrement). Practice implementing methods that validate before updating and return success indicators. Watch for: using wrong operators (++ vs --) and forgetting to check boundary conditions.