AP Computer Science A • Unit 3.3

Anatomy of a Java Class

Detailed answer explanations for the Unit 3.3 multiple-choice practice questions.

Q1A
Q2B
Q3B
Q4C
Q5A
Q6D
Q7B
Q8C
Q9C
Q10D
Q11B
Q12A
Q13A
How to use this page: First try each question without looking at the explanation. Then compare your answer with the key and read why the other choices are not correct.

Answer Explanations

Q1

Which statement about instance variables is correct?

AThey belong to an object, and each object has its own copy.
BThey are shared automatically by every object of the class.
CThey must be declared public so methods can use them.
DThey can be accessed only from classes outside their declaring class.
Correct answer: A. An instance variable belongs to an individual object, so every object created from the class carries its own copy.
Explanation

The word instance is the clue: each instance of the class gets its own storage for the variable. This is what lets two objects of the same class hold different values at the same time.

  • B is not correct: a value shared by every object of the class is a class variable, which is a different kind of declaration.
  • C is not correct: the methods of a class can use its instance variables regardless of access designation, so public is not required for that.
  • D is not correct: this reverses the situation; the declaring class is exactly where an instance variable is always usable.
Q2

Unless a class specification states otherwise, which declaration is most appropriate for an instance variable?

Apublic int score;
Bprivate int score;
Cpublic int score()
Dprivate class Score
Correct answer: B. Instance variables are declared private so that outside classes cannot change an object's state directly.
Explanation

Keeping data private is the practical side of encapsulation. The class stays in control of how its own values change, because every change has to go through a method the class itself wrote.

  • A is not correct: a public field lets any outside class assign to it directly, which is what private access is meant to prevent.
  • C is not correct: the parentheses make this a method header rather than a variable declaration.
  • D is not correct: this begins a class rather than declaring a variable inside one.
Q3

Which access designation is used for constructors in this course?

AConstructors must be designated private.
BConstructors are designated public.
CConstructors have no access designation.
DConstructors use the same access as every instance variable.
Correct answer: B. Constructors are public so that other classes can create objects of the class.
Explanation

A constructor and an instance variable have opposite needs. The variable is hidden because outside code should not change it directly; the constructor is exposed because outside code must be able to bring the object into existence in the first place.

  • A is not correct: a private constructor could not be called from another class, so no outside code could create the object.
  • C is not correct: constructors carry an access designation like the other members of a class.
  • D is not correct: instance variables are private, and matching them would make the constructor unreachable from outside.
Q4

Which line begins a class named Ticket?

Aprivate class Ticket
Bpublic Ticket
Cpublic class Ticket
Dclass public Ticket
Correct answer: C. A class header names the access designation, then the keyword class, then the class name.
Explanation

The three parts appear in a fixed order. Recognizing that order also helps distinguish a class header from a constructor header, which uses the class name but no class keyword.

  • A is not correct: classes in this course are declared public so that other classes can use them.
  • B is not correct: without the keyword class this reads as a constructor header rather than a class header.
  • D is not correct: the access designation comes before the keyword class, not after it.
Q5

Which statement correctly compares public and private methods?

AA public method can be called inside or outside its class; a private method can be called only inside its class.
BBoth public and private methods can be called only from outside their class.
CA private method is available outside its class, but a public method is not.
DThe two designations have no effect on method access.
Correct answer: A. Public widens access to include other classes; private restricts it to the declaring class.
Explanation

This distinction is what lets a class publish a small set of behaviors while keeping its internal steps to itself. A private method is still fully usable by the class that declared it; it is only outside code that cannot reach it.

  • B is not correct: both kinds of method can be called from inside the declaring class.
  • C is not correct: this swaps the two designations.
  • D is not correct: controlling access is precisely what these designations do.
Q6

A display method must be called from another class. Which declaration uses the appropriate access designation?

Aprivate void display()
Bvoid display public()
Cprivate class display
Dpublic void display()
Correct answer: D. A method that must be reachable from another class is declared public.
Explanation

The access designation follows from where the method has to be called. Deciding that first, then writing the header, keeps the design deliberate rather than accidental.

  • A is not correct: a private method cannot be called from another class, which is exactly what the requirement asks for.
  • B is not correct: the access designation belongs at the start of the header, not after the method name.
  • C is not correct: this declares a class rather than a method, and it is private as well.
Q7

In a class declaration, where are instance variables, constructors, and methods written?

AIn the class header, before the class name
BIn the class body, between its opening and closing braces
COutside the class declaration
DOnly inside the constructor
Correct answer: B. The class header names the class; everything the class contains is written in the body between the braces.
Explanation

