What this quiz covers
This quiz focuses on Objects Instances Of Classes, giving you a quick way to practice the rules, question types, and explanations that matter most for AP Computer Science a.
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)
}
}
```
AP Computer Science a Quiz
Practice Objects Instances Of Classes 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.
This quiz focuses on Objects Instances Of Classes, giving you a quick way to practice the rules, question types, and explanations that matter most for AP Computer Science a.
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.
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)
}
}
```
Explanation: 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.
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());
}
}
```
Explanation: 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.
public class Counter { private static int totalCount = 0; private int instanceCount;
public Counter() {
totalCount++;
instanceCount = 0;
}
public void increment() {
instanceCount++;
totalCount++;
}
public int getInstanceCount() {
return instanceCount;
}
public static int getTotalCount() {
return totalCount;
}
}
Consider the following code segment:
Counter c1 = new Counter(); Counter c2 = new Counter(); c1.increment(); c1.increment(); c2.increment();
After this code executes, what will c1.getInstanceCount() and Counter.getTotalCount() return?
Explanation: When c1 and c2 are created, the constructor increments totalCount twice (once for each object), making totalCount = 2. Each object starts with instanceCount = 0. When c1.increment() is called twice, c1's instanceCount becomes 2, and totalCount increases by 2 (to 4). When c2.increment() is called once, c2's instanceCount becomes 1, and totalCount increases by 1 (to 5). Therefore, c1.getInstanceCount() returns 2 (c1's individual count), and Counter.getTotalCount() returns 5 (the shared static count across all instances).
public class Car { private String model; private int year; // constructors and methods not shown }
Based on the Car class definition, if two Car objects, car1 and car2, are created, which statement is true?
car1 and car2 must have the same values for model and year.model and year are part of the Car class itself, not the individual objects.car1 will have its own model and year attributes, and car2 will also have its own model and year attributes. (correct answer)model of car1 is changed, the model of car2 will automatically change to the same value.Explanation: 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.
public class Student { private String name; private int studentID; // implementation not shown }
Which of the following code segments correctly declares a variable that is capable of holding a reference to a Student object?
Explanation: 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.
In a zoological classification system, a Canine is a general category, while Wolf and Fox are more specific types of Canine.
If these relationships were modeled using classes in Java, which of the following statements would be most accurate?
Canine would be a subclass of Wolf.Wolf and Fox would be subclasses of Canine. (correct answer)Wolf would be a superclass of Fox.Canine, Wolf, and Fox would all be unrelated classes.Explanation: 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.
public class Box { /* details not shown */ }
// In some other method: Box b1 = new Box(); Box b2 = new Box(); b1 = b2;
After the code segment above is executed, which statement is true?
Box object originally referenced by b1 is copied into the memory location of b2.Box object originally referenced by b2 is copied into the memory location of b1.b1 and b2 now both hold references to the same Box object. (correct answer)b1 and b2 are now equivalent, but they still refer to two separate Box objects.Explanation: The assignment b1 = b2; copies the reference value from b2 into b1. As a result, both variables now "point" to the same object in memory—the one that was originally created and referenced by b2. The object originally referenced by b1 is now eligible for garbage collection.
// Line 1: public class LightBulb { ... } // Line 2: // Line 3: LightBulb deskLamp;
In the code snippet above, what does the statement on Line 3 accomplish?
LightBulb class named deskLamp.LightBulb object and stores it in a variable named deskLamp.deskLamp that can hold a reference to a LightBulb object. (correct answer)deskLamp on the LightBulb class.Explanation: The statement LightBulb deskLamp; follows the pattern ClassName variableName;. This is the syntax for declaring a reference variable. It allocates space for a reference but does not create an object (which would require the new keyword).
If Car is a class, which statement best explains the relationship between the Car class and the concept of inheritance in Java?
Car class is a subclass of the Object class, inheriting its fundamental methods. (correct answer)Car class must be a superclass to at least one other class, such as ElectricCar.Car class cannot participate in inheritance unless it is declared as public static.Car class inherits its attributes from the objects that are created from it.Explanation: Unless explicitly stated otherwise, every class in Java automatically extends the Object class. This means the Car class is a subclass in the universal Java class hierarchy and inherits methods like toString() and equals() from its ultimate superclass, Object.
Which statement best describes a class in object-oriented programming?
Explanation: A class serves as a blueprint for creating objects. It defines the common properties (attributes) and actions (behaviors) that all objects of that type will have. An object is a specific entity (A), a method is a sequence of instructions (C), and a variable is a named storage location (D).
In the context of Java, which of the following best defines an object?
Explanation: An object is created from a class and represents a tangible instance of that class. Each object has its own state (values for its instance variables) but shares the behaviors (methods) defined in the class. A set of source files is a project (A), the class keyword defines the structure (B), and a method's description is its signature (D).
Consider a class named Robot. Which statement accurately describes the relationship between the Robot class and objects created from it?
Robot object can be created from the Robot class.Robot objects can be created, and each object is an independent instance with its own state. (correct answer)Robot objects created from the class share the same state and attributes.Robot class is an object itself, and no other objects can be created from it.Explanation: A class is a template from which multiple, distinct objects can be created. Each object is an instance of the class and maintains its own state (the values of its instance variables) independently of other objects.
All classes in Java are part of a class hierarchy. What does this imply about program design and functionality?
Explanation: The class hierarchy, with its superclass-subclass relationships (inheritance), is a fundamental mechanism for code reuse. A general superclass can define common features, and multiple subclasses can inherit and extend those features for more specialized purposes.
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?
workoutMixtape.addSong that adds a new song to the playlist.Song objects contained in the playlist. (correct answer)Playlist class definition itself.Explanation: 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.
Which of the following statements provides the most accurate analogy for the relationship between a class and an object?
Explanation: This analogy effectively captures the core concept. The recipe (class) is the set of instructions and definitions for ingredients (attributes) and steps (methods). The cookie (object) is a concrete instance created according to that recipe. Multiple distinct cookies can be made from the same recipe.
In Java, what is the role of the Object class?
int and double are derived.Explanation: Every class in Java implicitly or explicitly inherits from the Object class. This means all objects, regardless of their class, are part of a single class hierarchy and inherit a common set of methods, such as toString() and equals().
A class is a formal implementation of attributes and behaviors. How do these concepts relate to an object created from that class?
Explanation: 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 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());
}
}
```
Explanation: 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 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);
}
}
```
Explanation: 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.
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());
}
}
```
Explanation: 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.