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)
}
}
```
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());
}
}
```
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?
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.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?
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.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.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.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.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?
In the context of Java, which of the following best defines an object?
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.All classes in Java are part of a class hierarchy. What does this imply about program design and functionality?
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.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?
In Java, what is the role of the Object class?
int and double are derived.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?
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());
}
}
```
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);
}
}
```
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());
}
}
```