Chapter 8 Book Quiz Flashcards
- This type of method cannot access any non-static member variables in its own class.
a. instance
b. void
c. static
d. non-static
c. static
Explanation: Static methods belong to the class itself, not to any instance of the class. As a result, static methods cannot access non-static (instance) variables, which require an object to be instantiated first.
When an object is passed as an argument to a method, this is actually passed.
a. a copy of the object
b. the name of the object
c. a reference to the object
d. none of these; you cannot pass an object
c. a reference to the object
Explanation: In Java, when an object is passed to a method, the reference (memory address) of the object is passed, not a copy of the object itself. This allows the method to modify the original object.
If you write this method for a class, Java will automatically call it any time you concatenate an object of the class with a string.
a. toString
b. plusString
c. stringConvert
d. concatString
a. toString
Explanation: The toString() method is automatically called whenever an object is concatenated with a string. This method returns a string representation of the object.
Making an instance of one class a field in another class is called .
a. nesting
b. class fielding
c. aggregation
d. concatenation
c. aggregation
Explanation: Aggregation is a concept where one class contains an instance of another class as a field. It represents a “has-a” relationship between objects.
This is the name of a reference variable that is always available to an instance method and refers to the object that is calling the method.
a. callingObject
b. this
c. me
d. instance
b. this
Explanation: The this keyword refers to the current instance of the class within an instance method, allowing access to its fields and methods.
This enum method returns the position of an enum constant in the declaration.
a. position
b. location
c. ordinal
d. toString
c. ordinal
Explanation: The ordinal() method in an enum returns the zero-based index (position) of an enum constant in its declaration.
Assuming the following declaration exists: enum Seasons { SPRING, WINTER, SUMMER, FALL } what is the fully qualified name of the FALL constant?
a. FALL
b. enum.FALL
c. FALL.Seasons
d. Seasons.FALL
d. Seasons.FALL
Explanation: The fully qualified name of an enum constant includes the enum type followed by the constant name, i.e., Seasons.FALL.
You cannot use the fully qualified name of an enum constant for this.
a. a switch expression
b. a case expression
c. an argument to a method
d. all of these
d. all of these
Explanation: The fully qualified name of an enum constant is typically used for reference purposes in code but not in cases such as switch expressions or method arguments. The enum type should be used for these cases instead.
The Java Virtual Machine periodically performs this process, which automatically removes unreferenced objects from memory.
a. memory cleansing
b. memory deallocation
c. garbage collection
d. object expungement
c. garbage collection
Explanation: Garbage collection is the automatic process by which the Java Virtual Machine reclaims memory from objects that are no longer referenced.
If a class has this method, it is called automatically just before an instance of the class is destroyed by the Java Virtual Machine.
a. finalize
b. destroy
c. remove
d. housekeeper
a. finalize
Explanation: The finalize() method is called by the garbage collector just before an object is destroyed. It allows the object to perform cleanup tasks before memory is reclaimed.
CRC stands for
a. Class, Return value, Composition
b. Class, Responsibilities, Collaborations
c. Class, Responsibilities, Composition
d. Compare, Return, Continue
: b. Class, Responsibilities, Collaborations
Explanation: CRC (Class, Responsibilities, Collaborations) cards are a tool for identifying and documenting the responsibilities of a class and its interactions with other classes.
TRUE OR FALSE: A static member method may refer to non-static member variables of the same class, but only after an instance of the class has been defined.
False
Explanation: A static method cannot directly access non-static variables or methods, as they are tied to specific instances of the class. Static methods can only access static variables.
TRUE OR FALSE: All static member variables are initialized to 0 by default.
True
Explanation: Static variables are initialized to their default values (e.g., 0 for numbers, null for references) if no explicit initialization is provided.
TRUE OR FALSE: When an object is passed as an argument to a method, the method can access the argument.
True
Explanation: The method can access the object’s fields and methods via the reference passed as an argument.
TRUE OR FALSE: A method cannot return a reference to an object.
False
Explanation: A method can return a reference to an object. This is often used to return the instance of a class or an object created within the method.