Practice Test 12 - AP Computer Science A

20 questions. Answer them in any order, then submit to see your score and every explanation.

  1. A senior developer suggests replacing this design with an Email class containing address, subject, body, and date fields, and using a single List<Email> in EmailSystem. What is the most compelling reason this change would improve the program design?
  2. Based on the shopping cart program below, how does changing taxRate from double to int affect the output? double subtotal = 40.00; double taxRate = 0.0825; double total = Cart.addTax(subtotal, taxRate); System.out.println(total);
  3. What is the result of the following method call given the input deposit 50.0? <pre>public class BankAccount { private double balance; public BankAccount(double startBalance) { balance = startBalance; } public void deposit(double amount) { balance = balance + amount; } public double getBalance() { return balance; } } BankAccount account = new BankAccount(100.0); account.deposit(50.0); double newBalance = account.getBalance(); System.out.println(newBalance);</pre>
  4. A Timer class has instance variables hours, minutes, and seconds. Which method correctly advances the timer by one second, handling rollovers appropriately (60 seconds = 1 minute, 60 minutes = 1 hour)?
  5. Which of the following ArrayLists would cause the method hasDuplicates to return true? I. ["a", "b", "c", "d"] II. ["a", "b", "c", "a"] III. ["a", "a", "b", "c"]
  6. What is printed when the following code segment is executed? Temperature temp = new Temperature(0.0); System.out.println((int)(temp.getFahrenheit())); temp.setCelsius(100.0); System.out.println((int)(temp.getFahrenheit() / 4.0));
  7. A process for grading an essay is described as: "Read the entire essay from beginning to end. If the essay contains any spelling errors, deduct 5 points from the total score." Which combination of algorithmic concepts is represented?
  8. Consider a class that has both instance methods and static methods. Which statement most accurately describes the relationship between these method types and their access to class members?
  9. An e-commerce app totals prices as floats:class Cart { public static int checkoutCents(float priceA, float priceB) { float total = priceA + priceB; int cents = (int) (total * 100); // rounding/truncation risk return cents; } }How can the casting from float to int affect the calculation?
  10. A programmer writes the following comment above a method: // TODO: Optimize database queries and add input validation. Six months later, during code review, this comment is still present above a method that now includes input validation but still has unoptimized database queries. What does this situation BEST illustrate about comment maintenance?
  11. A student prints coordinates for seats in a small theater using nested loops. Given the program below, for (int r = 1; r <= 2; r++) { for (int c = 1; c <= 3; c++) { System.out.print("(" + r + "," + c + ")"); } }What is the output of the following nested loop?
  12. A store tracks weekly sales in an int[] and uses totals for restocking decisions. Consider: A pharmacy tracks daily mask sales in an int[] for a week to decide whether to place a larger order. The manager needs a quick way to update Thursday's sales when a late online order is added, then recompute the total for decision-making. int[] dailySales = {14, 12, 16, 11, 13, 15, 10}; // Add 3 more sales to Thursday (index 3) dailySales[3] += 3; int total = 0; for (int i = 0; i < dailySales.length; i++) { total += dailySales[i]; } Refer to the problem scenario in the passage, what is the outcome of the given code snippet for total?
  13. A student is scanning a seating chart stored in a 2D array. Consider the following Java code. int[][] seats = { {1, 2, 3}, {4, 5, 6} }; for (int r = 0; r < seats.length; r++) { for (int c = 0; c <= seats[r].length; c++) { System.out.print(seats[r][c] + " "); } }Identify the error in the nested loop and suggest a fix.
  14. Which statement correctly declares a double variable named TAXRATETAX_RATE and initializes it to 0.065 in a single line of code?
  15. 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?
  16. What will be the output when the main method is executed? // Student demonstrates class variables (static) and class methods (static). public class Student { // Class variable tracking total enrolled students across all instances. private static int totalEnrolled = 0; // Instance variable indicating whether this student is currently enrolled. private boolean enrolled; // Constructor enrolls the student upon creation. public Student() { enrolled = true; totalEnrolled++; } // Instance method withdraws this student and updates the class total. public void withdraw() { if (enrolled) { enrolled = false; totalEnrolled--; } } // Static method returns total enrolled students. public static int getTotalEnrolled() { return totalEnrolled; } // Main method simulates enrollment and withdrawal. public static void main(String[] args) { Student s1 = new Student(); Student s2 = new Student(); s1.withdraw(); Student s3 = new Student(); System.out.println(Student.getTotalEnrolled()); } }
  17. Consider the following code segment: Counter c1 = new Counter(); Counter c2 = new Counter(); c1.increment(); c1.increment(); c2.increment(); After this code executes, what will c1.getInstanceCount() and Counter.getTotalCount() return?
  18. In a bank account example, an object stores data and methods operate on that data. A method header lists a parameter type and name, and the caller supplies an argument. Example: <pre>public void deposit(double amount) { balance += amount; } ... acct.deposit(15.5); </pre> Refer to the example in the text, what does double amount represent in deposit?
  19. Which of the following statements about the GameSession class is most accurate regarding the relationship between class variables and instance methods?
  20. Problem: Write public static boolean isPalindrome(String text) using a loop that compares mirrored characters. Constraints: length 0 to 1000. Example: "abba" returns true. Given the problem description above, how many iterations will the loop execute for input of length nn?

Practice tests