Calling Instance Methods

Help Questions

AP Computer Science A › Calling Instance Methods

Questions 1 - 10
1

A Book class has methods: void checkOut(); void returnBook(); boolean isCheckedOut(). Book b = new Book("1984") starts checked out. How does calling b.returnBook() affect its state?

returnBook returns false

checkedOut becomes false

checkedOut becomes true

b.returnBook(true) sets it available

Explanation

This question tests the ability to correctly use objects and call instance methods on them in Java, aligning with AP Computer Science A standards. The returnBook() method changes the internal checkedOut state of a Book object from true (checked out) to false (available). Since the book starts in a checked out state (checkedOut = true), calling returnBook() will set it back to false. Choice B is correct because returnBook() sets the checkedOut field to false, making the book available again. Choice A is incorrect because it suggests the opposite effect - returnBook makes books available (false), not checked out (true). To help students: Use consistent naming conventions to understand method purposes. Create state diagrams showing how methods transition objects between different states.

2

A Car class has methods: void service(String serviceType) records a service entry; int getMileage() returns mileage. Car car = new Car("Accord", 10000) exists. Which method call correctly records an oil change for car?

car.service("oil change");

car.service();

car.service = "oil change";

service(car, "oil change");

Explanation

This question tests the ability to correctly use objects and call instance methods on them in Java, aligning with AP Computer Science A standards. Instance methods are called using dot notation on object references, and parameters must match the method signature exactly. The service method expects a String parameter describing the service type. Choice A is correct because car.service("oil change") properly calls the instance method with the required String parameter using correct dot notation. Choice B is incorrect because it attempts to use static method syntax for what is clearly an instance method that operates on a specific car object. To help students: Review the difference between instance and static method call syntax. Practice matching method calls to their signatures, paying attention to parameter types.

3

A Book class has methods: void checkOut() sets checkedOut to true; void returnBook() sets checkedOut to false; boolean isCheckedOut() returns checkedOut. Book novel = new Book("Dune") starts not checked out. How does calling novel.checkOut() affect its state?

checkOut returns true

checkedOut becomes true

novel.checkOut(true) is required

checkedOut becomes false

Explanation

This question tests the ability to correctly use objects and call instance methods on them in Java, aligning with AP Computer Science A standards. Instance methods like checkOut() modify the internal state of objects - in this case, changing the checkedOut boolean field from false to true. The Book object starts with checkedOut as false (not checked out), and calling checkOut() sets this field to true. Choice B is correct because calling novel.checkOut() changes the internal checkedOut state to true, indicating the book is now checked out. Choice A is incorrect because it reverses the effect - checkOut makes a book checked out (true), not available (false). To help students: Use real-world analogies to understand state changes. Practice tracing object state through method calls using diagrams showing before and after states.

4

A BankAccount class has methods: void deposit(double amount) adds to balance; double getBalance() returns balance. BankAccount acct = new BankAccount(75.0) starts with $75. Which method call correctly performs depositing $25 into acct?

acct.deposit();

deposit(acct, 25.0);

acct.deposit = 25.0;

acct.deposit(25.0);

Explanation

This question tests the ability to correctly use objects and call instance methods on them in Java, aligning with AP Computer Science A standards. Instance methods must be called using dot notation on the object reference, with the correct parameters matching the method signature. The deposit method expects a double parameter representing the amount to deposit. Choice B is correct because acct.deposit(25.0) uses proper dot notation to call the instance method on the acct object with the required parameter. Choice A is incorrect because it uses incorrect syntax that treats deposit as a static method rather than an instance method. To help students: Emphasize the object.method(parameters) syntax pattern. Practice identifying when to use instance methods (on objects) versus static methods (on classes).

5

A BankAccount class has methods: boolean withdraw(double amount) returns false and changes nothing if amount > balance; double getBalance(). BankAccount acct = new BankAccount(40.0). How does calling acct.withdraw(60.0) affect its state?

Balance becomes 0.0 and returns false

Balance stays 40.0 and returns false

Balance becomes -20.0 and returns true

Balance stays 40.0 and returns 40.0

Explanation

This question tests the ability to correctly use objects and call instance methods on them in Java, aligning with AP Computer Science A standards. The withdraw method includes validation logic: it only processes the withdrawal if the requested amount is less than or equal to the current balance. When attempting to withdraw $60 from an account with only $40, the condition (60.0 <= 40.0) is false, so the method returns false and leaves the balance unchanged. Choice B is correct because withdraw(60.0) returns false and keeps the balance at 40.0 when there are insufficient funds. Choice A is incorrect because Java banking implementations never allow negative balances - the method protects against overdrafts. To help students: Trace through conditional logic carefully. Emphasize that validation in methods prevents invalid state changes.

6

