AP Computer Science a Quiz: Objects Instances Of Classes
20 questions · exam conditions
0:00
Objects Instances Of ClassesQuestion 1 of 20

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)
    }
}
```​
The balance is $130.0.
The balance is $80.0.
The balance is $250.0.
The balance is $200.0.
← Back to quizzes

AP Computer Science a Quiz

AP Computer Science a Quiz: Objects Instances Of Classes

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.

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.

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

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)
    }
}
```​
  1. The balance is $130.0. (correct answer)
  2. The balance is $80.0.
  3. The balance is $250.0.
  4. The balance is $200.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, 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.

Question 2

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());
    }
}
```​
  1. 130.0 (correct answer)
  2. 100.0
  3. 40.0
  4. 90.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, 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.

Question 3

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?

  1. c1.getInstanceCount() returns 2, Counter.getTotalCount() returns 3
  2. c1.getInstanceCount() returns 2, Counter.getTotalCount() returns 5 (correct answer)
  3. c1.getInstanceCount() returns 3, Counter.getTotalCount() returns 5
  4. c1.getInstanceCount() returns 5, Counter.getTotalCount() returns 5

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).

Question 4

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?

  1. Both car1 and car2 must have the same values for model and year.
  2. The model and year are part of the Car class itself, not the individual objects.
  3. car1 will have its own model and year attributes, and car2 will also have its own model and year attributes. (correct answer)
  4. If the 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.

Question 5

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?

  1. Student newStudent; (correct answer)
  2. Student newStudent = Student();
  3. new Student newStudent;
  4. Student = newStudent;

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.

Question 6

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?

  1. Canine would be a subclass of Wolf.
  2. Wolf and Fox would be subclasses of Canine. (correct answer)
  3. Wolf would be a superclass of Fox.
  4. 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.

Question 7

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?

  1. The Box object originally referenced by b1 is copied into the memory location of b2.
  2. The Box object originally referenced by b2 is copied into the memory location of b1.
  3. The variables b1 and b2 now both hold references to the same Box object. (correct answer)
  4. The variables 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.

Question 8

// Line 1: public class LightBulb { ... } // Line 2: // Line 3: LightBulb deskLamp;

In the code snippet above, what does the statement on Line 3 accomplish?

  1. It creates a new LightBulb class named deskLamp.
  2. It creates a new LightBulb object and stores it in a variable named deskLamp.
  3. It declares a variable named deskLamp that can hold a reference to a LightBulb object. (correct answer)
  4. It calls a method named 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).

Question 9

If Car is a class, which statement best explains the relationship between the Car class and the concept of inheritance in Java?

  1. The Car class is a subclass of the Object class, inheriting its fundamental methods. (correct answer)
  2. The Car class must be a superclass to at least one other class, such as ElectricCar.
  3. The Car class cannot participate in inheritance unless it is declared as public static.
  4. The 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.

Question 10

Which statement best describes a class in object-oriented programming?

  1. A specific, concrete entity that exists in memory during program execution.
  2. A blueprint or template that defines the attributes and behaviors for a type of object. (correct answer)
  3. A sequence of instructions that performs a specific task and can be called from other parts of a program.
  4. A named storage location in memory that holds a single primitive value or an object reference.

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).

Question 11

In the context of Java, which of the following best defines an object?

  1. A set of source code files that can be compiled into a runnable program.
  2. A keyword in Java that is used to define the fundamental structure of a data type.
  3. A specific instance of a class, having its own state and access to the behaviors defined by its class. (correct answer)
  4. A formal description of a method's name, parameters, and return type.

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).

Question 12

Consider a class named Robot. Which statement accurately describes the relationship between the Robot class and objects created from it?

  1. Only one Robot object can be created from the Robot class.
  2. Multiple Robot objects can be created, and each object is an independent instance with its own state. (correct answer)
  3. All Robot objects created from the class share the same state and attributes.
  4. The 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.

Question 13

All classes in Java are part of a class hierarchy. What does this imply about program design and functionality?

  1. It forces every program to have a graphical user interface.
  2. It allows for code reuse and the creation of specialized classes from more general ones. (correct answer)
  3. It restricts a program to using only one superclass for the entire application.
  4. It means that objects cannot interact with objects of a different class.

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.

Question 14

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?

  1. A specific playlist object named workoutMixtape.
  2. A method named addSong that adds a new song to the playlist.
  3. A variable that stores a list of Song objects contained in the playlist. (correct answer)
  4. The 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.

Question 15

Which of the following statements provides the most accurate analogy for the relationship between a class and an object?

  1. A class is like a car, and an object is like the driver of the car.
  2. A class is like a cookie recipe, and an object is like an actual cookie baked from that recipe. (correct answer)
  3. A class is like a single musical note, and an object is like a complete song.
  4. A class is like a building, and an object is like the architect who designed the building.

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.

Question 16

In Java, what is the role of the Object class?

  1. It is a special class that cannot be instantiated and is used only for defining program structure.
  2. It is the direct or indirect superclass of all other classes, providing a set of common behaviors. (correct answer)
  3. It is a utility class that contains static methods for common mathematical and string operations.
  4. It is the class from which all primitive data types like 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().

Question 17

A class is a formal implementation of attributes and behaviors. How do these concepts relate to an object created from that class?

  1. The object's behaviors define its attributes, and its attributes define its class.
  2. The class defines the attributes and behaviors, and an object is a specific instance that possesses those characteristics. (correct answer)
  3. The object is a list of behaviors, and the class is a collection of attributes.
  4. The class is a single attribute, and the object is a single behavior.

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.

Question 18

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());
    }
}
```​
  1. 0.0
  2. -5.0
  3. 75.0 (correct answer)
  4. 80.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 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.

Question 19

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);
    }
}
```​
  1. The balance is $-20.0.
  2. The balance is $40.0. (correct answer)
  3. The balance is $30.0.
  4. The balance is $90.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.

Question 20

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());
    }
}
```​
  1. checking prints 80.0 after the call.
  2. checking prints 100.0 after the call. (correct answer)
  3. checking prints 20.0 after the call.
  4. checking prints 60.0 after the call.

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.