What this quiz covers
This quiz focuses on Abstraction And Program Design, giving you a quick way to practice the rules, question types, and explanations that matter most for AP Computer Science a.
A programmer is designing a class to model a specific type of laptop computer. The specification notes that all laptops of this model share the same manufacturer name and screen resolution. However, each individual laptop has its own unique serial number and current battery percentage. How should these attributes best be represented in the Laptop class?
manufacturer and screenResolution as class variables; serialNumber and batteryPercentage as instance variables.manufacturer, screenResolution, serialNumber, batteryPercentage) should be designed as instance variables.manufacturer, screenResolution, serialNumber, batteryPercentage) should be designed as class variables.serialNumber as a class variable; manufacturer, screenResolution, and batteryPercentage as instance variables.AP Computer Science a Quiz
Practice Abstraction And Program Design in AP Computer Science a with focused quiz questions that help you check what you know, review explanations, and build confidence with test-style prompts.
This quiz focuses on Abstraction And Program Design, giving you a quick way to practice the rules, question types, and explanations that matter most for AP Computer Science a.
Try each quiz question before looking at the correct answer. Use the explanations to review missed ideas, then come back to similar questions until the pattern feels familiar.
A programmer is designing a class to model a specific type of laptop computer. The specification notes that all laptops of this model share the same manufacturer name and screen resolution. However, each individual laptop has its own unique serial number and current battery percentage. How should these attributes best be represented in the Laptop class?
manufacturer and screenResolution as class variables; serialNumber and batteryPercentage as instance variables. (correct answer)manufacturer, screenResolution, serialNumber, batteryPercentage) should be designed as instance variables.manufacturer, screenResolution, serialNumber, batteryPercentage) should be designed as class variables.serialNumber as a class variable; manufacturer, screenResolution, and batteryPercentage as instance variables.manufacturer and screenResolution fit this description. Instance variables are used for data that is unique to each instance. serialNumber and batteryPercentage are specific to each individual laptop object, making them ideal instance variables.A programmer designs a single, complex method named generateSalesReport. To improve the design, they break its logic into several smaller, more focused helper methods: fetchSalesData, calculateTotalRevenue, and formatReport. This design strategy is a direct example of which concept?
A programmer designs a method, calculateArea, that computes the area of a rectangle. The initial version only works for a rectangle with a fixed width of 10 and height of 5. Which of the following modifications would best generalize this method using abstraction?
calculateAreaOf10x5Rectangle to make its limited purpose more explicit.width and height parameters to the method so it can calculate the area for any rectangle, not just one specific size. (correct answer)width and height as parameters, the method is no longer tied to a single case and becomes a reusable tool for calculating the area of any rectangle, which is a much more abstract and useful design.A programmer implements a method public static int[] sort(int[] data) that uses an insertion sort algorithm. Later, they discover that a merge sort algorithm would be more efficient for large data sets. They rewrite the internal logic of the sort method to use merge sort but do not change the method signature. Why do other parts of the program that call this sort method not need to be modified?
sort method together with the data it operates on.public static int[] sort(int[] data)) and the overall behavior (sorting an array) remain the same, the internal implementation can be changed without affecting the code that uses it.In the design of a Student class for a university, each student must have a unique ID number. The university also needs to maintain a running total of how many Student objects have been created in the system. Which of the following represents the most appropriate design for these two pieces of data?
Student object needs its own unique value. A class variable (static) is appropriate for the total count because this value is shared across all Student objects and belongs to the class as a whole, not to any single instance.Which statement accurately describes the relationship between an attribute and an instance variable in object-oriented design?
A programmer is designing an online shopping application. The problem description includes the following sentence: "A Customer places an Order, which contains several Products."
Based on standard object-oriented design principles, the italicized nouns in the description are most likely to be modeled as which of the following program components?
ShoppingApplication class, such as customer(), order(), and product().public ShoppingApplication(Customer c, Order o, Product p).Customer, Order, and Product, each with its own attributes and behaviors. (correct answer)main method of the application, used to track the application's current state.Customer, Order, and Product are excellent candidates for becoming distinct classes in the program design.A programmer is designing a Car class. The problem specification states: "Each car has a specific color and a current speed. A car should be able to accelerate to increase its speed and brake to decrease its speed."
Based on this specification, what are the most appropriate attributes (instance variables) for the Car class?
accelerate and brake, which directly modify the car's state.color and currentSpeed, which describe the car's state. (correct answer)Car and the property color, which are fundamental to its identity.accelerate and the property color, representing one behavior and one state.color and currentSpeed. The actions accelerate and brake are behaviors, which would be implemented as methods.A programmer is creating a BankAccount class. The specification states: "Each bank account has an account number and a current balance. A user should be able to deposit money into the account and withdraw money from the account."
Based on this specification, what are the most appropriate behaviors (methods) for the BankAccount class?
accountNumber and balance, which describe the account's state.deposit and the property accountNumber, representing a behavior and a state.deposit and withdraw, which modify the account's state. (correct answer)withdraw and the property balance, representing a behavior and a state.deposit and withdraw are the actions that change the state (the balance) of a BankAccount object.A software team is building a complex inventory management system. Before writing any Java code, they spend time creating diagrams that outline the necessary classes (Product, Warehouse, Shipment), the attributes for each class, and the key methods. Why is this initial design phase crucial for the project's success?
When designing a Circle class, a programmer decides to store the radius as a private instance variable. They also include public methods getArea() and getCircumference(). Why is it generally better to design the class with methods that calculate these values on demand, rather than storing area and circumference as separate instance variables?
getArea() always execute faster than retrieving the value of an instance variable, improving program performance.radius is changed, the values returned by getArea() and getCircumference() will be correct without needing to manually update other variables. (correct answer)area and circumference as instance variables would violate encapsulation and require making the radius variable public for them to be calculated.area and circumference were also stored as instance variables, any method that changed the radius would also have to remember to update both of those values. By calculating them on demand, the class guarantees that the returned values are always consistent with the current radius.The dashboard of a modern car provides a driver with simple controls like a steering wheel, accelerator, and brake pedal, while hiding the immense complexity of the engine, transmission, and electronic systems. This real-world example is an effective analogy for which fundamental computer science concept?
Which of the following is NOT a direct benefit achieved through the use of procedural abstraction and method decomposition in program design?
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.
Consider a Student class where each student has a unique ID that should never change after object creation, a name that can be updated, and a GPA that should only be modified through official grade updates. Which combination of access modifiers and design patterns best supports this abstraction?
A Temperature class is designed to store temperature values and convert between Celsius and Fahrenheit. The internal storage format should be hidden from users, and the class should prevent invalid temperatures below absolute zero (-273.15°C). Which implementation strategy best demonstrates abstraction principles?
A Library class manages a collection of books and needs to support searching by title, author, and ISBN. The internal data structure choice should be hidden from clients. Which design approach best balances abstraction with the need to support multiple search criteria efficiently?
A Clock class represents a 24-hour clock and needs to ensure that hour values stay between 0-23 and minute values between 0-59. The class should support adding minutes and hours. Which implementation detail most significantly impacts the quality of abstraction?
public class ShoppingCart { private double totalPrice; private ArrayList items;
public void addItem(Item item) {
items.add(item);
totalPrice += item.getPrice();
}
public void removeItem(Item item) {
if (items.remove(item)) {
totalPrice -= item.getPrice();
}
}
}
The ShoppingCart class above violates a key principle of abstraction. Which modification best addresses this violation while maintaining good object-oriented design?
ShoppingCart class violates abstraction by maintaining redundant data—the totalPrice field duplicates information that's already contained within the items list. This creates a dangerous situation where the total price could become out of sync with the actual items if any modification occurs outside the provided methods or if bugs exist in the update logic.
Answer A correctly addresses this by eliminating the redundant totalPrice field and calculating totals dynamically from the authoritative source—the items themselves. This ensures the total is always accurate and removes the possibility of inconsistent state.
Answer B makes the problem worse by exposing internal implementation details and violating encapsulation. Making fields public breaks the abstraction barrier that protects clients from implementation changes.
Answer C acknowledges the synchronization problem but doesn't solve it—it just shifts responsibility to clients to maintain data consistency, which violates good encapsulation principles.
Answer D introduces even more redundancy by creating a second data structure that must stay synchronized with the items list, multiplying the potential for inconsistency.
Study tip: On AP Computer Science A, watch for design questions that test whether you can identify redundant data storage. The best solution almost always involves maintaining a single authoritative source of information rather than trying to keep multiple copies synchronized.A programmer is designing a BankAccount class that must enforce the principle that account balances can never go below zero. The class should allow deposits, withdrawals (only if sufficient funds exist), and balance inquiries. Which design approach best demonstrates proper abstraction and encapsulation for this requirement?