AP Computer Science a Quiz: Calling Instance Methods
20 questions · exam conditions
0:00
Calling Instance MethodsQuestion 1 of 20

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 true and sets balance to -20.0.
It returns false and leaves balance unchanged.
It returns 0.0 and empties the account.
It returns 60.0 and decreases balance by 60.0.
← Back to quizzes

AP Computer Science a Quiz

AP Computer Science a Quiz: Calling Instance Methods

Practice Calling Instance 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.

What this quiz covers

This quiz focuses on Calling Instance Methods, giving you a quick way to practice the rules, question types, and explanations that matter most for AP Computer Science a.

How to use this quiz

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.

All questions

Question 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?

  1. It returns true and sets balance to -20.0.
  2. It returns false and leaves balance unchanged. (correct answer)
  3. It returns 0.0 and empties the account.
  4. It returns 60.0 and decreases balance by 60.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.

Question 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?

  1. car.service("oil change"); (correct answer)
  2. service(car, "oil change");
  3. car.service();
  4. car.service = "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.

Question 3

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

  1. true (correct answer)
  2. false
  3. "true"
  4. 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.

Question 4

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?

  1. Balance becomes -20.0 and returns true
  2. Balance stays 40.0 and returns false (correct answer)
  3. Balance becomes 0.0 and returns false
  4. 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.

Question 5

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?

  1. It sets the balance to 40.0.
  2. It decreases the balance by 40.0.
  3. It increases the balance by 40.0. (correct answer)
  4. It returns 40.0 and leaves the 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, 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.

Question 6

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?

  1. checkedOut becomes false
  2. checkedOut becomes true (correct answer)
  3. checkOut returns true
  4. novel.checkOut(true) is required

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.

Question 7

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?

  1. "42000"
  2. 42000 (correct answer)
  3. It returns void
  4. car.getMileage(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.

Question 8

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?​

  1. 150.0
  2. 30.0
  3. 90.0 (correct answer)
  4. No output; withdraw returns a double.

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.

Question 9

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?

  1. true (correct answer)
  2. false
  3. "Dune"
  4. No output; checkOut returns boolean.

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.

Question 10

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?

  1. deposit(acct, 25.0);
  2. acct.deposit(25.0); (correct answer)
  3. acct.deposit();
  4. 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).

Question 11

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?

  1. 3.4 (correct answer)
  2. 85.0
  3. 170.0
  4. 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.

Question 12

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?

  1. It sets GPA to 4.0 immediately
  2. It adds 100.0 as a stored grade (correct answer)
  3. It returns the new GPA as a double
  4. 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.

Question 13

A Student class has public void addGrade(double grade) and public double calculateGPA(). A Student s = new Student("Mina"); calls s.addGrade(2.0); and then calls s.addGrade(3.0);. Which method call correctly performs the task of adding another grade of 4.0?

  1. s.addGrade(4.0); (correct answer)
  2. s.calculateGPA(4.0);
  3. Student.addGrade(4.0);
  4. s.addGrade();

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 on specific objects using dot notation, with the correct parameters matching the method signature. The addGrade method requires a double parameter representing the grade to add. Choice A is correct because s.addGrade(4.0) correctly calls the instance method on the object s with the required double parameter 4.0. Choice C is incorrect because it attempts to call addGrade as a static method on the class rather than as an instance method on the object s. To help students: Emphasize the difference between static and instance method calls. Practice identifying correct syntax for calling methods with parameters on specific objects.

Question 14

A BankAccount class has public void deposit(double amount) and public void withdraw(double amount) to change balance, plus public double getBalance(). A BankAccount acct = new BankAccount(500.0); calls acct.deposit(25.0); then acct.withdraw(10.0);. What will be the output after calling getBalance() on acct?

  1. 515.0 (correct answer)
  2. 535.0
  3. 465.0
  4. 500.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 can be chained together to perform multiple operations on an object, with each method modifying the state based on the previous state. Starting with 500.0, depositing 25.0 gives 525.0, then withdrawing 10.0 leaves 515.0. Choice A is correct because the sequence of operations (500.0 + 25.0 - 10.0) correctly results in a final balance of 515.0 when getBalance() is called. Choice B is incorrect because it adds both amounts instead of adding then subtracting, misunderstanding the withdraw operation. To help students: Emphasize careful tracking of state changes through multiple method calls. Practice working through sequences of operations step-by-step to predict final outcomes.

