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
- 5.1 Anatomy of a class and constructors
- 5.2 Methods: accessors, mutators, and parameters
- 5.3 Static variables and methods
- 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
Instance variables in a well-designed class are usually declared
- public, so any class can read and change them directly
- private, so they're accessed only through the class's methods
- static, so they are shared by every object of the class
- 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.
An accessor method
- changes the value of an instance variable to a new value
- creates and returns a new object of the same class
- returns information about an object's state without changing it
- 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.
A static variable
- is unique to each object, with its own copy in every instance
- belongs to the class and is shared by all objects of that class
- can never change once the class has been loaded into memory
- 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.
A local variable declared inside a method
- can be used by any method in the class, like an instance variable
- keeps its value between calls to the method, like a static variable
- can be used only within that method, from its declaration to the end of its block
- 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.
A constructor
- must return the new object with a return statement
- is called with the keyword void
- has the same name as the class and no return type
- 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.
A mutator method typically
- changes one or more instance variables and is often void
- returns an instance variable's value without changing it
- has the same name as the class and no return type
- 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.
A static method
- is called on the class and can't use instance variables directly
- can only be called on an object, not on the class itself
- can use this to access the current object's instance variables
- 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.
Which statement about private methods is true?
- They can be called only from inside the same class
- They can be called from any class in the same package
- They can't take parameters or return a value
- 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.
If a class has no constructor written, Java
- refuses to compile the class until a constructor with parameters is written
- provides a default no-argument constructor that sets fields to default values
- requires every object of the class to be created and set to null first
- 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.
Which method header correctly returns the average of two ints as a double?
- public void average(int a, int b)
- public double average(int a, int b)
- public int average(double a, double b)
- 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.
More AP Computer Science A
- Unit 1: Primitive Types, Variables, and Expressions
- Unit 2: Using Objects: Strings, Math, and Wrappers
- Unit 3: Boolean Expressions and if Statements
- Unit 4: Iteration
- Unit 6: Arrays
- Unit 7: ArrayList
- Unit 8: 2D Arrays
- Unit 9: Recursion, Searching, and Sorting
Studying for a test in your own class? Make a practice test from your class notes.