What this quiz covers
This quiz focuses on Calling Class Methods, giving you a quick way to practice the rules, question types, and explanations that matter most for AP Computer Science a.
Consider the following class for student records. The StudentRecord class has addGrade(double points) and calculateGpa(int numClasses) that returns totalPoints / numClasses. It also has getName() that returns the student name. A program instantiates StudentRecord r = new StudentRecord("Liam"); then calls r.addGrade(9.0). The expected output of calculateGpa depends on the parameter value. If calculateGpa(3) is called, what is the expected result?
AP Computer Science a Quiz
Practice Calling Class Methods in AP Computer Science a with focused quiz questions that help you check what you know, review explanations, and build confidence with test-style prompts.
This quiz focuses on Calling Class Methods, giving you a quick way to practice the rules, question types, and explanations that matter most for AP Computer Science a.
Try each quiz question before looking at the correct answer. Use the explanations to review missed ideas, then come back to similar questions until the pattern feels familiar.
Consider the following class for student records. The StudentRecord class has addGrade(double points) and calculateGpa(int numClasses) that returns totalPoints / numClasses. It also has getName() that returns the student name. A program instantiates StudentRecord r = new StudentRecord("Liam"); then calls r.addGrade(9.0). The expected output of calculateGpa depends on the parameter value. If calculateGpa(3) is called, what is the expected result?
Explanation: This question tests understanding of calling class methods in Java, central to APCSA. Method calls allow interaction with objects by executing predefined actions or calculations, often influencing object state or returning data. In this scenario, a StudentRecord has 9.0 total points after addGrade(9.0), then calculateGpa(3) divides total points by the parameter value. Choice A is correct as it accurately reflects the calculation: 9.0 / 3 = 3.0. Choice C is incorrect because it returns the total points instead of performing the division, misunderstanding the method's purpose. To help students: Distinguish between methods that return stored values versus those that perform calculations. Practice with methods that take parameters to understand how arguments affect return values.
Consider the following class for a library system. The Library class has issueBook() that decreases copies by 1 only when copies > 0, and getCopies() that returns copies. A program instantiates Library lib = new Library("1984", 1); then calls lib.issueBook() twice. The expected output of getCopies() reflects that the second issue may not occur. If getCopies() is called after both calls, what is the expected result?
Explanation: This question tests understanding of calling class methods in Java, central to APCSA. Method calls allow interaction with objects by executing predefined actions or calculations, often influencing object state or returning data. In this scenario, a Library with 1 copy calls issueBook() twice - the first succeeds (reducing to 0), but the second fails because copies must be greater than 0. Choice B is correct as it reflects that only the first issue succeeds: 1 - 1 = 0. Choice A is incorrect because it assumes both issues succeed, resulting in negative copies, which the method prevents. To help students: Trace through multiple method calls with conditional logic step by step. Emphasize understanding preconditions that must be met for methods to execute successfully.
Consider the following class for a library system. The Library class includes addBooks(int n), returnBook() that increases copies by 1, and getCopies(). A program instantiates Library lib = new Library("Hamlet", 0); then calls lib.addBooks(2) and lib.returnBook(). The expected output of getCopies() reflects both method calls. If getCopies() is called next, what is the expected result?
Explanation: This question tests understanding of calling class methods in Java, central to APCSA. Method calls allow interaction with objects by executing predefined actions or calculations, often influencing object state or returning data. In this scenario, a Library object starts with 0 copies, then addBooks(2) increases it to 2, and returnBook() adds 1 more for a total of 3. Choice C is correct as it accurately reflects both method calls: 0 + 2 + 1 = 3 copies. Choice B is incorrect because it only accounts for the addBooks() call, overlooking the returnBook() effect. To help students: Create sequence diagrams showing multiple method calls and their cumulative effects. Use print statements after each method call during practice to verify state changes.
Consider the following class for bank accounts. The BankAccount class includes deposit(double amount) that adds to balance and getBalance() that returns balance. A program instantiates BankAccount a = new BankAccount("Zoe", 10.0); then calls a.deposit(0.5). The expected output of getBalance() reflects the parameter passed. If getBalance() is called next, what is the expected result?
Explanation: This question tests understanding of calling class methods in Java, central to APCSA. Method calls allow interaction with objects by executing predefined actions or calculations, often influencing object state or returning data. In this scenario, a BankAccount starts with balance 10.0, then deposit(0.5) adds 0.5 to the balance. Choice C is correct as it accurately reflects the method's output: 10.0 + 0.5 = 10.5. Choice B is incorrect because it subtracts instead of adds, misunderstanding the deposit method's behavior. To help students: Use clear method names that indicate their action (deposit adds, withdraw subtracts). Practice with decimal values to ensure students understand floating-point arithmetic in method calls.
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?
Explanation: This question tests understanding of calling class methods in Java, central to APCSA. Method calls allow interaction with objects by executing predefined actions or calculations, often influencing object state or returning data. In this scenario, a BankAccount object starts with balance 100.0, then deposit(25.0) adds 25 to make 125.0, and withdraw(40.0) subtracts 40 to make 85.0. Choice B is correct as it accurately reflects the method's output after both operations: 100 + 25 - 40 = 85. Choice C is incorrect because it adds both values instead of subtracting the withdrawal, a common error when students misunderstand method functionality. To help students: Use trace tables to track object state changes step-by-step through method calls. Emphasize the importance of understanding whether methods add, subtract, or otherwise modify instance variables.
Consider the following class for bank accounts. The BankAccount class has methods deposit(double amount) and withdraw(double amount) that change the balance. The withdraw method only subtracts when amount is less than or equal to the current balance. It also has getBalance() that returns the balance. A program instantiates BankAccount b = new BankAccount("Noah", 30.0); then calls b.withdraw(50.0). The expected output of getBalance() reflects that the withdrawal may not occur. If getBalance() is called next, what is the expected result?
Explanation: This question tests understanding of calling class methods in Java, central to APCSA. Method calls allow interaction with objects by executing predefined actions or calculations, often influencing object state or returning data. In this scenario, a BankAccount with balance 30.0 attempts to withdraw(50.0), but the withdraw method only processes when the amount is less than or equal to the current balance. Choice C is correct as the withdrawal fails (50 > 30), so the balance remains 30.0. Choice A is incorrect because it assumes the withdrawal always occurs, resulting in a negative balance, which the method prevents. To help students: Emphasize reading method specifications carefully for conditional behavior. Practice with methods that have validation logic to understand when operations succeed or fail.
Consider the following class for a car rental service. The CarRental class stores how many cars are available. It has methods rentCar(int n) to reduce availability if enough cars exist and returnCar(int n) to increase availability. It also has getAvailable() that returns the current number available. A program instantiates CarRental cr = new CarRental(5); then calls cr.rentCar(2). The expected output of getAvailable() reflects the new availability. If getAvailable() is called next, what is the expected result?
Explanation: This question tests understanding of calling class methods in Java, central to APCSA. Method calls allow interaction with objects by executing predefined actions or calculations, often influencing object state or returning data. In this scenario, a CarRental object starts with 5 available cars, then rentCar(2) is called to reduce availability by 2. Choice B is correct as it accurately reflects the method's output: 5 - 2 = 3 cars remaining available. Choice C is incorrect because it returns the original value, indicating the student didn't account for the state change from rentCar(). To help students: Use debugging exercises where students step through code line by line. Emphasize the difference between getter methods (which don't change state) and mutator methods (which do change state).
Consider the following class for a library system. The Library class tracks how many copies of a title are available. It has methods addBooks(int n) to increase copies and issueBook() to decrease copies by 1 if possible. It also has getCopies() that returns the current number of copies. A program instantiates Library lib = new Library("Dune", 2); then calls lib.issueBook() once. The expected output of getCopies() shows the updated count. If getCopies() is called next, what is the expected result?
Explanation: This question tests understanding of calling class methods in Java, central to APCSA. Method calls allow interaction with objects by executing predefined actions or calculations, often influencing object state or returning data. In this scenario, a Library object starts with 2 copies of 'Dune', then issueBook() is called once, which decreases the count by 1. Choice A is correct as it accurately reflects the method's output: 2 - 1 = 1 copy remaining. Choice B is incorrect because it represents the original count, suggesting the student overlooked that issueBook() modifies the state. To help students: Create visual diagrams showing object state before and after each method call. Practice with concrete examples where students predict outcomes before running code to reinforce understanding of state changes.
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?
Explanation: This question tests understanding of calling class methods in Java, central to APCSA. Method calls allow interaction with objects by executing predefined actions or calculations, often influencing object state or returning data. In this scenario, a StudentRecord object starts with 0 total points, then addGrade(3.0) and addGrade(4.0) are called sequentially, accumulating points. Choice B is correct as it accurately reflects the method's output: 0 + 3.0 + 4.0 = 7.0 total points. Choice A is incorrect because it averages the grades instead of summing them, a misconception about what getTotalPoints() returns. To help students: Distinguish between methods that accumulate values versus those that calculate averages. Use method documentation and naming conventions to clarify method behavior before implementation.
Consider the following class for a car rental service. The CarRental class has rentCar(int n) that only rents if enough cars are available, and getAvailable() to report availability. A program instantiates CarRental cr = new CarRental(1); then calls cr.rentCar(2). The expected output of getAvailable() reflects that the request may fail. If getAvailable() is called next, what is the expected result?
Explanation: This question tests understanding of calling class methods in Java, central to APCSA. Method calls allow interaction with objects by executing predefined actions or calculations, often influencing object state or returning data. In this scenario, a CarRental with 1 available car attempts to rentCar(2), but the method only processes when enough cars are available. Choice C is correct as the rental fails (can't rent 2 when only 1 available), so availability remains 1. Choice B is incorrect because it assumes the partial rental occurs, which typical implementations prevent. To help students: Discuss edge cases and validation in method implementations. Practice tracing through conditional logic within methods to predict when operations will or won't execute.
public class NumberAnalyzer { public static boolean isEven(int n) { return n % 2 == 0; }
public static int digitSum(int n) {
int sum = 0;
n = Math.abs(n);
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
public static boolean isPrime(int n) {
if (n < 2) return false;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}
}
Using the NumberAnalyzer class above, what is the result of this sequence of operations?
int num = -147; int sum = NumberAnalyzer.digitSum(num); boolean evenCheck = NumberAnalyzer.isEven(sum); boolean primeCheck = NumberAnalyzer.isPrime(sum);
Explanation: First, digitSum(-147) takes the absolute value (147) and sums the digits: 1 + 4 + 7 = 12. Then, isEven(12) returns true because 12 % 2 == 0. Finally, isPrime(12) returns false because 12 has divisors other than 1 and itself (e.g., 12 % 2 == 0, so 2 is a divisor). Choice A incorrectly claims 12 is prime. Choice C incorrectly calculates the digit sum as 11. Choice D incorrectly calculates the digit sum as 13.
public class StringProcessor { public static String reverseCase(String str) { String result = ""; for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (Character.isUpperCase(c)) { result += Character.toLowerCase(c); } else if (Character.isLowerCase(c)) { result += Character.toUpperCase(c); } else { result += c; } } return result; }
public static int countVowels(String str) {
int count = 0;
String vowels = "aeiouAEIOU";
for (int i = 0; i < str.length(); i++) {
if (vowels.indexOf(str.charAt(i)) != -1) {
count++;
}
}
return count;
}
}
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);
Explanation: When you encounter string manipulation problems on the AP Computer Science A exam, you need to carefully trace through each method's logic step by step, paying attention to how characters are transformed.
Let's trace through this code systematically. The reverseCase method takes "Hello World!" and flips the case of each letter while leaving non-letters unchanged. Going character by character: 'H' becomes 'h', 'e' becomes 'E', 'l' becomes 'L', and so on. The complete transformation gives us "hELLO wORLD!"
Next, countVowels checks each character in "hELLO wORLD!" against the vowels string "aeiouAEIOU". The method uses indexOf(), which returns -1 if a character isn't found in the vowels string. Scanning through "hELLO wORLD!", we find: 'h' (not a vowel), 'E' (vowel #1), 'L' (not a vowel), 'L' (not a vowel), 'O' (vowel #2), space (not a vowel), 'w' (not a vowel), 'O' (vowel #3), 'R' (not a vowel), 'L' (not a vowel), 'D' (not a vowel), '!' (not a vowel). Total: 3 vowels.
Choice A correctly identifies that case reversal doesn't change vowel count but miscounts the original vowels. Choice B incorrectly identifies 'h' as a vowel—it's not in the vowels string. Choice C suggests all vowels are preserved somehow, which doesn't match our count.
Remember to always trace string methods character by character rather than making assumptions. Method calls that transform strings require careful step-by-step analysis to avoid counting errors.
public class Calculator { public static double multiply(double a, double b) { return a * b; }
public static double divide(double a, double b) {
return a / b;
}
public static double average(double a, double b) {
return divide(Calculator.multiply(a, 2.0) + Calculator.multiply(b, 2.0), 4.0);
}
}
What does Calculator.average(6.0, 10.0) return when using the Calculator class shown above?
Explanation: When you encounter method tracing questions, you need to carefully follow the execution path step by step, paying attention to how methods call other methods within the same class.
Let's trace through Calculator.average(6.0, 10.0). The average method calls Calculator.multiply(a, 2.0) + Calculator.multiply(b, 2.0) in the numerator, then passes that sum to the divide method with 4.0 as the denominator.
First, Calculator.multiply(6.0, 2.0) returns 6.0×2.0=12.0. Then Calculator.multiply(10.0, 2.0) returns 10.0×2.0=20.0. The sum is 12.0+20.0=32.0. Finally, Calculator.divide(32.0, 4.0) returns 32.0÷4.0=8.0.
Choice A correctly identifies this calculation and notes that it does simplify to the average of 6.0 and 10.0. Choice B is wrong because while 8.0 is indeed the arithmetic mean of 6.0 and 10.0, the method doesn't calculate it in the standard way—it uses an unnecessarily complex approach that happens to work. Choice C incorrectly states the method divides by 2.0 instead of 4.0, but if you look at the code, it clearly passes 4.0 to the divide method. Choice D claims the result is 4.0, but our calculation shows it's 8.0.
Study tip: When tracing method calls, write out each step with actual values substituted. Don't assume a method works correctly based on its name—follow the actual code logic to see what it really does.
What happens when the following code is executed?
String str = null; int length = str.length(); System.out.println(length);
Explanation: When str is null and you attempt to call str.length(), a NullPointerException is thrown at runtime because you cannot invoke instance methods on a null reference. Choice A is incorrect because null strings don't have a defined length of 0. Choice B is incorrect because the method call fails before any value can be returned. Choice D is incorrect because this is a runtime error, not a compilation error - the compiler doesn't prevent calling methods on potentially null references.
Consider this method call: Math.pow(Math.abs(-3.7), Math.floor(2.9)). What value does this expression evaluate to?
Explanation: Math.abs(-3.7) returns 3.7 (the absolute value). Math.floor(2.9) returns 2.0 (the largest integer less than or equal to 2.9). Therefore, Math.pow(3.7, 2.0) calculates 3.7², which equals 13.69. Choice A incorrectly states the final calculation result. Choice C has wrong values for both Math.abs(-3.7) and Math.floor(2.9). Choice D incorrectly claims Math.abs(-3.7) returns 4.0.
Which of the following statements about calling static methods is most accurate?
Explanation: Static methods can indeed be called using either the class name (preferred style) or through an instance reference. When called through an instance, the instance is not actually used - the method belongs to the class, not the instance. Java allows both syntaxes without generating compiler warnings, though IDEs may suggest using the class name for clarity. Choice A is incorrect because instance references can be used. Choice B is incorrect because no compiler warning is generated. Choice D is incorrect because even if the instance reference is null, the static method will still execute normally since the instance is not actually used.
What is the result of the following expression?
Math.max(Math.min(15, 8), Math.min(12, Math.max(5, 9)))
Explanation: When you encounter nested function calls like this, the key is to evaluate from the inside out, just like you would with nested parentheses in algebra. Start with the innermost function calls and work your way outward.
Let's trace through this step by step:
First, evaluate the innermost functions:
Now the expression becomes: Math.min(15, 8) returns 8 (the smaller of 15 and 8)Math.max(5, 9) returns 9 (the larger of 5 and 9)Math.max(8, Math.min(12, 9))
Next, evaluate Math.min(12, 9), which returns 9 (the smaller of 12 and 9).
Finally, evaluate Math.max(8, 9), which returns 9 (the larger of 8 and 9).
Choice A correctly identifies that the result is 9 because the outer Math.max compares 8 and 9, returning 9 as the larger value.
Choice B makes the same logical error but incorrectly states that Math.max returns the smaller value instead of the larger one. Choice C contains a calculation error—it claims the final comparison somehow produces 12, but Math.max(8, 9) clearly returns 9, not 12. Choice D fundamentally misunderstands how Math.min works, incorrectly claiming that Math.min(15, 8) returns 15 when it actually returns 8.
Study tip: Always work from inside out with nested function calls, and remember that Math.min returns the smaller value while Math.max returns the larger value. Write out each step to avoid calculation errors.