Practice Test 3 - AP Computer Science A
20 questions. Answer them in any order, then submit to see your score and every explanation.
- Which of the following statements about calling static methods is most accurate?
- If
Caris a class, which statement best explains the relationship between theCarclass and the concept of inheritance in Java? - Analyze the following code.
// Scenario: counting loop with an incorrect update int count = 1; // initialization while (count <= 10) { // condition System.out.println(count); count--; // update (intended to increment) }Identify the error in the loop logic. - Consider the following class for bank accounts. The BankAccount class stores an owner name and a balance. It has methods deposit(double amount) and withdraw(double amount) that update the balance. It also has getBalance() that returns the current balance. A program instantiates BankAccount a = new BankAccount("Mia", 100.0); then calls a.deposit(25.0) and a.withdraw(40.0). The expected outputs for getBalance() after these calls reflect the updated balance. If getBalance() is called after the transactions, what is the expected result?
- An 'exception' is a specific event that occurs during program execution and disrupts the normal flow of instructions. This event is a type of which broader category of programming error?
- A bank app charges a fee only when an account is overdrawn. What will be the output if the input balance is -5?
int balance = -5; int fee = 0; // Apply overdraft fee when balance is negative if (balance < 0) { fee = 35; } System.out.println(fee); - Considering the Java class example below, what is the purpose of the constructor?
// Car represents a real-world car object. public class Car { // Fields describe state private String make; private String model; private int year; // Constructor public Car(String make, String model, int year) { this.make = make; this.model = model; this.year = year; } // Methods describe behavior public void start() { // start the car } public void stop() { // stop the car } } // Encapsulation keeps fields private and exposes behavior via methods. // Inheritance and polymorphism allow subclasses to reuse and override methods.Based on the class structure presented above, what is the purpose of the constructor in the Java class example? - What is the time complexity of this recursive binary search on a sorted integer array of size ?
public class Searcher { public static int binarySearch(int[] nums, int target, int low, int high) { if (low > high) { return -1; } int mid = (low + high) / 2; if (nums[mid] == target) { return mid; } if (target < nums[mid]) { return binarySearch(nums, target, low, mid - 1); } else { return binarySearch(nums, target, mid + 1, high); } } } - In the library system, checkAvailability returns whether a Book can be lent; method must not expose fields directly. Which method signature correctly implements checkAvailability?
- Problem: Find the first occurrence of pattern in text using a loop (no libraries). Input: text and pattern. Output: first index or -1. Function signature: public static int indexOf(String text, String pattern). Given the problem description above, what is the output of the function when given the input "banana" and "ana"?
- Consider three classes: Product (shown above), Electronics extends Product (in the same package), and Store (in a different package). Which statement about member accessibility is correct?
- Consider the
Counterclass. Which statement best describes theincrementSharedmethod? - Consider this method call sequence:
Math.abs(-5),Integer.parseInt("123"),"hello".charAt(1). What do these demonstrate about the relationship between static methods, instance methods, and object-oriented design principles? - What value is returned when the maximum is found in the array [10, -4, 6, 6, 2] by traversal?
- A student counts duplicate pairs in a list of usernames to find repeats. Given the program below,
String[] names = {"ava", "ben", "ava", "cy"}; int pairs = 0; for (int i = 0; i < names.length; i++) { for (int j = i + 1; j < names.length; j++) { if (names[i].equals(names[j])) pairs++; } } System.out.print(pairs);What is the output of the following nested loop? - Consider this code segment: Point p1 = new Point(10, 20); Point p2 = new Point(10, 20); Point result1 = PointProcessor.processPoint(p1, true); Point result2 = PointProcessor.processPoint(p2, false); Which statement correctly describes the final state?
- Consider a binary search implementation that has a bug: when calculating the middle index, it uses
mid = (low + high) / 2with integer division, but it always rounds up instead of down when there's a remainder. How does this affect the algorithm's behavior when searching for an element that is NOT in the array? - Analyze the following code. How many times will the loop execute?
// Scenario: counting loop prints values from 1 up to 10 int count = 1; // initialize while (count < 10) { // condition System.out.println(count); count++; // update } - A weather monitoring system stores daily temperature readings in an ArrayList<Double> called temperatures. The system needs to identify days with "extreme" temperatures, defined as readings that are more than 2 standard deviations away from the mean. What information must be calculated before identifying extreme temperatures?
- In the AP Computer Science A course, which of the following is considered a reference data type?