Question 15

A Student class stores a running total of grade points and number of grades. It has methods public void addGrade(double grade) (adds a grade), and public double calculateGPA() (returns average of added grades). A Student s = new Student("Kai"); calls s.addGrade(4.0); s.addGrade(3.0); then s.calculateGPA();. What is returned when calculateGPA() is called on s?

  1. 7.0
  2. 3.5 (correct answer)
  3. 2.0
  4. No value; calculateGPA 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. Instance methods can maintain running totals and perform calculations on accumulated data within an object. The calculateGPA() method returns the average of all grades added, which after adding 4.0 and 3.0 is (4.0 + 3.0) / 2 = 3.5. Choice B is correct because calling calculateGPA() correctly computes and returns the average of the two grades: 7.0 total divided by 2 grades equals 3.5. Choice A is incorrect because it returns the sum of grades rather than the average, misunderstanding what GPA calculation requires. To help students: Emphasize understanding what calculations methods perform, not just their names. Practice tracing through multiple method calls and tracking how object state changes with each call.

Question 16

A Car class includes public void service(String serviceType) and public int getMileage(). A Car car = new Car(84500); is created, and no driving occurs; then car.getMileage(); is called. What is returned when getMileage() is called on car?

  1. "84500"
  2. 0
  3. 84500 (correct answer)
  4. No value; getMileage 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. Instance methods like getMileage() return specific data from an object without modifying its state. Since the car was created with 84500 miles and no driving occurred, getMileage() returns this initial value. Choice C is correct because calling getMileage() returns the integer value 84500, which was set when the car object was created. Choice A is incorrect because it returns the mileage as a String in quotes, but getMileage() returns an int, not a String. To help students: Emphasize understanding method return types from their signatures. Practice distinguishing between different data types and how they appear in output.

Question 17

public class Rectangle { private int length; private int width;

public Rectangle(int l, int w) {
    length = l;
    width = w;
}

public int getArea() {
    return length * width;
}

public int getPerimeter() {
    return 2 * (length + width);
}

public void scale(int factor) {
    length *= factor;
    width *= factor;
}

public boolean isSquare() {
    return length == width;
}

}

Consider the following code segment that uses the Rectangle class above:

Rectangle r1 = new Rectangle(3, 4); Rectangle r2 = new Rectangle(5, 5); int result = r1.getArea() + r2.getPerimeter(); r2.scale(2); boolean check = r2.isSquare();

What are the values of result and check after this code executes?

  1. result is 32, check is true
  2. result is 42, check is true (correct answer)
  3. result is 32, check is false
  4. result is 42, check is false

Explanation: r1.getArea() returns 3 * 4 = 12. r2.getPerimeter() returns 2 * (5 + 5) = 20. So result = 12 + 20 = 42. After r2.scale(2), r2's length and width both become 10, so r2.isSquare() returns true since 10 == 10. Choice A incorrectly calculates result as 32. Choice C has the wrong result value. Choice D has both correct result but wrong boolean value.

Question 18

public class BankAccount { private double balance; private String accountNumber;

public BankAccount(String num, double initialBalance) {
    accountNumber = num;
    balance = initialBalance;
}

public void deposit(double amount) {
    balance += amount;
}

public boolean withdraw(double amount) {
    if (amount <= balance) {
        balance -= amount;
        return true;
    }
    return false;
}

public double getBalance() {
    return balance;
}

}

Consider the following code segment:

BankAccount account = new BankAccount("12345", 100.0); account.deposit(50.0); boolean success1 = account.withdraw(75.0); boolean success2 = account.withdraw(100.0); double finalBalance = account.getBalance();

What are the values of success1, success2, and finalBalance?

  1. success1 is true, success2 is true, finalBalance is 75.0
  2. success1 is true, success2 is false, finalBalance is 75.0 (correct answer)
  3. success1 is false, success2 is false, finalBalance is 150.0
  4. success1 is true, success2 is false, finalBalance is 25.0