A Student class has methods: void addGrade(double grade) appends a grade; double calculateGPA() returns average grade divided by 25.0. Student s = new Student("Ava") has grades 80.0 and 90.0 already added. What is returned when s.calculateGPA() is called with no parameters?

3.4

85.0

170.0

It returns void

Explanation

This question tests the ability to correctly use objects and call instance methods on them in Java, aligning with AP Computer Science A standards. The calculateGPA() method computes the average of stored grades and divides by 25.0 to produce a GPA on a 4.0 scale. With grades 80.0 and 90.0, the average is (80.0 + 90.0) / 2 = 85.0, then 85.0 / 25.0 = 3.4. Choice A is correct because calculateGPA() returns 3.4, which represents the GPA calculation (85.0 / 25.0). Choice B is incorrect because it returns the average grade (85.0) without the GPA conversion division by 25.0. To help students: Break down complex calculations into steps. Emphasize reading method documentation carefully to understand return value calculations, not just assuming standard behavior.

7

A Student class has methods: void addGrade(double grade) adds one grade; double calculateGPA() returns average grade divided by 25.0. Student s = new Student("Noah") has no grades yet. How does calling s.addGrade(100.0) affect its state?

It sets GPA to 4.0 immediately

It returns the new GPA as a double

It adds 100.0 as a stored grade

It replaces the student name with 100.0

Explanation

This question tests the ability to correctly use objects and call instance methods on them in Java, aligning with AP Computer Science A standards. The addGrade method is a mutator that stores individual grades in the Student object, likely in an internal list or array. It doesn't calculate or return anything - it simply adds the grade to the student's collection of grades for later GPA calculation. Choice B is correct because addGrade(100.0) stores 100.0 as a grade in the student's internal grade collection without performing any calculations. Choice A is incorrect because addGrade only stores grades; it doesn't calculate or set the GPA directly. To help students: Distinguish between methods that store data versus those that calculate results. Emphasize understanding method names and their implied functionality.

8

A Book class has methods: void checkOut(); boolean isCheckedOut() returns status. Book book = new Book("Hamlet") starts not checked out. What will be the output after calling book.isCheckedOut() immediately after book.checkOut()?

true

"true"

false

It prints nothing because isCheckedOut is void

Explanation

This question tests the ability to correctly use objects and call instance methods on them in Java, aligning with AP Computer Science A standards. The sequence of method calls first changes the book's state with checkOut(), then queries that state with isCheckedOut(). After checkOut() is called, the book's checkedOut field becomes true, so the subsequent isCheckedOut() call returns this boolean value. Choice A is correct because after calling checkOut(), the book is in a checked-out state, so isCheckedOut() returns the boolean value true. Choice B is incorrect because it fails to recognize that checkOut() changes the book's state before isCheckedOut() queries it. To help students: Practice tracing state changes through sequences of method calls. Emphasize that methods can be chained and each affects or queries the current object state.

9

A Car class has methods: void service(String serviceType) records a service; int getMileage() returns current mileage. Car car = new Car("Civic", 42000) starts at 42,000 miles. What is returned when car.getMileage() is called?

42000

"42000"

car.getMileage(42000)

It returns void

Explanation

This question tests the ability to correctly use objects and call instance methods on them in Java, aligning with AP Computer Science A standards. Instance methods like getMileage() are accessor methods that return information about an object's state without modifying it. The Car object was initialized with 42000 miles, and getMileage() simply returns this integer value. Choice B is correct because getMileage() returns the integer value 42000 (without quotes, as it's a number not a string). Choice A is incorrect because it shows a string "42000" when the method returns an int primitive type. To help students: Distinguish between different return types (int vs String). Practice identifying accessor methods (getters) that retrieve but don't modify object state.

10

A BankAccount class has methods void deposit(double amount) and boolean withdraw(double amount). If BankAccount acct = new BankAccount(40.0); what is returned when withdraw(60.0) is called, assuming it fails when funds are insufficient?

It returns 0.0 and empties the account.

It returns false and leaves balance unchanged.

It returns 60.0 and decreases balance by 60.0.

It returns true and sets balance to -20.0.

Explanation

This question tests the ability to correctly use objects and call instance methods on them in Java, aligning with AP Computer Science A standards. Instance methods operate on specific objects of a class. By calling these methods, you can manipulate the object's state or retrieve specific data. For example, withdraw attempts to decrease the balance but may fail if insufficient funds exist. Choice B is correct because withdraw(60.0) returns false when attempting to withdraw more than the available balance (40.0), and the balance remains unchanged at 40.0. Choice A is incorrect because a well-designed withdraw method should not allow negative balances - it should fail and return false instead. To help students: Emphasize understanding method preconditions and failure behaviors. Practice predicting method behavior in edge cases like insufficient funds.

Page 1 of 2