Practice Test 8 - AP Computer Science A
20 questions. Answer them in any order, then submit to see your score and every explanation.
- In the code below, how does the loop condition i < numbers.length affect the loop's execution?
- Analyze the following code. What will be the output of the given code?
// Scenario: conditional loop finds a value, then reports index int[] data = {4, 7, 7, 9}; int pos = 0; // initialize while (pos < data.length && data[pos] != 7) { pos++; // update } System.out.println(pos); - Which of the following statements best describes the divide-and-conquer strategy used by the merge sort algorithm?
- A recursive method counts how many times a number can be halved before reaching 1, for input size .
public static int halveCount(int n) { if (n <= 1) { return 0; } else { return 1 + halveCount(n / 2); } }What is the run-time complexity of the algorithm in the code snippet? - A programmer is building an application that needs to interact with a remote database over a network. What is the most appropriate and common practice for the programmer to take?
- What happens when this code executes? Account acc1 = new Account("Alice", 100.0); Account acc2 = new Account("Bob", 50.0); Account result = BankUtils.transfer(acc1, acc2, 150.0);
- Palindrome Checker: What will be the output if the string is "Level"?
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("Level")); - What information do the inline comments provide in this SimpleCalculator divide method's error handling?
- A programmer designs a method,
calculateArea, that computes the area of a rectangle. The initial version only works for a rectangle with a fixed width of 10 and height of 5. Which of the following modifications would best generalize this method using abstraction? - An algorithm performs a flood fill operation on a 2D character array, replacing all connected cells of the same character with a new character. Using a recursive approach, which modification would be most effective in preventing stack overflow for large connected regions?
- A developer is planning to write a program to manage student grades. The data set includes each student's name, ID number, and a list of assignment scores. Before writing the code, the developer creates a table with columns for "Name," "ID," and "Scores." What is the main benefit of this action?
- Based on the student grade manager below, which data type is best suited for studentName and why?
String studentName = "Ava"; GradeBook gb = new GradeBook(studentName); gb.addScore(90); gb.addScore(85); System.out.println(gb.getAverage()); - In a shopping cart discount system, what will be the output if the input is totalAmount = 120?
// Apply a 10% discount for orders over $100 int totalAmount = 120; int discountPercent = 0; if (totalAmount > 100) { discountPercent = 10; } System.out.println(discountPercent); - A programmer writes a program that successfully compiles without any issues. What can be concluded with certainty from this fact?
- Consider two scenarios: (1) A Manager class extends Employee in the same package, and (2) A PayrollSystem class in a different package. What is true about constructor and method accessibility?
- Analyze the following code.
// Scenario: process an array until a target is found int[] a = {1, 3, 5, 7, 9}; int index = 0; // initialization while (index < a.length && a[index] != 7) { // condition System.out.println(a[index]); // iterate through sequence index++; // update }What will be the output of the given code? - In the LibraryManager code below, how do the comments enhance the readability and maintainability of the code?
import java.util.ArrayList; import java.util.List; /** * A tiny library catalog storing author names alongside titles. * Comments highlight design intent for educational clarity. */ public class LibraryManager { private final List<String> entries = new ArrayList<>(); /** * Adds a book entry in the format "Title - Author". * * @param title the book title * @param author the author name * @return true if the entry is added */ public boolean addBook(String title, String author) { if (title == null || author == null) { // Both fields are required to keep entries consistent. return false; } String t = title.trim(); String a = author.trim(); if (t.isEmpty() || a.isEmpty()) { // Avoid storing incomplete records. return false; } entries.add(t + " - " + a); return true; } /** * Returns all entries that contain the given text. * * @param query the text to search for * @return a list of matching entries */ public List<String> search(String query) { List<String> results = new ArrayList<>(); if (query == null) { return results; } String needle = query.trim().toLowerCase(); for (String entry : entries) { // Normalize case so searches behave consistently for users. if (entry.toLowerCase().contains(needle)) { results.add(entry); } } return results; } } - A teacher stores quiz scores in an array of integers. Given the code snippet,
public class Grades { public static void main(String[] args) { int[] grades = new int[5]; // 5 students grades[0] = 90; grades[1] = 85; } }The array stores whole-number scores. How is the array grades initialized in the code? - Consider the
Bookclass shown. The constructor is intended to initialize the instance variablestitleandauthorwith the values of the parameters that have the same names. Which of the following can replace/* missing code */so that the constructor works as intended? - In this class, which method call is valid from main? ```java
public class MathUtil {
private static int factorialIter(int n){
int prod = 1;
for(int i=2;i<=n;i++) prod *= i; // multiply up
return prod;
}
public static int factorial(int n){
return factorialIter(n); // delegate
}
}