Practice Test 4 - AP Computer Science A
20 questions. Answer them in any order, then submit to see your score and every explanation.
- A shopping cart adds free shipping for large orders. What will be the output if the input totalAmount is 50?
int totalAmount = 50; int shipping = 8; // Free shipping for orders over 75 if (totalAmount > 75) { shipping = 0; } System.out.println(shipping); - A
BankAccountclass has instance variablesbalanceandaccountNumber. Which method would be most appropriate for allowing controlled access to withdraw money, ensuring the account cannot go into negative balance? - Considering the Java class example below, what is the purpose of the constructor?
public class Car { // Fields (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 (behavior) public void start() { /* start engine */ } public void stop() { /* stop engine */ } } // Encapsulation: fields are private; methods are public. // Inheritance: a class can extend another class. // Polymorphism: overridden methods run based on object type.Based on the class structure presented above, what is the constructor's purpose? - Based on the example code, collections require objects, so Integer wraps primitive scores: ```java
ArrayList<Integer> scores = new ArrayList<>();
Integer scoreObj = Integer.valueOf(11);
scores.add(scoreObj);
- How does the order of the algorithmic components potentially cause these algorithms to have different outcomes or efficiencies?
- A programmer is designing a class to model a specific type of laptop computer. The specification notes that all laptops of this model share the same manufacturer name and screen resolution. However, each individual laptop has its own unique serial number and current battery percentage. How should these attributes best be represented in the
Laptopclass? - In this library catalog, what is printed after calling remove and then size?
import java.util.ArrayList; public class LibraryDemo { public static void main(String[] args) { ArrayList<String> books = new ArrayList<String>(); // create catalog books.add("Dune"); books.add("1984"); books.add("Hamlet"); books.remove(1); // remove "1984" System.out.println(books.size()); // number of books left } } - In the code below, what change would you make to print only words containing "ar" anywhere in the string?
import java.util.ArrayList; public class PatternPrintDemo { public static void main(String[] args) { // Set up a collection of strings to iterate over ArrayList<String> words = new ArrayList<String>(); words.add("cat"); words.add("car"); words.add("dog"); words.add("cart"); // Traverse the list by index for (int i = 0; i < words.size(); i++) { // Select only strings that start with "car" if (words.get(i).startsWith("car")) { System.out.println(words.get(i)); } } } } - A program requires a variable to store whether a file has been successfully saved. Which declaration is most suitable for this task?
- A bank app blocks withdrawals that exceed the available balance. Which condition must be met for balance to decrease?
int balance = 200; int withdrawAmount = 150; // Withdraw only if funds are sufficient if (withdrawAmount <= balance) { balance = balance - withdrawAmount; } System.out.println(balance); - In the BankAccount code below, how does constructor chaining work in this example?
public class BankAccount { private String accountNumber; private double balance; // Public default constructor: creates an account with a default balance of 0.0 public BankAccount() { this("000000", 0.0); // Chain to the parameterized constructor } // Public parameterized constructor: initializes both fields from arguments public BankAccount(String accountNumber, double balance) { this.accountNumber = accountNumber; this.balance = balance; } // Private constructor: internal helper for creating a "closed" account private BankAccount(String accountNumber) { this(accountNumber, 0.0); // Chain to the parameterized constructor } } - Consider the following Java code that prints a grid for a pixel-art icon; what is the output?
for (int r = 0; r < 2; r++) { for (int c = 0; c < 3; c++) { System.out.print(r + "," + c + " "); } System.out.println(); } - In this student database, what does the call to remove return?
import java.util.ArrayList; public class StudentDemo { public static void main(String[] args) { ArrayList<String> students = new ArrayList<String>(); students.add("Ava"); students.add("Ben"); students.add("Cara"); String removed = students.remove(1); System.out.println(removed); } } - How does the code modify the ArrayList?
// Part A: Setup ArrayList<Integer> data = new ArrayList<>(); data.add(11); data.add(10); data.add(15); // Part B: Conditional modification for (int i = 0; i < data.size(); i++) { // traverse list int current = data.get(i); if (current > 10) { data.set(i, current * 2); // replace element } } // Part C: Output System.out.println(data); - Consider an ArrayList<Integer> named data. Which of the following code segments will correctly rotate the elements of the list one position to the right (i.e., the last element becomes the first, and all other elements shift one position to the right)?
- In BankAccount, which parameter list would correctly overload withdraw(int dollars) without changing the method name?
- Based on the example code, collections require objects, so Integer wraps primitive scores: ```java
ArrayList<Integer> scores = new ArrayList<>();
Integer bonus = 4;
scores.add(bonus);
- A program needs to read a large text file and find all lines containing a specific keyword. Which approach is most memory-efficient for processing files that may be too large to fit entirely in memory?
- A grade program uses nested if logic. What is grade when score=88 and extraCredit=false?
int score = 88; boolean extraCredit = false; String grade = ""; // Base grade depends on score if (score >= 90) { grade = extraCredit ? "A+" : "A"; } else { // Next tier is B if (score >= 80) grade = "B"; else grade = "C"; } - In a grading system, what will be the output if the input is score = 90?
// Assign letter grade based on score threshold int score = 90; String grade = "F"; if (score >= 90) { grade = "A"; } System.out.println(grade);