Chapter 8 Book Quiz Flashcards

1
Q
  1. This type of method cannot access any non-static member variables in its own class.
    a. instance
    b. void
    c. static
    d. non-static
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Making an instance of one class a field in another class is called .
a. nesting
b. class fielding
c. aggregation
d. concatenation

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

This enum method returns the position of an enum constant in the declaration.
a. position
b. location
c. ordinal
d. toString

A

c. ordinal
Explanation: The ordinal() method in an enum returns the zero-based index (position) of an enum constant in its declaration.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

CRC stands for
a. Class, Return value, Composition
b. Class, Responsibilities, Collaborations
c. Class, Responsibilities, Composition
d. Compare, Return, Continue

A

: 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

TRUE OR FALSE: All static member variables are initialized to 0 by default.

A

True
Explanation: Static variables are initialized to their default values (e.g., 0 for numbers, null for references) if no explicit initialization is provided.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

TRUE OR FALSE: When an object is passed as an argument to a method, the method can access the argument.

A

True
Explanation: The method can access the object’s fields and methods via the reference passed as an argument.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

TRUE OR FALSE: A method cannot return a reference to an object.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

TRUE OR FALSE: You can declare an enumerated data type inside a method.

A

True
Explanation: Java allows the declaration of an enum inside a method, but this limits its scope to the method itself.

17
Q

TRUE OR FALSE: Enumerated data types are actually special types of classes.

A

True
Explanation: In Java, enums are essentially special types of classes that represent a fixed set of constants.

18
Q

TRUE OR FALSE: Enum constants have a toString method.

A

True
Explanation: By default, the toString() method in an enum returns the name of the enum constant. However, you can override it to provide a custom string representation.

19
Q

The following class definition has an error. What is it?
1.
public class MyClass
{
private int x;
private double y;
public static void setValues(int a, double
b)
{ x = a;
y = b;
}
}

A

The error is in the setValues method. The setValues method is static, but it is trying to access non-static instance variables (x and y) directly.

Explanation:
Static methods belong to the class itself, not to any specific instance of the class. Therefore, static methods cannot directly access instance variables (non-static fields) without creating an object of the class.
To fix this, you need to either:
Make the x and y variables static (if they should be shared among all instances of the class), or
Create an instance of MyClass within the setValues method or pass an instance of MyClass to the method and then modify the instance variables through that object.
Corrected Code Example:
Option 1: Make instance variables static:

java
Copy code
public class MyClass {
private static int x; // Make x static
private static double y; // Make y static

public static void setValues(int a, double b) {
    x = a;
    y = b;
} } Option 2: Use an instance of MyClass:

java
Copy code
public class MyClass {
private int x;
private double y;

public void setValues(int a, double b) {
    x = a;
    y = b;
} } In this case, setValues is no longer static, and it will work on an instance of MyClass.
19
Q

Assume the following declaration exists :
enum Coffee { MEDIUM, DARK, DECAF }
Find the error(s) in the following switch statement:
// This code has errors!
Coffee myCup = DARK;
switch (myCup)
{
case Coffee.MEDIUM :
System.out.println(“Mild flavor.”);
break;
case Coffee.DARK :
System.out.println(“Strong flavor.”);
break;
case Coffee.DECAF :
System.out.println(“Won’t keep you
awake.”);
break;
default:
System.out.println(“Never heard of
it.”);
}

A

The error in the code is that myCup is assigned the value DARK directly, but DARK is not recognized as a valid value. The correct way to refer to an enum constant is by qualifying it with the enum type Coffee.

Explanation:
In Java, when using enums, you need to specify the enum type when assigning a value from the enum.
In this case, DARK is a member of the Coffee enum, but you must prefix it with the enum type (Coffee) when assigning it to the myCup variable.
Error:

The line Coffee myCup = DARK; is incorrect because DARK should be qualified with the enum name (Coffee).
Corrected Code:

java
Copy code
Coffee myCup = Coffee.DARK; // Correct way to assign enum value
switch (myCup) {
case Coffee.MEDIUM:
System.out.println(“Mild flavor.”);
break;
case Coffee.DARK:
System.out.println(“Strong flavor.”);
break;
case Coffee.DECAF:
System.out.println(“Won’t keep you awake.”);
break;
default:
System.out.println(“Never heard of it.”);
}
Additional Notes:
The switch statement itself is correct. It is properly checking each enum value (Coffee.MEDIUM, Coffee.DARK, Coffee.DECAF).
The only issue is with the assignment of the enum value to myCup, which requires specifying the enum type (e.g., Coffee.DARK instead of just DARK).