Calling Instance Methods

Help Questions

AP Computer Science A › Calling Instance Methods

Questions 1 - 10
1

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 60.0 and decreases balance by 60.0.

It returns 0.0 and empties the account.

It returns true and sets balance to -20.0.

It returns false and leaves balance unchanged.

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.

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("oil change");

service(car, "oil change");

car.service();

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(); void returnBook(); boolean isCheckedOut(). Book b = new Book("1984") starts checked out. How does calling b.returnBook() affect its state?

checkedOut becomes false

b.returnBook(true) sets it available

checkedOut becomes true

returnBook returns 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. 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.

4

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()?

false

It prints nothing because isCheckedOut is void

"true"

true

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.

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 -20.0 and returns true

Balance becomes 0.0 and returns false

Balance stays 40.0 and returns 40.0

Balance stays 40.0 and returns 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. 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 BankAccount class stores a balance. It has methods public void deposit(double amount) (adds to balance), public void withdraw(double amount) (subtracts from balance), and public double getBalance() (returns current balance). A BankAccount acct = new BankAccount(250.0); is created, then acct.deposit(40.0); is called. How does calling deposit(40.0) on acct affect its state?

It decreases the balance by 40.0.

It sets the balance to 40.0.

It returns 40.0 and leaves the balance unchanged.

It increases the balance by 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. Instance methods operate on specific objects of a class, and by calling these methods, you can manipulate the object's state or retrieve specific data. The deposit method adds the specified amount to the account's balance, so calling deposit(40.0) on an account with initial balance 250.0 increases the balance to 290.0. Choice C is correct because calling deposit(40.0) correctly increases the balance by 40.0, as per the method's definition which adds to the existing balance. Choice A is incorrect because it misunderstands deposit as setting the balance to a specific value rather than adding to it. To help students: Emphasize understanding method signatures and their effects on object state. Practice tracing method calls step-by-step and predicting the resulting state changes.

7

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.

8

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?

It returns void

car.getMileage(42000)

42000

"42000"

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.

9

A BankAccount class has public void deposit(double amount), public void withdraw(double amount), and public double getBalance(). A BankAccount acct = new BankAccount(120.0); is created, then acct.withdraw(30.0); is executed. What will be the output after calling getBalance() on acct?​

150.0

No output; withdraw returns a double.

90.0

30.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 like withdraw modify object state by performing specific operations - in this case, subtracting an amount from the balance. Starting with a balance of 120.0 and withdrawing 30.0 leaves a balance of 90.0. Choice C is correct because calling withdraw(30.0) correctly decreases the balance from 120.0 to 90.0, and getBalance() returns this updated value. Choice B is incorrect because it returns only the withdrawn amount rather than the remaining balance, misunderstanding what getBalance() returns. To help students: Emphasize tracking object state through multiple method calls. Practice distinguishing between the amount used in a method call and the resulting state of the object.

10

A Book class tracks whether it is checked out. It has methods public void checkOut() (marks checkedOut true), public void returnBook() (marks checkedOut false), and public boolean isCheckedOut() (returns status). A Book novel = new Book("Dune"); is created and initially not checked out; then novel.checkOut(); is called. What will be the output after calling isCheckedOut() on novel?

No output; checkOut returns boolean.

false

"Dune"

true

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, allowing you to change object state or query current state. The checkOut() method marks the book's checkedOut status as true, and isCheckedOut() returns this boolean status. Choice A is correct because after calling checkOut(), the book's checkedOut status becomes true, so isCheckedOut() returns true. Choice C is incorrect because it confuses the return value of isCheckedOut() (a boolean) with the book's title, showing a misunderstanding of method return types. To help students: Emphasize the difference between void methods that change state and methods that return values. Practice predicting method outcomes by carefully reading method signatures and understanding their purpose.

Page 1 of 2