- Consider the following code segment that creates and manipulates Student objects:
Student s1 = new Student("Alice", 85);
Student s2 = new Student("Bob", 92);
Student s3 = s1;
s3.setGrade(88);
s2 = s3;
After this code executes, what are the grades of the objects referenced by s1, s2, and s3?
Answer: s1: 88, s2: 88, s3: 88
Initially, s1 references a Student object with grade 85, and s2 references a different Student object with grade 92. When s3 = s1 is executed, s3 references the same object as s1. Then s3.setGrade(88) modifies the object that both s1 and s3 reference, changing its grade to 88. Finally, s2 = s3 makes s2 reference the same object as s1 and s3. All three variables now reference the same Student object with grade 88. The original Student object that s2 referenced (with grade 92) is no longer referenced by any variable.
- A class is a formal implementation of attributes and behaviors. How do these concepts relate to an object created from that class?
Answer: The class defines the attributes and behaviors, and an object is a specific instance that possesses those characteristics.
A class serves as the blueprint, defining what attributes (e.g., color, size) and behaviors (e.g., move, calculate) an object will have. An object is a concrete realization of that blueprint, with its own specific values for the attributes.
- Consider the following code segment:
StringHolder sh1 = new StringHolder("Hello");
StringHolder sh2 = sh1;
String str = sh1.getValue();
sh2.appendTo(" World");
str = str + "!";
After this code executes, what are the values stored in the objects referenced by sh1 and sh2, and what is the value of str?
Answer: sh1: "Hello World", sh2: "Hello World", str: "Hello!"
This question tests your understanding of object references and string immutability in Java. When you see code involving object assignments and string operations, pay close attention to whether variables point to the same object or different objects.
Let's trace through the execution step by step. First, sh1 is created with the value "Hello". Then sh2 = sh1 creates a reference copy—both sh1 and sh2 now point to the same StringHolder object in memory. Next, str gets the current value "Hello" from the shared object. When sh2.appendTo(" World") executes, it modifies the value field of the object that both sh1 and sh2 reference, changing it to "Hello World". Finally, str = str + "!" creates a new string "Hello!" and assigns it to str, but this doesn't affect the StringHolder object.
Choice A incorrectly assumes sh1 and sh2 reference different objects—they both point to the same object that now contains "Hello World". Choice B mistakenly thinks that the string concatenation str = str + "!" somehow affects the StringHolder object, but string operations on str are completely separate from the object's internal state. Choice D combines both errors from A and B, showing different values for sh1 and sh2 while also incorrectly linking the string operation to one of the objects.
Remember: when one object variable is assigned to another (sh2 = sh1), they share the same object. Changes through either reference affect the shared object, but separate string operations create new strings without affecting existing objects.
- In an object-oriented program that simulates a library,
Book is a class. Which of the following would be an object? Answer: A specific copy of a novel with its own unique barcode and condition.
The class Book is the general template (D). A specific, tangible book like a particular copy of a novel is an instance of that class, making it an object. It has its own state (barcode, condition) based on the attributes defined in the Book class (A). Checking out is a behavior (B).
- Consider the following Java class and code; what is the effect of the method call checking.deposit(savings.getBalance()) on checking?
class BankAccount {
private String owner;
private double balance;
public BankAccount(String owner, double startingBalance) {
this.owner = owner;
balance = startingBalance;
}
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;
}
}
class Main {
public static void main(String[] args) {
BankAccount checking = new BankAccount("Ava", 20.0);
BankAccount savings = new BankAccount("Ben", 80.0);
checking.deposit(savings.getBalance());
System.out.println(checking.getBalance());
}
}
``` Answer: checking prints 100.0 after the call.
This question tests AP Computer Science A skills: understanding objects as instances of classes and their interaction through methods. Objects in Java are instances of classes, which define fields and methods; methods allow objects to perform actions or modify state. In this scenario, savings.getBalance() returns 80.0, which is then passed as the argument to checking.deposit(). This adds $80.0 to checking's initial balance of $20.0, resulting in a final balance of $100.0. Choice B is correct because it shows the result of depositing the value returned by one object's method into another object. Choice D is incorrect because it only considers the deposited amount, ignoring checking's initial balance. To help students: Practice method composition where one method's return value becomes another's argument. Emphasize that deposit adds to the existing balance rather than replacing it. Watch for: confusion about whether methods add to or replace values, or misunderstanding method chaining.
- Consider the following Java class and code; after executing it, what is the state of savings's balance?
class BankAccount {
private String owner;
private double balance;
public BankAccount(String owner, double startingBalance) {
this.owner = owner;
balance = startingBalance;
}
public void deposit(double amount) {
balance += amount;
}
public boolean withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
return true;
}
return false;
}
// Transfer money between two objects (interaction)
public void transferTo(BankAccount other, double amount) {
if (withdraw(amount)) {
other.deposit(amount);
}
}
public double getBalance() {
return balance;
}
}
class Main {
public static void main(String[] args) {
BankAccount checking = new BankAccount("Ava", 300.0);
BankAccount savings = new BankAccount("Ben", 100.0);
checking.transferTo(savings, 80.0);
savings.withdraw(50.0);
}
}
``` Answer: The balance is $130.0.
This question tests AP Computer Science A skills: understanding objects as instances of classes and their interaction through methods. Objects in Java are instances of classes, which define fields and methods; methods allow objects to perform actions or modify state. In this scenario, the transferTo method demonstrates object interaction: checking withdraws $80 (if possible) and deposits it into savings. Starting with $100, savings receives $80 to become $180, then withdraws $50, resulting in a final balance of $130. Choice B is correct because it accurately reflects the balance after both the transfer and withdrawal operations. Choice C is incorrect because it only accounts for the transfer, ignoring the subsequent withdrawal. To help students: Trace complex operations involving multiple objects step-by-step. Emphasize how methods can coordinate actions between different object instances. Watch for: students who lose track of operations or don't understand how objects can interact through method calls.
- A variable of a reference type, such as
String, is declared. What kind of value does this variable hold? Answer: A reference, which is similar to a memory address, to the location of an object.
Reference variables do not store the objects themselves. Instead, they store a reference (often conceptualized as a memory address) that indicates where the object is located in memory. This is different from primitive type variables, which store the actual value.
- Consider the following Java class and code; what is the output of the following method call?
class BankAccount {
private String owner;
private double balance;
public BankAccount(String owner, double startingBalance) {
this.owner = owner;
balance = startingBalance;
}
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;
}
}
class Main {
public static void main(String[] args) {
BankAccount checking = new BankAccount("Ava", 75.0);
BankAccount savings = new BankAccount("Ben", 125.0);
checking.withdraw(80.0);
System.out.println(checking.getBalance());
}
}
``` Answer: 75.0
This question tests AP Computer Science A skills: understanding objects as instances of classes and their interaction through methods. Objects in Java are instances of classes, which define fields and methods; methods allow objects to perform actions or modify state. In this scenario, checking has a balance of $75.0 and attempts to withdraw $80.0. Since the withdrawal amount exceeds the balance, the withdraw method's condition fails, and the balance remains unchanged at $75.0. Choice C is correct because the withdraw method protects against overdrafts, maintaining the original balance when the requested amount exceeds available funds. Choice B is incorrect because it assumes the withdrawal succeeds and creates a negative balance, which the method logic prevents. To help students: Reinforce the importance of conditional checks in methods. Practice tracing through failed operations and understanding their effects. Watch for: students who assume all method calls succeed or who incorrectly calculate results of failed operations.
- Consider the following Java class and code; after executing it, what is the state of acct2's balance?
class BankAccount {
private String owner;
private double balance;
public BankAccount(String owner, double startingBalance) {
this.owner = owner;
balance = startingBalance;
}
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;
}
}
class Main {
public static void main(String[] args) {
BankAccount acct1 = new BankAccount("Ava", 100.0);
BankAccount acct2 = acct1; // two references to the same object
acct2.deposit(40.0);
acct1.withdraw(10.0);
System.out.println(acct2.getBalance());
}
}
``` Answer: 130.0
This question tests AP Computer Science A skills: understanding objects as instances of classes and their interaction through methods. Objects in Java are instances of classes, which define fields and methods; methods allow objects to perform actions or modify state. In this scenario, acct2 = acct1 creates two references pointing to the same BankAccount object, not two separate objects. When acct2.deposit(40.0) is called, it modifies the shared object to have balance $140.0, and acct1.withdraw(10.0) further modifies it to $130.0. Choice A is correct because both references point to the same object, so all modifications affect the single shared balance. Choice B is incorrect because it assumes acct1 and acct2 are separate objects with independent balances. To help students: Use memory diagrams to visualize reference variables pointing to objects. Emphasize the difference between creating new objects versus creating new references. Watch for: the common misconception that assignment creates a copy of the object rather than copying the reference.
- Consider a class named
Playlist designed to represent a collection of songs. Which of the following is an example of an attribute that would be defined within the Playlist class? Answer: A variable that stores a list of Song objects contained in the playlist.
An attribute is a piece of data that describes the state of an object. For a Playlist object, an essential piece of data would be the collection of songs it contains. This would be represented by an instance variable. A is an object, B is a method, and D is the class.
- Consider the following Java class and code; after executing it, what is the state of checking's balance?
// Bank account management example
class BankAccount {
private String owner;
private double balance;
public BankAccount(String owner, double startingBalance) {
this.owner = owner;
balance = startingBalance;
}
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;
}
}
class Main {
public static void main(String[] args) {
BankAccount checking = new BankAccount("Ava", 200.0);
BankAccount savings = new BankAccount("Ben", 500.0);
checking.deposit(50.0);
savings.withdraw(100.0);
checking.withdraw(120.0);
// (No printing here)
}
}
``` Answer: The balance is $130.0.
This question tests AP Computer Science A skills: understanding objects as instances of classes and their interaction through methods. Objects in Java are instances of classes, which define fields and methods; methods allow objects to perform actions or modify state. In this scenario, the checking account starts with $200.0, then deposit(50.0) adds $50 to make it $250, and withdraw(120.0) subtracts $120, resulting in a final balance of $130. Choice A is correct because it reflects the updated balance after all method calls, showing accurate tracking of state changes through multiple operations. Choice C is incorrect because it only accounts for the deposit, ignoring the withdrawal. To help students: Trace through each method call step-by-step, updating the balance after each operation. Emphasize that each object maintains its own state independently. Watch for: students who only track some operations or confuse the states of different objects.
- If these relationships were modeled using classes in Java, which of the following statements would be most accurate?
Answer: Wolf and Fox would be subclasses of Canine.
The Canine class represents the more general concept, making it the superclass. The Wolf and Fox classes represent more specialized versions of a Canine, so they would be subclasses that inherit from Canine.
- Consider the following code segment:
Temperature temp1 = new Temperature(0.0);
Temperature temp2 = temp1;
Temperature temp3 = new Temperature(temp1.getCelsius());
temp2.setCelsius(100.0);
double f1 = temp1.getFahrenheit();
double f2 = temp2.getFahrenheit();
double f3 = temp3.getFahrenheit();
What are the values of f1, f2, and f3?
Answer: f1 = 212.0, f2 = 212.0, f3 = 32.0
When you encounter object reference questions in Java, the crucial concept is understanding the difference between creating new objects versus copying references. This distinction determines whether changes to one variable affect others.
Let's trace through the code execution. Initially, temp1 references a Temperature object with 0.0°C. The line Temperature temp2 = temp1; doesn't create a new object—it makes temp2 point to the same Temperature object as temp1. However, Temperature temp3 = new Temperature(temp1.getCelsius()); creates a completely separate Temperature object, initialized with the current value from temp1 (0.0°C).
When temp2.setCelsius(100.0) executes, it modifies the Temperature object that both temp1 and temp2 reference. Since they point to the same object, both now have 100.0°C, which converts to 212.0°F. Meanwhile, temp3 remains unaffected at 0.0°C (32.0°F) because it's a separate object.
Choice B incorrectly assumes temp1 wasn't affected by the change to temp2. Choice C incorrectly assumes all three variables reference the same object. Choice D incorrectly assumes the change affected temp3 even though it's a separate object.
The correct answer is A: f1 = 212.0, f2 = 212.0, f3 = 32.0.
Study tip: Always distinguish between assignment statements that copy references (obj2 = obj1) versus those that create new objects (obj2 = new Class()). Draw memory diagrams showing which variables point to which objects—this visual approach prevents reference confusion.
- Based on the
Car class definition, if two Car objects, car1 and car2, are created, which statement is true? Answer: car1 will have its own model and year attributes, and car2 will also have its own model and year attributes.
model and year are instance variables. This means that every instance (object) of the Car class gets its own copy of these variables. The state of one object is independent of the state of another object of the same class.
- Which of the following code segments correctly declares a variable that is capable of holding a reference to a
Student object? Answer: Student newStudent;
The correct syntax to declare a reference variable is ClassName variableName;. This creates a variable named newStudent of type Student that can hold a reference to a Student object. This statement does not create the object itself.
- A programmer needs a variable to store a reference to an object from the
Game class. Which of the following lines of code correctly accomplishes only the declaration of this variable? Answer: Game myGame;
The line Game myGame; declares a variable named myGame of type Game. This variable is now ready to hold a reference to a Game object, but no object has been created or assigned to it yet. Choice A both declares and initializes.
- Consider the following code segment:
Book[] library = new Book[3];
library[0] = new Book("Java Basics", "Smith");
library[1] = library[0];
library[2] = new Book("Java Basics", "Smith");
boolean result1 = library[0].checkOut();
boolean result2 = library[1].checkOut();
boolean result3 = library[2].checkOut();
What are the values of result1, result2, and result3?
Answer: result1 = true, result2 = false, result3 = true
library[0] and library[1] reference the same Book object (created with "Java Basics", "Smith"), while library[2] references a different Book object with the same title and author. When library[0].checkOut() is called, the shared object is checked out and the method returns true (result1 = true). When library[1].checkOut() is called, it attempts to check out the same object that is already checked out, so the method returns false (result2 = false). When library[2].checkOut() is called, it checks out a different Book object that is available, so it returns true (result3 = true).
- Consider the following code segment:
Rectangle rect1 = new Rectangle(4.0, 3.0);
Rectangle rect2 = rect1;
double area1 = rect1.getArea();
rect2.scale(2.0);
double area2 = rect1.getArea();
What are the values of area1 and area2?
Answer: area1 = 12.0, area2 = 48.0
Initially, rect1 references a Rectangle with length 4.0 and width 3.0. When rect2 = rect1 executes, both variables reference the same Rectangle object. The first call to getArea() returns 4.0 3.0 = 12.0, so area1 = 12.0. When rect2.scale(2.0) is called, it modifies the single Rectangle object that both rect1 and rect2 reference, changing length to 8.0 and width to 6.0. The second call to rect1.getArea() returns 8.0 6.0 = 48.0, so area2 = 48.0.
- In a class hierarchy, what is the role of a superclass?
Answer: It is a general class that provides common attributes and behaviors to one or more specialized classes.
A superclass, also known as a parent or base class, defines general characteristics. More specific classes, called subclasses, can inherit these characteristics from the superclass. A is the definition of a subclass.
- Consider the following Java class and code; after executing it, what is the state of checking's balance?
class BankAccount {
private String owner;
private double balance;
public BankAccount(String owner, double startingBalance) {
this.owner = owner;
balance = startingBalance;
}
public void deposit(double amount) {
balance += amount;
}
public boolean withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
return true;
}
return false;
}
public void transferTo(BankAccount other, double amount) {
if (withdraw(amount)) {
other.deposit(amount);
}
}
public double getBalance() {
return balance;
}
}
class Main {
public static void main(String[] args) {
BankAccount checking = new BankAccount("Ava", 90.0);
BankAccount savings = new BankAccount("Ben", 10.0);
checking.transferTo(savings, 50.0);
checking.transferTo(savings, 60.0);
}
}
``` Answer: The balance is $40.0.
This question tests AP Computer Science A skills: understanding objects as instances of classes and their interaction through methods. Objects in Java are instances of classes, which define fields and methods; methods allow objects to perform actions or modify state. In this scenario, checking starts with $90.0, successfully transfers $50.0 to savings (leaving $40.0), then attempts to transfer $60.0 but fails because only $40.0 remains. The second transfer doesn't occur due to insufficient funds, so checking's final balance is $40.0. Choice B is correct because it reflects the balance after one successful transfer and one failed transfer attempt. Choice D is incorrect because it assumes both transfers succeed, ignoring the balance check in the withdraw method. To help students: Practice scenarios with multiple operations where some may fail. Emphasize that each operation depends on the current state. Watch for: students who don't track state changes between operations or assume all transfers succeed.