Explanation: Initial balance is 100.0. After deposit(50.0), balance becomes 150.0. First withdraw(75.0) succeeds since 75.0 ≤ 150.0, so success1 is true and balance becomes 75.0. Second withdraw(100.0) fails since 100.0 > 75.0, so success2 is false and balance remains 75.0. Choice A incorrectly shows success2 as true. Choice C shows both withdrawals failing. Choice D shows the wrong final balance.

Question 19

public class StringProcessor { private String text;

public StringProcessor(String s) {
    text = s;
}

public int getLength() {
    return text.length();
}

public String getUpperCase() {
    return text.toUpperCase();
}

public void appendText(String additional) {
    text = text + additional;
}

public boolean contains(String substring) {
    return text.indexOf(substring) >= 0;
}

public String getText() {
    return text;
}

}

Consider the following code segment:

StringProcessor sp = new StringProcessor("Hello"); int len1 = sp.getLength(); sp.appendText(" World"); boolean found = sp.contains("lo W"); String result = sp.getUpperCase();

What are the values of len1, found, and result?

  1. len1 is 5, found is true, result is "HELLO WORLD" (correct answer)
  2. len1 is 11, found is true, result is "HELLO WORLD"
  3. len1 is 5, found is false, result is "HELLO WORLD"
  4. len1 is 5, found is true, result is "Hello World"

Explanation: len1 is calculated before appendText, so it's the length of "Hello" which is 5. After appendText(" World"), the text becomes "Hello World". contains("lo W") searches for "lo W" in "Hello World" - this substring exists starting at index 3, so found is true. getUpperCase() returns "HELLO WORLD". Choice B shows len1 as 11 (the final length). Choice C shows found as false. Choice D shows result in original case.

Question 20

public class Calculator { private double memory;

public Calculator() {
    memory = 0.0;
}

public double add(double a, double b) {
    double result = a + b;
    memory = result;
    return result;
}

public double multiply(double a, double b) {
    double result = a * b;
    memory = result;
    return result;
}

public double getMemory() {
    return memory;
}

public void clearMemory() {
    memory = 0.0;
}

public double addToMemory(double value) {
    memory += value;
    return memory;
}

}

Consider the following code segment:

Calculator calc = new Calculator(); double val1 = calc.add(3.0, 4.0); double val2 = calc.multiply(2.0, 5.0); calc.addToMemory(val1); double final_memory = calc.getMemory();

What are the values of val1, val2, and final_memory?

  1. val1 is 7.0, val2 is 10.0, final_memory is 27.0
  2. val1 is 7.0, val2 is 10.0, final_memory is 7.0
  3. val1 is 7.0, val2 is 10.0, final_memory is 10.0
  4. val1 is 7.0, val2 is 10.0, final_memory is 17.0 (correct answer)

Explanation: When you encounter object-oriented programming questions involving instance variables and method calls, you need to carefully trace through each method execution and track how the object's state changes over time. Let's trace through this code step by step. The Calculator starts with memory = 0.0. When calc.add(3.0, 4.0) executes, it calculates 3.0 + 4.0 = 7.0, stores this result in memory, and returns 7.0, so val1 = 7.0. Next, calc.multiply(2.0, 5.0) calculates 2.0 × 5.0 = 10.0, overwrites the memory with 10.0, and returns 10.0, so val2 = 10.0. The crucial step is calc.addToMemory(val1), which adds val1 (7.0) to the current memory value (10.0), making memory = 17.0. Finally, calc.getMemory() returns this value, so final_memory = 17.0. Choice A incorrectly assumes final_memory would be 27.0, perhaps by mistakenly adding all three values (7.0 + 10.0 + 10.0). Choice B gives final_memory as 7.0, which would only be correct if you forgot that the multiply method overwrote the memory before addToMemory was called. Choice C shows final_memory as 10.0, which ignores the addToMemory operation entirely and just returns what multiply stored. The key trap here is forgetting that each arithmetic method overwrites the memory variable. Always trace through instance variable changes methodically, noting when values get overwritten versus when they get modified through operations like addition.