Practice Test 7 - AP Computer Science A

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

  1. What is the value of finalResult after the code executes?
  2. Given the task requirements, which condition will correctly exit the while loop under described circumstances? Problem description (Game Logic) A Java guessing game picks a secret integer from 1 to 100. The player has at most maxAttempts guesses. After each guess, the program prints feedback using selection: - if guess is too low: print Too low - else if guess is too high: print Too high - else: print Correct The game uses iteration to repeatedly prompt until the player guesses correctly or runs out of attempts. Input - Line 1: secret (for testing) - Line 2: maxAttempts - Next lines: guesses (one per line) Output - Feedback after each guess - End message: Win if correct, otherwise Lose Constraints - 1≤secret≤1001 \le secret \le 100 - 1≤maxAttempts≤101 \le maxAttempts \le 10 Example Input: 42 3 10 50 42 Output: Too low Too high Correct Win Loop skeleton:int attempts = 0; while (/* condition */) { // read guess, attempts++ // print feedback }
  3. A Car class has methods: void service(String serviceType) records a service entry; int getMileage() returns mileage. Car car = new Car("Accord", 10000) exists. Which method call correctly records an oil change for car?
  4. A programmer needs to use the binary search algorithm on a list of items. What is a necessary precondition for the list of items?
  5. Palindrome Checker: What will be the output if the string is "Race car"? public class PalTool { public static String check(String word) { word = word.replace(" ", ""); String lower = word.toLowerCase(); String ends = lower.substring(0, 1).toUpperCase() + lower.substring(lower.length() - 1); boolean isPal = true; for (int i = 0; i < lower.length() / 2; i++) { if (lower.charAt(i) != lower.charAt(lower.length() - 1 - i)) { isPal = false; } } return (isPal ? "PAL:" : "NOT:") + ends; } } // Test case: System.out.println(PalTool.check("Race car"));
  6. Consider the compound condition: (x % 2 == 0 && x > 0) || (x % 3 == 0 && x < 0). For which value of x would this expression be false?
  7. Based on the documentation, which of the following are considered behaviors of a Robot object?
  8. Given the following array, find the index of the first occurrence of target using binary search on a sorted array that may contain duplicates. Input: sorted int[] a (0≤n≤1050 \le n \le 10^5), int target. Output: smallest index i with a[i]==target, else -1. Example: a=[1,2,2,2,5], target=2 -> 1. Which method is correct?
  9. The recursive method above is part of a sorting algorithm. Based on the structure shown, which statement best describes the time complexity and the role of the combine method?
  10. A bank account program stores balance inside each object and uses methods to update it. The balance field is private, so other classes cannot access it directly: private double balance; Based on the passage, why is making balance private consistent with encapsulation?​
  11. Consider the following class for a car rental service. The CarRental class stores how many cars are available. It has methods rentCar(int n) to reduce availability if enough cars exist and returnCar(int n) to increase availability. It also has getAvailable() that returns the current number available. A program instantiates CarRental cr = new CarRental(5); then calls cr.rentCar(2). The expected output of getAvailable() reflects the new availability. If getAvailable() is called next, what is the expected result?
  12. The setScore method is intended to update an instance variable named score. Why does this implementation fail to meet its documented postcondition?
  13. Given the Counter class above, what will be printed by the following code? Counter c1 = new Counter(); Counter c2 = new Counter(); Counter.incrementGlobal(); c1.incrementInstance(); c2.incrementInstance(); System.out.println(Counter.getGlobalCount() + " " + c1.getInstanceCount() + " " + c2.getInstanceCount());
  14. A BankAccount class has methods: void deposit(double amount) adds to balance; double getBalance() returns balance. BankAccount acct = new BankAccount(75.0) starts with $75. Which method call correctly performs depositing $25 into acct?
  15. How does the initialization int i = 0 affect which array element is processed first?
  16. A research team analyzes survey response times stored in an ArrayList<Integer> called responseTimes (in seconds). They want to remove outliers defined as response times that are either less than 10 seconds or greater than 300 seconds. After removing outliers, what additional consideration is necessary for further statistical analysis?
  17. How does the static method interact with the class variable in this code? // BankAccount demonstrates class variables (static) and class methods (static). public class BankAccount { // Class variable shared by all accounts in the bank. private static double bankTotalBalance = 0.0; // Instance variable unique to each BankAccount object. private double accountBalance; // Constructor initializes an account with an opening deposit. public BankAccount(double openingDeposit) { accountBalance = openingDeposit; // Update the class-level total whenever an account is created. bankTotalBalance += openingDeposit; } // Static (class) method returns the total balance across all accounts. public static double getBankTotalBalance() { return bankTotalBalance; } // Instance method deposits money into this account and updates the class total. public void deposit(double amount) { accountBalance += amount; bankTotalBalance += amount; } // Instance method withdraws money from this account and updates the class total. public void withdraw(double amount) { accountBalance -= amount; bankTotalBalance -= amount; } // Main method demonstrates creating accounts and adjusting balances. public static void main(String[] args) { BankAccount a = new BankAccount(100.0); BankAccount b = new BankAccount(50.0); a.deposit(25.0); b.withdraw(10.0); System.out.println(BankAccount.getBankTotalBalance()); } }
  18. In the performMaintenance method of the Car class, which of the following statements about accessing Vehicle class members is true?
  19. In the context of writing this paper, which of the following would be considered a data set that the student might analyze to support their arguments?
  20. After all operations are completed, what is the final state of the ArrayList and what value does list.indexOf("cherry") return?

Practice tests