Practice Test 11 - AP Computer Science A
20 questions. Answer them in any order, then submit to see your score and every explanation.
- The
setLocationmethod is intended to update thexandyinstance variables to the values of thexandyparameters. Which of the following code segments will NOT work as intended? - An algorithm is designed to count how many emails were received from the sender "newsletter@example.com". What is the fundamental operation the algorithm must perform on the data set?
- Explain the result of calling remove with an int on this friends list.
import java.util.ArrayList; public class RemoveDemo { public static void main(String[] args) { ArrayList<String> friends = new ArrayList<String>(); friends.add("Lia"); friends.add("Max"); friends.add("Nia"); friends.remove(1); // remove by index System.out.println(friends.get(1)); } } - Based on the code snippet above, which describes the state of acct after depositAndReturn executes?
public class BankAccount { private String accountNumber; private double balance; public BankAccount(String accountNumber, double balance) { this.accountNumber = accountNumber; this.balance = balance; } // Adds funds to this account public void deposit(double amount) { balance += amount; } // Deposits then returns this same account reference public BankAccount depositAndReturn(double amount) { deposit(amount); return this; } public double getBalance() { return balance; } public static void main(String[] args) { BankAccount acct = new BankAccount("A200", 100.0); BankAccount ref = acct.depositAndReturn(40.0); ref.deposit(10.0); System.out.println(acct.getBalance()); } } - A programmer suggests that the
isAuthormethod can be rewritten asreturn author.equals(author);. Which statement best evaluates this suggestion? - Consider the following class for student records. The StudentRecord class stores a student name and total grade points. It has methods addGrade(double points) to add to total points and getTotalPoints() to return the total. It also has printTranscript() that prints the student name and total points. A program instantiates StudentRecord s = new StudentRecord("Ava"); then calls s.addGrade(3.0) and s.addGrade(4.0). The expected output for getTotalPoints() reflects both additions. If getTotalPoints() is called next, what is the expected result?
- A teacher wants to compute each student's total points from a 2D array of assignments. Consider the following Java code.
int[][] pts = { {5, 4, 6}, {3, 7, 2} }; int[] totals = new int[pts.length]; for (int r = 0; r < pts.length; r++) { for (int c = 0; c < pts[r].length; c++) { totals[r] += pts[r][c]; } } System.out.print(totals[0] + "," + totals[1]);What is the output of the following nested loop? - An
ArrayListofIntegerobjects,list, initially contains{10, 8, 4, 6}. If a standard insertion sort algorithm is applied tolist, what is the content oflistafter the second pass of the outer loop completes? - Given the StringProcessor class above, what is the output of the following code? String original = "Hello World!"; String modified = StringProcessor.reverseCase(original); int vowelCount = StringProcessor.countVowels(modified); System.out.println(vowelCount);
- A hybrid sorting algorithm uses insertion sort for subarrays of size 5 or smaller, and quicksort for larger arrays. Given an array of 12 elements that gets partitioned by quicksort into subarrays of sizes 4, 1, and 7, which statement best describes what happens next in the sorting process?
- Analyze the following code.
// Counting from 1 to 10 using a while loop int counter = 1; // initialization while (counter <= 10) { // condition controls repetition System.out.println(counter); // iterate through the sequence 1..10 counter++; // update }What will be the output of the given code? - What change to the nested loop would cause the program to print each row in reverse order?
int[][] nums = { {1, 2, 3}, {4, 5, 6} }; for (int r = 0; r < nums.length; r++) { for (int c = 0; c < nums[r].length; c++) { System.out.print(nums[r][c]); } System.out.println(); } - How do the comments enhance the readability and maintainability of this LibraryManager book-search method?
- A senior developer is reviewing code comments written by junior team members. They notice that some methods have extensive comments explaining obvious operations, while complex algorithmic methods have minimal documentation. This situation MOST likely indicates that the junior developers need guidance on:
- What is the purpose of the comments in the provided StudentRecord code snippet?
- A class contains these two method signatures:
public void process(String data)andpublic void process(String[] data). When the statementprocess(null)is executed, what will happen? - Given the code below, which line of code correctly creates an object of Customer?
public class Customer { private String name; // Constructor creates a customer with a stored name. public Customer(String name) { this.name = name; } // getName returns the customer's name. public String getName() { return name; } } class Order { private Customer customer; // Constructor stores a reference to a Customer object. public Order(Customer customer) { this.customer = customer; } // printReceipt uses the nested object's method. public void printReceipt() { System.out.println("Customer: " + customer.getName()); } public static void main(String[] args) { // Objects are instantiated and then used together. // Example method invocation on an instantiated object: // order.printReceipt(); } } - Palindrome Checker: What will be the output if the string is "Level"?
String s = input.toUpperCase(); char first = s.charAt(0); char last = s.charAt(s.length() - 1); System.out.println(first == last); - A store tracks inventoryCount for one item; transactions update stock until a sentinel ends input. Inputs are starting inventoryCount and a list of (type, amount) transactions; output is final inventoryCount. Example input: start 10, BUY 3, RETURN 2, END; output 9. Example input: start 1, BUY 2, END; output 1.
int inventoryCount = startCount; while (true) { String type = getType(); // "BUY", "RETURN", or "END" if (type.equals("END")) break; int amount = getAmount(); if (type.equals("BUY")) { if (amount <= inventoryCount) { inventoryCount -= amount; } } else if (type.equals("RETURN")) { inventoryCount += amount; } } System.out.println(inventoryCount);Consider the algorithm for inventory updates; which part ensures stock never becomes negative after a BUY? - In BankAccount, which parameter list would correctly overload getBalance() while keeping the same method name?