Practice Test 10 - AP Computer Science A

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

  1. Based on the example code, collections store objects, so scores use Integer wrappers; Java boxes and unboxes as needed. import java.util.ArrayList; public class ScoreTracker { public static void main(String[] args) { ArrayList<Integer> scores = new ArrayList<>(); scores.add(4); Integer y = scores.get(0); int z = y; // unboxing System.out.println(z); } }How does autoboxing work in the given example?
  2. In the Book code below, how does constructor chaining work in the provided example? public class Book { private String title; private String author; // Public default constructor: provides standard defaults public Book() { this("Untitled", "Unknown"); // Chain to parameterized constructor } // Public parameterized constructor: initializes both fields public Book(String title, String author) { this.title = title; this.author = author; } // Private constructor: internal helper for author-only private Book(String author) { this("Untitled", author); // Chain to parameterized constructor } }
  3. A programmer creates a subclass called SavingsAccount that extends BankAccount. Within the SavingsAccount class methods, which BankAccount members can be directly accessed without using getter/setter methods?
  4. Which parameter type is appropriate for convertTemperature when converting Celsius to Fahrenheit and returning a decimal result?
  5. To find the page with the most consistent traffic, the team should calculate which statistical measure for each page, and what programming approach would be most efficient?
  6. To identify the most frequently purchased item in the last month, which of the following analytical processes is most appropriate?
  7. What is the output of this inventory code after removing the sold-out product?import java.util.ArrayList; public class RemoveSoldOut { public static void main(String[] args) { ArrayList<String> products = new ArrayList<String>(); products.add("Apples"); products.add("Bananas"); products.add("Carrots"); products.remove(2); System.out.println(products.size() + " " + products.get(1)); } }
  8. A city's animal shelter wants a program to identify all dogs that have been at the shelter for more than 90 days. Which of the following data sets would be sufficient to solve this problem?
  9. An algorithm is designed that iterates through the entire list of movies. It initializes a counter to zero. For each movie it examines, if the movie's genre is "Documentary" and its rating is greater than 4, the algorithm increments the counter. What question does this algorithm answer?
  10. Consider the following class; how do you create an instance of BankAccount? public class BankAccount { private String owner; private double balance; // BankAccount represents a simple account that tracks an owner's balance. public BankAccount(String owner, double startingBalance) { this.owner = owner; balance = startingBalance; } // deposit increases the balance. public void deposit(double amount) { balance += amount; } } class Main { public static void main(String[] args) { // The account reference stores the new object, then deposit manipulates its state. // Example use: acct.deposit(50.0); } } ```​
  11. Which of the following methods correctly implements a binary search algorithm for a sorted ArrayList<Integer>? The method should return the index of the target value, or -1 if not found.
  12. Based on the provided 2D array [[2,5,9],[1,4,7]], what is the resulting matrix after transposing the array into a new 2D array?
  13. A transportation agency has a data set containing the start time, end time, and GPS coordinates for every public bus trip on a particular day. An analyst writes a program that processes this data set by calculating the duration of each trip and counting how many trips exceed 30 minutes. Which question is this program designed to answer?
  14. In a class definition, when should instance variables be declared as static, and what is the primary consequence of this decision?
  15. What is the purpose of the comments in the provided BankAccount code snippet overall?
  16. Refer to the code below. How does the condition in the loop affect its execution? public class EvenSumBoundary { public static void main(String[] args) { int[] numbers = {1, 2, 3, 4}; int evenSum = 0; // Loop condition uses <= instead of < for (int index = 0; index <= numbers.length - 1; index++) { if (numbers[index] % 2 == 0) { evenSum += numbers[index]; } } System.out.println("Even sum: " + evenSum); } }
  17. In this BankAccount code, how do the comments enhance the readability and maintainability of the program?
  18. A class stores daily step counts in a 2D array (weeks by days). Given the program below, int[][] steps = { {1000, 2000, 3000}, {4000, 5000, 6000} }; int max = steps[0][0]; for (int r = 0; r < steps.length; r++) { for (int c = 0; c < steps[r].length; c++) { if (steps[r][c] > max) { max = steps[r][c]; } } } System.out.println(max);What is the output of the following nested loop?
  19. In the BankAccount code below, identify the function of the Javadoc comments in this code. /** * Models a bank account with basic operations. * Javadoc is used so documentation tools can generate an API reference. */ public class BankAccount { private double balance; /** * Deposits funds into the account. * * @param amount the deposit amount; must be positive */ public void deposit(double amount) { if (amount <= 0) { // Ignore invalid deposits rather than corrupting the balance. return; } balance += amount; } /** * Withdraws funds if the balance is sufficient. * * @param amount the withdrawal amount; must be positive * @return true when the withdrawal is applied */ public boolean withdraw(double amount) { if (amount <= 0) { return false; } // Check first, then update, to keep state changes predictable. if (balance < amount) { return false; } balance -= amount; return true; } }
  20. Which code snippet correctly overloads factorial by parameter type, not by changing only the return type?

Practice tests