AP Computer Science A • Unit 1.12

Objects: Instances of Classes

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

Q1A
Q2B
Q3A
Q4B
Q5A
Q6C
Q7B
Q8B
Q9B
Q10A
Q11B
Q12B
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 best describes a class?

AA blueprint that defines a kind of object
BOne specific object with stored data
CA single attribute value
DA method call
Correct answer: A. A class is the definition of a type of object, the blueprint Java follows when it builds one.
Explanation

A class describes what information objects of that class can store and what actions they can perform. It is the plan rather than the thing built from the plan, in the same way that a blueprint describes a kind of building without being the building.

  • B is not correct: one specific object with stored data is an instance of a class, such as the building named "North Hall", rather than the definition every such object is built from.
  • C is not correct: a single attribute value is one piece of data an object knows about itself, while a class describes which attributes every object of that kind can store.
  • D is not correct: a method call asks an object to perform a behavior, which Topic 1.14 covers; a class instead defines the kind of object that receives such a call.
Q2

Which statement best describes an object?

AA keyword used before every variable
BA specific instance of a class
CA blueprint with no stored data
DA primitive type
Correct answer: B. An object is one actual instance of a class, with its own stored data, such as a particular building.
Explanation

The class describes the kind, and each object is one concrete instance of that kind. Building is a class, while a specific building named "North Hall" is an object of that class.

  • A is not correct: an object is not a keyword; private is the word written before the instance variables shown here, and it marks data kept inside the object.
  • C is not correct: a blueprint with no stored data describes the class instead, and an object is one instance that holds its own values for the declared variables.
  • D is not correct: a primitive type stores a simple value directly, while an object is an instance of a class that a reference variable can locate through an object reference.
Q3

A Building's stored name is:

Aan attribute
Ba behavior
Ca class
Dan instance
Correct answer: A. An attribute is data an object knows about itself, and a stored name is exactly that kind of data.
Explanation

In Java, attributes are stored in instance variables. The declaration private String name; gives each object a place to keep its own name, so the name is data the object knows about itself.

  • B is not correct: a behavior is an action an object can perform, such as reporting its total number of rooms or changing its name, not the stored value itself.
  • C is not correct: a class is the definition of a type of object, while the stored name is one piece of data held inside one such object.
  • D is not correct: an instance is a whole object built from the class, and the name is a single attribute stored inside that object rather than the object itself.
Q4

A Building reports its room count. Reporting is:

Aan attribute
Ba behavior
Ca class name
Dan instance variable value
Correct answer: B. Reporting the room count is an action the object performs, and actions are behaviors written in Java as methods.
Explanation

A Building object can report its total number of rooms and can change its name. Those are behaviors. The stored facts, such as the building number and the floor count, are the attributes the object keeps.

  • A is not correct: an attribute is data the object knows about itself, such as the stored counts, rather than the act of reporting a value to whoever asked.
  • C is not correct: a class name identifies the kind of object, so Building is the class name while reporting is one of the actions its objects perform.
  • D is not correct: an instance variable value is stored data, and reporting that stored data is an action carried out on it rather than the data itself.
Q5

Which declaration stores a floor-count attribute?

Aprivate int floorCount;
Bpublic int floorCount()
CBuilding floorCount;
Dvoid floorCount()
Correct answer: A. private int floorCount; declares an instance variable, and instance variables are where a class stores its attributes.
Explanation

Read a declaration from left to right. The word private marks data kept inside the object, the type states what kind of value can be stored, and the identifier names the attribute.

  • B is not correct: public int floorCount() is written in the form of a method, and methods express behaviors rather than the attributes an object stores.
  • C is not correct: Building floorCount; gives the variable a class type, so it could hold an object reference to a Building rather than a count of floors.
  • D is not correct: void floorCount() is also written as a method, so it describes an action; attributes are stored in instance variables instead.
Q6

Which member represents a behavior?

Aprivate String name;
Bprivate int rooms;
Cpublic int totalRooms()
Dprivate int floors;
Correct answer: C. public int totalRooms() is written as a method, and in Java behaviors are the actions written as methods.
Explanation

A behavior is an action that an object can perform, such as reporting a total number of rooms. The other three members are instance-variable declarations, and instance variables hold attributes rather than actions.

  • A is not correct: private String name; declares an instance variable, so it stores the name attribute rather than describing an action the object performs.
  • B is not correct: private int rooms; declares an instance variable holding a stored count, and stored data is an attribute rather than a behavior.
  • D is not correct: private int floors; declares another instance variable, so it records data the object knows about itself rather than an action it can carry out.
