Calling Class Methods

Help Questions

AP Computer Science A › Calling Class Methods

Questions 1 - 10
1

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?

It causes a syntax error.

It returns 2.

It returns 5.

It returns 3.

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).

2

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?

It returns 1.

It returns 3.

It returns 2.

It causes a syntax error.

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.

3

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?

It returns 6.0.

It returns 9.0.

It returns 3.0.

It causes a syntax error.

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.

4

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?

It causes a syntax error.

It returns 3.5.

It returns 7.0.

It returns 12.0.

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.

5

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?

It returns 0.0.

It causes a syntax error.

It returns 30.0.

It returns -20.0.

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.

6

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?

It returns 2.

It returns 3.

It causes a syntax error.

It returns 1.

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.

7

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?

It causes a syntax error.

It returns 0.

It returns 1.

It returns -1.

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.

8

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?

It returns 0.5.

It returns 10.5.

It returns 9.5.

It causes a syntax error.

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.

9

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?

It returns -1.

It returns 0.

It returns 1.

It causes a syntax error.

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.

10

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?

It causes a syntax error.

It returns 85.0.

It returns 60.0.

It returns 165.0.

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.