- 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?
Answer: 3.4
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.
- 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?
Answer: len1 is 5, found is true, result is "HELLO WORLD"
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.
- 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()?
Answer: true
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.
- 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?
Answer: acct.deposit(25.0);
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).
- 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?
Answer: val1 is 7.0, val2 is 10.0, final_memory is 17.0
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.
- Consider the following code segment:
Circle c1 = new Circle(3.0);
Circle c2 = new Circle(4.0);
boolean result1 = c1.isLargerThan(c2);
c1.setRadius(5.0);
boolean result2 = c1.isLargerThan(c2);
double ratio = c1.getArea() / c2.getArea();
Which of the following best describes the values after execution?
Answer: result1 is false, result2 is true, ratio is approximately 1.56
Initially c1 has radius 3, c2 has radius 4. c1's area is 9π, c2's area is 16π, so result1 is false. After c1.setRadius(5.0), c1's area becomes 25π. Now c1.isLargerThan(c2) returns true since 25π > 16π, so result2 is true. The ratio is 25π/16π = 25/16 = 1.5625 ≈ 1.56. Choice B has the wrong ratio. Choice C incorrectly shows result1 as true. Choice D shows both comparisons wrong.
- Consider the following code segment:
Counter c1 = new Counter();
Counter c2 = new Counter(10);
c1.increment();
c1.increment(5);
c2.increment();
c2.reset();
c2.increment(3);
What will be printed by System.out.println(c1.getValue() + " " + c2.getValue());?
Answer: 6 3
c1 starts at 0, increment() makes it 1, increment(5) makes it 6. c2 starts at 10, increment() makes it 11, reset() makes it 0, increment(3) makes it 3. So the output is "6 3". Choice A forgets the initial increment() call on c1. Choice C forgets that c2 was reset before the final increment. Choice D miscalculates c1's final value and c2's value after reset.
- 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?
Answer: success1 is true, success2 is false, finalBalance is 75.0
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.
- 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? Answer: true
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.
- Consider the following code segment:
Student s1 = new Student("Alex", 11, 3.2);
Student s2 = new Student("Beth", 12, 3.7);
boolean alex_honor = s1.isHonorRoll();
s1.updateGPA(3.6);
boolean alex_senior = s1.isSenior();
String beth_class = s2.getClassification();
What are the values of alex_honor, alex_senior, and beth_class?
Answer: alex_honor is false, alex_senior is false, beth_class is "Senior"
This question tests your understanding of object state, method calls, and how changes to instance variables affect subsequent method calls. When tracing through code with objects, you need to carefully track the order of operations and what each method returns based on the object's current state.
Let's trace through each variable assignment step by step. Student s1 is created with grade 11 and GPA 3.2, while s2 has grade 12 and GPA 3.7. When alex_honor = s1.isHonorRoll() executes, the isHonorRoll() method checks if GPA ≥ 3.5. Since Alex's GPA is 3.2, this returns false. Next, s1.updateGPA(3.6) changes Alex's GPA to 3.6, but this happens after alex_honor was already assigned. When alex_senior = s1.isSenior() executes, it checks if grade equals 12. Alex's grade is 11, so this returns false. Finally, beth_class = s2.getClassification() checks Beth's grade (12) and returns "Senior".
Choice A incorrectly assumes alex_honor is true, likely because students see the GPA update and forget it happens after the honor roll check. Choice B makes the same mistake with alex_honor but correctly identifies alex_senior as false. Choice C incorrectly assumes alex_senior is true (perhaps confusing Alex with Beth) and gets beth_class wrong. Choice D correctly identifies all three: alex_honor is false (GPA was 3.2 when checked), alex_senior is false (grade 11 ≠ 12), and beth_class is "Senior".
Remember: always trace code in exact execution order. Method calls use the object's state at the moment they're called, not after subsequent modifications.
- 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?
Answer: checkedOut becomes false
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.
- 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?
Answer: 42000
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.
- 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? Answer: 90.0
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.
- 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? Answer: It increases the balance by 40.0.
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.
- 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?
Answer: car.service("oil change");
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.
- 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? Answer: s.addGrade(4.0);
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.
- 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? Answer: 84500
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.
- 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? Answer: 3.5
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.
- 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?
Answer: It adds 100.0 as a stored grade
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.
- 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?
Answer: result is 42, check is true
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.