Practice Test 5 - AP Computer Science A
20 questions. Answer them in any order, then submit to see your score and every explanation.
- Consider the following code segment: Circle c1 = new Circle(); Circle c2 = new Circle(3.0); Circle.setDefaultRadius(5.0); Circle c3 = new Circle(); c1.setRadius(2.0); What are the radii of c1, c2, and c3 after this code executes?
- If this method is called on an ArrayList containing ["a", "b", "b", "c", "c", "c", "d"], what will the ArrayList contain after the method executes?
- Consider the expression
a || b && c || d. Given Java's operator precedence rules, how is this expression grouped, and what would be an equivalent fully parenthesized version? - Given the code below, which line of code correctly creates an object of ArrayList?
import java.util.ArrayList; class Main { public static void main(String[] args) { // ArrayList stores a dynamic list of scores. // The list reference stores the object, then add manipulates its contents. // Example use: scores.add(100); } } ``` - Which statement correctly describes the base case and recursive case in this binary search?
int bSearch(int[] a, int target, int lo, int hi) { if (lo > hi) return -1; // base case int mid = (lo + hi) / 2; if (a[mid] == target) return mid; // base case if (target < a[mid]) return bSearch(a, target, lo, mid-1); return bSearch(a, target, mid+1, hi); } ``` - The implementation of the
addMoneymethod is incorrect because it does not meet its postcondition. Why? - Based on the class design for the online store, how does encapsulation benefit the design of the Product class with private price and stockQuantity fields?
- A club tracks points in a 2D array and wants row totals. Given the program below,
int[][] pts = { {2, 1, 3}, {4, 0, 2} }; for (int r = 0; r < pts.length; r++) { int sum = 0; for (int c = 0; c < pts[r].length; c++) { sum += pts[r][c]; } System.out.print(sum + " "); }What is the output of the following nested loop? - Based on the example code, collections require objects, so Integer wraps an int: ```java
ArrayList<Integer> scores = new ArrayList<>();
scores.add(7); // autoboxing
scores.add(12);
- If five Course objects are created with enrollments of 25, 30, 35, 40, and 45 respectively, and then two more Course objects are created with enrollments of 20 and 50, what will be the return value of Course.getAverageEnrollment()?
- Implement an algorithm to sort the following array in ascending order using insertion sort (no built-in sort): int[] a. Constraints: . After sorting, a must be in nondecreasing order. Example: [4,1,3] -> [1,3,4]. Which method correctly sorts the array?
- 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, andformatReport. This design strategy is a direct example of which concept? - A programmer is designing a
Rectangleclass with instance variableslengthandwidth. Which method header would be most appropriate for a method that calculates and returns the area while ensuring the method can be called without modifying the object's state? - Refer to the BankAccount class code below: what value will the field balance have after executing new BankAccount()?
public class BankAccount { private String accountNumber; private double balance; // Public default constructor: sets a default account and zero balance. public BankAccount() { this("000000", 0.0); } // Public parameterized constructor: initializes both fields. public BankAccount(String accountNumber, double balance) { this.accountNumber = accountNumber; this.balance = balance; } // Private constructor: blocks constructing with only a balance. private BankAccount(double balanceOnly) { this("000000", balanceOnly); } } - The developer wants to determine if the level's difficulty is well-balanced. Which of the following data analysis tasks would be most useful for this purpose?
- Consider the Student class shown above. Which of the following statements about accessing the class members from outside the Student class is correct?
- Consider the following code segment: Timer t = new Timer(3); t.tick(); t.tick(); boolean expired1 = t.isExpired(); t.tick(); boolean expired2 = t.isExpired(); t.addTime(5); int timeLeft = t.getTimeLeft(); What are the values of expired1, expired2, and timeLeft?
- An e-commerce website has a data set of all customer transactions. An algorithm is designed to process this data to find the total revenue for the month of December. Which statement accurately describes the relationship between the data set and the algorithm?
- Considering the Java class example below, which access modifier would you use for owner to ensure encapsulation?
public class BankAccount { // Fields represent state private String owner; private double balance; public BankAccount(String owner, double startingBalance) { this.owner = owner; balance = startingBalance; } public String getOwner() { return owner; } } // Encapsulation: hide fields, expose methods. // Inheritance: subclasses can extend BankAccount. // Polymorphism: overridden methods can change behavior.Based on the class structure presented above, which modifier best fits owner? - In a library system, a Book class stores title and author while tracking whether it is checked out. The title and author should not be rewritten by patrons, but borrowBook and returnBook must be callable by the front desk app. A protected method calculateOverdueFee helps internal charge tracking. What access modifier should be used for the borrowBook method in this scenario?