A class declaration has two parts. The header identifies the class, and the body holds its members. Keeping that split in mind makes it easier to read an unfamiliar class from the outside in.

  • A is not correct: the header carries the access designation, the keyword class, and the name, and nothing else.
  • C is not correct: members written outside the braces would not belong to the class at all.
  • D is not correct: the constructor is itself one member among several, so it cannot contain the others.
Q8

A normalize helper method should be used only inside its class. Which declaration is most appropriate?

Apublic void normalize()
Bpublic class normalize
Cprivate void normalize()
Dprivate class normalize
Correct answer: C. A method meant only for internal use is declared private.
Explanation

This is the mirror image of the earlier display question. Marking a helper private keeps it out of the class's published behavior, which means it can be rewritten or removed later without affecting any outside code.

  • A is not correct: a public helper would be callable from other classes, which the requirement rules out.
  • B is not correct: this declares a class rather than a method, and a public one at that.
  • D is not correct: the access is right but the declaration is still a class rather than a method.
Q9

Examine the following class. Which declarations store the state of each Badge object?

public class Badge {
    private String label;
    private int points;
    public void display() {
    }
}
AThe class header
BThe display method
Clabel and points
DThe println statement
Correct answer: C. label and points are the instance variables, so they are what each Badge object stores.
Explanation

Reading a class body means sorting its members by role. Declarations with a type and a name hold state; headers followed by braces describe behavior. Here two declarations hold state and one member describes behavior.

  • A is not correct: the class header names the class; it stores nothing for an individual object.
  • B is not correct: display is a behavior, and its body holds no declaration of its own.
  • D is not correct: the class as shown contains no println statement.
Q10

The following class and client code are used. Why does the final line cause a compile-time error?

public class Lamp {
    private void calibrate() {
    }

    public void turnOn() {
        calibrate();
    }
}

Lamp deskLamp = new Lamp();
deskLamp.calibrate();
AThe Lamp class cannot contain methods.
BThe turnOn method must be private.
CAn object cannot be used to call a method.
Dcalibrate is accessible only inside Lamp.
Correct answer: D. calibrate is private, so the call from outside the class is rejected even though the object exists.
Explanation

The same method is called successfully from turnOn and unsuccessfully from the client code. The difference is not the method but the location of the call: inside the class it is reachable, outside it is not. This is exactly the boundary a private designation draws.

  • A is not correct: the class does contain methods, and two of them are shown.
  • B is not correct: turnOn is meant to be called from outside, so making it private would remove the class's usable behavior.
  • C is not correct: calling a method on an object is normal; the problem is which method is being called.
Q11

Outside code must be able to call show, but it should not directly change points. Which revision best supports encapsulation?

public class Account {
    public int points;
    public void show() {
        System.out.println(points);
    }
}
AMake both points and show private.
BMake points private and keep show public.
CKeep both declarations public.
DMake the Account class private.
Correct answer: B. Encapsulation hides the data and exposes the behavior, which is what this pair of designations produces.
Explanation

The two requirements point in opposite directions, and each one settles a different declaration. Because show must be callable from outside, it stays public; because points must not be assignable from outside, it becomes private. The method can still read the variable, since it belongs to the same class.

  • A is not correct: a private show could not be called from outside, which the first requirement rules out.
  • C is not correct: leaving points public lets outside code assign to it directly, which the second requirement rules out.
  • D is not correct: this changes the visibility of the whole class instead of separating its data from its behavior.
Q12

Examine the following class outline. What is the role of Route(int s) in this class?

public class Route {
    private int stops;

    public Route(int s) {
        stops = s;
    }

    public void printStops() {
    }
}
AIt is a constructor that initializes object data.
BIt is an instance variable that stores object data.
CIt is a private helper method.
DIt is the class header.
Correct answer: A. Its name matches the class and it has no return type, which identifies it as a constructor; its body gives stops its starting value.
Explanation

Two signs together identify a constructor: the name is the class name, and no return type appears between the access designation and the name. Its job is to put the object's instance variables into a usable starting state.

  • B is not correct: the instance variable in this class is stops; a variable declaration has a type and no parameter list.
  • C is not correct: this member is public, and a helper method would have a return type and a name different from the class name.
  • D is not correct: the class header is the line containing the keyword class.
Q13

In private int quantity;, a public constructor, and public void restock(), which roles appear in order?

AStored state, state initialization, and behavior
BBehavior, stored state, and a class name
CThree instance variables
DThree constructors
Correct answer: A. The three members play the three distinct roles a class body contains, in that order.
Explanation

This question gathers the whole topic into one line. An instance variable holds what the object knows, a constructor gives those values their starting state, and a method provides what the object can do. Almost every class is built from these three roles.

  • B is not correct: the order is reversed, and none of the three members is a class name.
  • C is not correct: only the first member is an instance variable; the others have parameter lists and bodies.
  • D is not correct: only the second member is a constructor; a constructor takes the class name and no return type.