Chapter 9: Objects & Classes Checkpoint Questions
How do you create an object?
The syntax to create an object is new ClassName();
What are the benefits of data field encapsulation?
Two benefits: (1) for protecting data and (2) for easy to maintain the class.
If all the data fields in a class are private and of primitive types, and the class doesn't contain any setter methods, is the class immutable?
Yes
What is an anonymous object?
An anonymous object is the one that does not have a reference variable referencing it.
Is an array an object or a primitive type value? Can an array contain elements of an object type? Describe the default value for the elements of an array.
An array is an object. An array can contain elements of an object type. The default value for the elements of an array is 0 for numeric, false for boolean, '\u0000' for char, null for object element type.
What is an accessor method? What is a mutator method? What are the naming conventions for accessor methods and mutator methods?
Accessor method is for retrieving private data value. Mutator method is for changing private data value. The naming convention for accessor method is getDataFieldName() for non-boolean values and isDataFieldName() for boolean values. The naming convention for mutator method is setDataFieldName(value).
Describe the relationship between an object and its defining class.
Objects of the same type are defined using a common class. A class is a template, blueprint, or contract that defines what an object's data fields and methods will be. An object is an instance of a class. It is possible to create many instances of a class.
Which operator is used to access a data field or invoke a method from an object?
The member access operator is used to access a data field or invoke a method from an object.
How do you declare a reference variable?
The syntax to declare a reference variable for an object is ClassName objectRefVar;
How do you define a class?
The syntax to define a class is public class ClassName { }
How do you create a Date for the current time? How do you display the current time?
Use the Date's no-arg constructor to create a Date for the current time. Use the Date's toString() method to display a string representation for the Date.
How do you create a Point2D? Suppose p1 and p2 are two instances of Point2D, how do you obtain the distance between the two points? How do you obtain the midpoint between the two points?
You can create a Point2D object using its constructor Point2D(x, y) for a point at (x, y). Use p1.distance(p2) to obtain the distance between p1 and p2. Use p1.midPoint(p2) to obtain the mid-point between p1 and p2.
Describe the role of the this keyword.
this refers to the object itself
What is NullPointerException?
A NullPointerException occurs when a null reference variable is used to access the members of an object.
When will a class have a default constructor?
A class has a default constructor only if the class does not define any constructor.
What are the differences between constructors and methods?
Constructors are special kinds of methods that are called when creating an object using the new operator. Constructors do not have a return type-not even void.
Which packages contain the classes Date, Random, Point2D, System, and Math?
Date is in the java.util package. Random is in the java.util package. Point2D is in the javafx.geometry package. System and Math are in the java.lang package.
If a class contains only private data fields and no setter methods, is the class immutable?
Not necessarily. To be immutable, the class must also contain no getter methods that would return a reference to a mutable data field object.
Can you invoke an instance method or reference an instance variable from a static method? Can you invoke a static method or reference a static variable from an instance method?
You cannot invoke an instance method or reference an instance variable from a static method. You can invoke a static method or reference a static variable from an instance method.