AP CSA Unit 5: Writing Classes

285 multiple-choice questions on AP Computer Science A Unit 5, Writing Classes, organized by topic. They are not tied to any one class or teacher: they follow the College Board course framework. Learn the 38 key terms with the flashcards first, then test yourself. Every answer comes with a short explanation.

Take the practice test · Flashcards · All study tools

What it covers

  1. 5.1 Anatomy of a class and constructors
  2. 5.2 Methods: accessors, mutators, and parameters
  3. 5.3 Static variables and methods
  4. 5.4 Scope and access

Key terms

5.1 Anatomy of a class and constructors

Client class
A class that constructs objects of another class and calls that class's public methods
Constructor
A special method with the class's name and no return type that initializes a new object
Constructor body
The block of a constructor, used to give a new object's instance variables their starting values
Constructor signature
A constructor's name together with the number, types, and order of its parameters
Default constructor
The no-argument constructor Java supplies only when a class writes no constructor of its own
Default field value
The starting value of an instance variable that no constructor assigns: 0, 0.0, false, or null
Encapsulation
Keeping instance variables private and providing public methods to access them
Instance variable
A field declared in a class and stored separately in each object of that class
No-argument constructor
A constructor that takes no parameters, whether written by the programmer or supplied as the default
Overloading
Methods or constructors with the same name but different parameter lists
this
A reference to the current object

5.2 Methods: accessors, mutators, and parameters

Accessor method
A method that returns information about an object without changing it
Actual parameter
A value or expression supplied in a method or constructor call, also called an argument
Formal parameter
A variable declared in a method or constructor header that receives the incoming argument
Helper method
A method, often private, that other methods of the same class call in order to share work
Method header
The first line of a method, listing its access modifier, return type, name, and parameter list
Mutator method
A method that changes an object's instance variables
Postcondition
A condition that a method guarantees will be true when it finishes, provided its precondition was met
Precondition
A condition that must already be true when a method is called for the method to work as specified
toString method
A method that returns a String describing an object, called automatically when the object is printed
void method
A method whose return type is void, so it returns no value and a call to it cannot be used as an expression

5.3 Static variables and methods

Class variable
Another name for a static variable: one copy of the data is stored with the class, not in each object
Class-name invocation
Calling a static method or reading a static variable through the class name, as in Math.max or Counter.getCount
Class-wide counter
A static integer that a constructor or method updates so the class can track how many objects exist or how often something happened
Initialized static variable
A static variable that is given a starting value in its declaration, often 0 for a counter
Shared class data
Information kept in a static variable so that every object of the class reads and writes the same copy
Static constant
A class-wide constant, usually declared public static final and named in uppercase with underscores
Static context
A static method or other class-level code, where there is no current object and this cannot be used
static final
The pair of modifiers used to declare a class-wide constant that is shared and cannot be reassigned
static variable
A variable shared by all instances of a class

5.4 Scope and access

Access modifier
A keyword such as public or private that controls which other classes may use a field, constructor, or method
Local variable
A variable declared in a method, constructor, or block and usable only in that block after it is initialized
Object state
The current values of an object's instance variables
Private member
A field or method declared private, so only code in the same class can refer to it by name
Public member
A field, constructor, or method declared public, so code in other classes may use it
Scope
The part of a program where a variable can be used
Uninitialized local variable
A local variable that has been declared but not yet assigned, and so cannot be read
Variable shadowing
A local variable or parameter that uses the same name as an instance variable, hiding the field in that block

Sample practice questions

  1. Instance variables in a well-designed class are usually declared

    1. public, so any class can read and change them directly
    2. private, so they're accessed only through the class's methods
    3. static, so they are shared by every object of the class
    4. final, so their values never change after construction

    Answer: B. private, so they're accessed only through the class's methods Encapsulation hides data behind private access and exposes behavior through public methods.

  2. An accessor method

    1. changes the value of an instance variable to a new value
    2. creates and returns a new object of the same class
    3. returns information about an object's state without changing it
    4. is always static, so it can be called on the class

    Answer: C. returns information about an object's state without changing it Accessors (getters) return values; mutators (setters) modify state.

  3. A static variable

    1. is unique to each object, with its own copy in every instance
    2. belongs to the class and is shared by all objects of that class
    3. can never change once the class has been loaded into memory
    4. must be private, so only static methods can access it

    Answer: B. belongs to the class and is shared by all objects of that class One copy of a static variable exists for the whole class, for example a count of objects created.

  4. A local variable declared inside a method

    1. can be used by any method in the class, like an instance variable
    2. keeps its value between calls to the method, like a static variable
    3. can be used only within that method, from its declaration to the end of its block
    4. is automatically initialized to 0 or null when the method starts

    Answer: C. can be used only within that method, from its declaration to the end of its block Local variables exist only while the method runs and must be initialized before use.

  5. A constructor

    1. must return the new object with a return statement
    2. is called with the keyword void
    3. has the same name as the class and no return type
    4. can only be written once per program

    Answer: C. has the same name as the class and no return type Constructors initialize a new object's instance variables and have no return type, not even void.

  6. A mutator method typically

    1. changes one or more instance variables and is often void
    2. returns an instance variable's value without changing it
    3. has the same name as the class and no return type
    4. must be private, so only the class can call it

    Answer: A. changes one or more instance variables and is often void Setters such as setName(String n) update the object's state.

  7. A static method

    1. is called on the class and can't use instance variables directly
    2. can only be called on an object, not on the class itself
    3. can use this to access the current object's instance variables
    4. must return void, since it has no object to return a value to

    Answer: A. is called on the class and can't use instance variables directly Static methods have no current object, so they cannot refer to instance variables or this. Math.sqrt is static.

  8. Which statement about private methods is true?

    1. They can be called only from inside the same class
    2. They can be called from any class in the same package
    3. They can't take parameters or return a value
    4. They must be static, since objects can't call them

    Answer: A. They can be called only from inside the same class private restricts access to code within the class, useful for helper methods.

  9. If a class has no constructor written, Java

    1. refuses to compile the class until a constructor with parameters is written
    2. provides a default no-argument constructor that sets fields to default values
    3. requires every object of the class to be created and set to null first
    4. uses the constructor of the Object class, which needs one argument per field

    Answer: B. provides a default no-argument constructor that sets fields to default values Without any constructor, Java supplies a no-argument one; int fields default to 0, references to null.

  10. Which method header correctly returns the average of two ints as a double?

    1. public void average(int a, int b)
    2. public double average(int a, int b)
    3. public int average(double a, double b)
    4. public average double(int a, int b)

    Answer: B. public double average(int a, int b) The return type comes before the method name, and parameters are listed with their types.

Practice all 285 questions

More AP Computer Science A

Studying for a test in your own class? Make a practice test from your class notes.