JAVA CH8
8.5 Q5: When implementing a method, use the class's set and get methods to access the class's ________ data. a. a. public. b. b. private. c. c. protected. d. d. All of the above.
ANS: b. private.
8.5 Q4: What happens when this is used in a constructor's body to call another constructor of the same class if that call is not the first statement in the constructor? a. a. A compilation error occurs. b. b. A runtime error occurs. c. c. A logic error occurs. d. d. Nothing happens. The program compiles and runs.
ANS: a. A compilation error occurs.
8.3 Q2: Which of the following statements is true? a. a. Methods and instance variables can both be either public or private. b. b. Information hiding is achieved by restricting access to class members via keyword public. c. c. The private members of a class are directly accessible to the client of a class. d. d. None of the above is true.
ANS: a. Methods and instance variables can both be either public or private.
8.6 Q1: Which statement is false? a. a. The compiler always creates a default constructor for a class. b. b. If a class's constructors all require arguments and a program attempts to call a no-argument constructor to initialize an object of the class, a compilation error occurs. c. c. A constructor can be called with no arguments only if the class does not have any constructors or if the class has a public no-argument constructor. d. d. None of the above.
ANS: a. The compiler always creates a default constructor for a class.
8.16 Q6: The import declaration import *; ________. a. a. causes a compilation error. b. b. imports all classes in the library. c. c. imports the default classes in the library. d. d. imports the classes in package java.lang.
ANS: a. causes a compilation error.
8.9 Q1: enum types are implicitly ________ and enum constants are implicitly ________. a. a. final, static. b. b. static, static. c. c. static, final. d. d. final, final. .
ANS: a. final, static
8.9 Q3: Which method returns an array of the enum's constants? a. a. values. b. b. getValues. c. c. constants. d. d. getConstants.
ANS: a. values.
8.16 Q2: A class within a package must be declared public if a. a. It will be used only by other classes in the same package. b. b. It will be used by classes that are not in the same package. c. c. It is in the same directory as the other classes in the package. d. d. It has a unique name.
ANS: b. It will be used by classes that are not in the same package. Classes outside the package cannot use a class if the class is not declared public.
8.2 Q2: The static method ________ of class String returns a formatted String. a. a. printf. b. b. format. c. c. formatString. d. d. toFormatString.
ANS: b. format.
8.8 Q1: Composition is sometimes referred to as a(n) ________. a. a. is-a relationship. b. b. has-a relationship. c. c. many-in-one relationship. d. d. one-to-many relationship.
ANS: b. has-a relationship.
8.12 Q1: Which syntax imports all static members of class Math? a. a. static import java.lang.Math.*. b. b. import static java.lang.Math.*. c. c. static import java.lang.Math. d. d. import static java.lang.Math.
ANS: b. import static java.lang.Math.*.
8.5 Q3: A programmer-defined constructor that has no arguments is called a(n) ________. a. a. empty constructor. b. b. no-argument constructor. c. c. default constructor. d. d. null constructor.
ANS: b. no-argument constructor.
8.16 Q7: The classpath consists of a list of directories or archive files, each separated by a ________ on Windows or a ________ on UNIX/Linux/Max OS X. a. a. colon (:), semicolon (;). b. b. semicolon (;), colon (:). c. c. comma (,), semicolon (;). d. d. semicolon (;), comma (,).
ANS: b. semicolon (;), colon (:).
8.16 Q4: When compiling a class in a package, the javac command-line option ________ causes the javac compiler to create appropriate directories based on the class's package declaration. a. a. -p. b. b. -a. c. c. -d. d. d. -dir.
ANS: c. -d.
8.4 Q1: When should a program explicitly use the this reference? a. a. Accessing a private variable. b. b. Accessing a public variable. c. c. Accessing a local variable. d. d. Accessing a field that is shadowed by a local variable.
ANS: c. Accessing a field that is shadowed by a local variable.
8.9 Q2: Which statement is false? a. a. An enum declaration is a comma-separated list of enum constants and may optionally include other components of traditional classes, such as constructors, fields and methods. b. b. Any attempt to create an object of an enum type with operator new results in a compilation error. c. c. An enum constructor cannot be overloaded. d. d. An enum constructor can specify any number of parameters.
ANS: c. An enum constructor cannot be overloaded.
8.13 Q1: Instance variables declared final do not or cannot: a. a. Cause syntax errors if used as a left-hand value. b. b. Be initialized. c. c. Be modified. d. d. None of the above.
ANS: c. Be modified.
8.17 Q1: When no access modifier is specified for a method or variable, the method or variable: a. a. Is public. b. b. Is private. c. c. Has package access. d. d. Is static.
ANS: c. Has package access.
8.10 Q1: Which of the following is false? a. a. Method finalize does not take parameters and has return type void. b. b. Memory leaks using Java are rare because of automatic garbage collection. c. c. Objects are marked for garbage collection by method finalize. d. d. The garbage collector reclaims unused memory.
ANS: c. Objects are marked for garbage collection by method finalize. (Objects are marked for garbage collection when there are no more references to the object).
Q1: Which of the following should usually be private? a. a. Methods. b. b. Constructors. c. c. Variables (or fields). d. d. All of the above. ANS: c. Variables (or fields).
ANS: c. Variables (or fields).
8.7 Q1: Set methods are also commonly called ________ methods and get methods are also commonly called ________ methods. a. a. query, mutator. b. b. accessor, mutator. c. c. mutator, accessor. d. d. query, accessor.
ANS: c. mutator, accessor.
8.2 Q1: The _________ of a class are also called the public services or the public interface that the class provides to its clients. a. a. public constructors. b. b. public instance variables. c. c. public methods. d. d. All of the above.
ANS: c. public methods.
8.5 Q1: A constructor cannot: a. a. be overloaded. b. b. initialize variables to their defaults. c. c. specify return types or return values. d. d. have the same name as the class.
ANS: c. specify return types or return values.
8.2 Q3: Which statement is false? a. a. The actual data representation used within the class is of no concern to the class's clients. b. b. Clients generally care about what the class does but not how the class does it. c. c. Clients are usually involved in a class's implementation. d. d. Hiding the implementation reduces the possibility that clients will become dependent on class-implementation details.
ANS: c: Clients are usually involved in a class's implementation
8.16 Q1: A package is: a. a. A directory structure used to organize classes and interfaces. b. b. A mechanism for software reuse. c. c. A group of related classes and interfaces. d. d. All of the above.
ANS: d. All of the above.
8.4 Q2: Having a this reference allows: a. a. a method to refer explicitly to the instance variables and other methods of the object on which the method was called. b. b. a method to refer implicitly to the instance variables and other methods of the object on which the method was called. c. c. an object to reference itself. d. d. All of the above.
ANS: d. All of the above.
8.7 Q3: Using public set methods provides data integrity if: a. a. The instance variables are public. b. b. The instance variables are private. c. c. The methods perform validity checking. d. d. Both b and c.
ANS: d. Both b and c.
8.5 Q2: Constructors: a. a. Initialize instance variables. b. b. When overloaded, can have identical argument lists. c. c. When overloaded, are selected by number, types and order of types of parameters. d. d. a and c.
ANS: d. a and c.
8.11 Q1: Static class variables: a. a. are final. b. b. are public. c. c. are private. d. d. are shared by all objects of a class.
ANS: d. are shared by all objects of a class.
8.16 Q8: By default, the classpath consists only of the ________. However, the classpath can be modified by providing the ________ option to the javac compiler. a. a. root directory of the package, -d. b. b. current directory, -d. c. c. root directory of the package, -classpath. d. d. current directory, -classpath.
ANS: d. current directory, -classpath.
8.13 Q2: A final field should also be declared ________ if it is initialized in its declaration. a. a. private. b. b. public. c. c. protected. d. d. static.
ANS: d. static.
8.16 Q5: The import declaration import java.util.*; is known as a ________. a. a. single-type-import declaration. b. b. all-type-import declaration. c. c. multiple-import declaration. d. d. type-import-on-demand declaration.
ANS: d. type-import-on-demand declaration.
8.11 Q2: Which of the following is false? a. a. A static method must be used to access private static instance variables. b. b. A static method has no this reference. c. c. A static method can be accessed even when no objects of its class have been instantiated. d. d. A static method can call instance methods directly.
d. d. A static method can call instance methods directly.