Q7

What may differ between two Building objects?

ATheir class
BTheir instance-variable values
CThe meaning of int
DThe Java language
Correct answer: B. Each object receives its own set of instance variables, so two buildings can store different values in variables with the same names.
Explanation

One building might have number 101, name "North Hall", three floors, and twenty rooms per floor. Another Building object can store different values in variables that carry those same names.

  • A is not correct: both objects are instances of the same class, so the class they are built from is the one thing that cannot differ between them.
  • C is not correct: int is a type in the Java language, and what it means is fixed rather than something each object decides for itself.
  • D is not correct: the Java language is the same for every program, so it is not part of the information one object remembers about itself.
Q8

In the blueprint analogy, “North Hall” is:

Athe class
Ban object
Ca behavior
Da data type keyword
Correct answer: B. A specific building named "North Hall" is one instance built from the Building class plan.
Explanation

A blueprint describes a kind of building; it is not the building itself. Building is the class, and "North Hall", Science Center, and Library Building are objects of that class.

  • A is not correct: the class is Building, the plan that describes the kind, while "North Hall" names one particular thing built from that plan.
  • C is not correct: a behavior is an action such as reporting a total number of rooms, whereas "North Hall" identifies one object rather than an action.
  • D is not correct: a data type keyword such as int states what kind of value a variable may store, not the identity of a single object.
Q9

Which pair is classified correctly?

Aname: behavior; rename: attribute
Bname: attribute; rename: behavior
Cboth are classes
Dboth are objects
Correct answer: B. A stored name is data the object knows about itself, and renaming it is an action the object performs.
Explanation

An attribute is data that an object knows about itself, and a behavior is an action that an object can perform. A Building object stores a name and can also change its name.

  • A is not correct: the two labels are swapped: the stored name is data the object knows, and changing that name is an action the object carries out.
  • C is not correct: a class is the definition of a type of object, while these two are a stored attribute and an action described inside such a class.
  • D is not correct: an object is a specific instance of a class, so a stored name and a renaming action are parts of an object rather than objects themselves.
Q10

What does an instance variable provide?

AOne stored attribute for each object
BOne shared object for every class
CA method call with no name
DA new Java keyword
Correct answer: A. An instance variable belongs to each object created from the class, giving every object its own value for that attribute.
Explanation

Attributes are stored in instance variables. A declaration such as private int roomsPerFloor; gives every future object one place to keep that value, and each Building object receives its own set of these variables.

  • B is not correct: an instance variable does not create a shared object, because each object created from the class receives its own separate set of these variables.
  • C is not correct: a method call is how a behavior is requested, and a method has a name; an instance variable stores one attribute instead of performing an action.
  • D is not correct: declaring an instance variable uses words that already exist, such as private and a type name, so nothing is added to the Java language.
Q11

Which statement is true?

AEvery object of a class must store identical values.
BEach object can have its own state.
CA behavior is stored only in an int.
DAn attribute is always a method.
Correct answer: B. Each object has its own values for its instance variables, so two objects of one class can differ.
Explanation

One building might store number 101 and name "North Hall", while another Building object stores different values in variables with the same names. The class plan is shared, but the stored values are not.

  • A is not correct: objects of a class share the plan rather than the values, and each one can store different data in variables that carry the same names.
  • C is not correct: a behavior is an action written as a method, so it is not something held inside a variable whose type is int.
  • D is not correct: an attribute is data stored in an instance variable, while a behavior is written as a method, so the two are different kinds of member.
Q12

Building and “Science Center” are, respectively:

ABoth are classes.
BBuilding is a class; Science Center is an object.
CBuilding is an object; Science Center is a class.
DBoth are behaviors.
Correct answer: B. Building is the class plan, and Science Center names one specific object built from that plan.
Explanation

The class describes the kind, and each object is one concrete instance of that kind. The section lists Building as the class, with Science Center among the objects belonging to it.

  • A is not correct: only Building is a class, since Science Center is one instance built from that plan rather than a second plan of its own.
  • C is not correct: the roles are reversed here: the plan is Building, and Science Center is one particular object built from that plan.
  • D is not correct: a behavior is an action an object performs, such as reporting a total number of rooms, and neither of these two names an action.