1.
8.2 Q1: The _________ of a class are also called the public services or the public interface that the class provides to its clients.
Correct Answer
C. public methods
Explanation
The public methods of a class are the functions or operations that the class provides to its clients. These methods can be accessed and used by other classes or objects to interact with the class and perform certain actions or operations. They form the public interface of the class, allowing clients to utilize the functionality provided by the class. Therefore, the correct answer is "public methods".
2.
The static method ________ of class String returns a formatted String.
Correct Answer
B. Format
Explanation
The static method "format" of class String returns a formatted String. This method allows you to specify a format string and a variable number of arguments, which will be inserted into the format string in the specified positions. The format method is commonly used for formatting strings with placeholders, such as "%d" for integers or "%s" for strings, and then replacing those placeholders with the actual values provided as arguments.
3.
Which statement is false?
Correct Answer
C. Clients are usually involved in a class’s implementation
Explanation
The statement "Clients are usually involved in a class’s implementation" is false. Clients are typically not involved in a class's implementation. The purpose of encapsulation is to hide the implementation details from the clients so that they can use the class without needing to know how it is implemented. Clients only need to know what the class does, not how it does it. Involving clients in the implementation would break the principle of encapsulation and increase the dependency on implementation details.
4.
Which of the following should usually be private?
Correct Answer
C. Variables (or fields).
Explanation
Variables (or fields) should usually be private because it helps to encapsulate the data within a class and prevent direct access from outside. This allows for better control over the data and ensures that it is accessed and modified only through the methods provided by the class. By keeping variables private, it also helps to maintain the integrity of the data and avoid unintended modifications or inconsistencies. On the other hand, methods and constructors can have different levels of access depending on the specific requirements of the class.
5.
Which of the following statements is true?
Correct Answer
A. Methods and instance variables can both be either public or private.
Explanation
The statement "Methods and instance variables can both be either public or private" is true. In object-oriented programming, methods and instance variables can have different access modifiers such as public or private. Public members can be accessed by any class, while private members can only be accessed within the same class. This allows for information hiding and encapsulation, as private members are not directly accessible to the client of a class. Therefore, the correct answer is that methods and instance variables can be either public or private.
6.
When should a program explicitly use the this reference?
Correct Answer
C. Accessing a local variable.
Explanation
The this reference should be explicitly used when accessing a local variable. The this keyword is used to refer to the current object, and it can be used to differentiate between a local variable and an instance variable with the same name. By using this, the program can access the local variable specifically associated with the current object.
7.
Having a this reference allows:
Correct Answer
D. All of the above.
Explanation
The correct answer is "All of the above." Having a "this" reference in a method allows all of the mentioned options. It allows the method to refer explicitly to the instance variables and other methods of the object on which the method was called. It also allows the method to refer implicitly to the instance variables and other methods of the object on which the method was called. Additionally, it allows an object to reference itself.
8.
A constructor cannot:
Correct Answer
C. Specify return types or return values.
Explanation
A constructor is a special method that is used to initialize objects of a class. It has the same name as the class and is called automatically when an object is created. Unlike regular methods, constructors do not have a return type or return values. Their purpose is to initialize the variables of the object to their default values or to values specified by the programmer. Overloading a constructor means having multiple constructors with different parameters, allowing for different ways to create objects of the class. Therefore, the correct answer is that a constructor cannot specify return types or return values.
9.
8.5 Q2: Constructors:
Correct Answer
D. A and c.
Explanation
The explanation for the given correct answer is that constructors are used to initialize instance variables, which means they are responsible for assigning initial values to the variables of an object. When constructors are overloaded, it means that there are multiple constructors with the same name but different parameters. In such cases, the constructor to be called is selected based on the number, types, and order of the parameters provided. Therefore, the correct answer is a and c.
10.
A programmer-defined constructor that has no arguments is called a ________.
Correct Answer
B. No-argument constructor.
Explanation
A programmer-defined constructor that has no arguments is called a no-argument constructor. This type of constructor is used to create objects without passing any arguments to initialize the object's state. It is commonly used when the object's initial state can be set to default values or when the values will be set later using setter methods.
11.
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?
Correct Answer
A. A compilation error occurs.
Explanation
When using "this" in a constructor's body to call another constructor of the same class, it is necessary for this call to be the first statement in the constructor. If it is not, a compilation error occurs. This is because the first statement in a constructor should always be either a call to another constructor in the same class or a call to the superclass constructor. If "this" is used to call another constructor, it must be the first statement to ensure proper initialization of the object.
12.
When implementing a method, use the class’s set and get methods to access the class’s ________ data.
Correct Answer
B. Private
Explanation
When implementing a method, it is recommended to use the class's private data. This is because private data can only be accessed within the class itself, ensuring encapsulation and data integrity. Using the class's set and get methods to access the private data allows for controlled and validated access, maintaining the class's internal state and preventing direct manipulation of the data by external entities.
13.
Which statement is false?
Correct Answer
A. The compiler always creates a default constructor for a class.
Explanation
The statement "The compiler always creates a default constructor for a class" is false. The compiler only creates a default constructor if no other constructors are explicitly defined in the class. If the class has any constructor with arguments, the compiler will not create a default constructor. Therefore, if a program attempts to call a no-argument constructor for a class that only has constructors with arguments, a compilation error will occur.
14.
Set methods are also commonly called ________ methods and get methods are also commonly called ________ methods.
Correct Answer
C. Mutator, accessor.
Explanation
Set methods are commonly known as mutator methods because they are used to modify or change the values of an object's attributes. These methods typically take parameters that represent the new values to be assigned to the attributes. On the other hand, get methods are commonly known as accessor methods because they are used to retrieve or access the values of an object's attributes. These methods typically do not take any parameters and simply return the current values of the attributes.
15.
Using public set methods provides data integrity if:
Correct Answer
D. Both b and c.
Explanation
Using public set methods provides data integrity if the instance variables are private and the methods perform validity checking.
If the instance variables are private, it means that they cannot be directly accessed or modified from outside the class. This ensures that the data remains encapsulated and can only be accessed through the set methods.
Additionally, if the set methods perform validity checking, it means that they validate the data before setting it. This helps to ensure that only valid data is stored in the instance variables, maintaining data integrity.
Therefore, both conditions (b and c) are necessary for using public set methods to provide data integrity.
16.
Composition is sometimes referred to as a(n) ________.
Correct Answer
B. Has-a relationship.
Explanation
Composition is a relationship between two classes, where one class contains an instance of another class as a member variable. This is known as a "has-a" relationship because one class "has" another class as a part of its structure. In composition, the lifetime of the contained object is dependent on the lifetime of the container object.
17.
: enum types are implicitly ________ and enum constants are implicitly ________.
Correct Answer
A. Final, static.
Explanation
Enum types are implicitly final, meaning they cannot be extended or subclassed. Enum constants, on the other hand, are implicitly static, meaning they belong to the enum type itself rather than to any particular instance of the enum. This allows enum constants to be accessed without the need to create an instance of the enum type.
18.
Which statement is false?
Correct Answer
C. An enum constructor cannot be overloaded
Explanation
An enum constructor cannot be overloaded means that it is not possible to have multiple constructors with different parameters in an enum. This is because enum constants are predefined and the constructor is called automatically when an enum constant is created. Therefore, there is no need for overloading the constructor in an enum.
19.
Which method returns an array of the enum’s constants?
Correct Answer
A. Values
Explanation
The "values" method returns an array of the enum's constants. This method is automatically generated by the compiler when an enum is defined. It allows you to access all the constants of the enum in a convenient way.
20.
Which of the following is false?
Correct Answer
C. Objects are marked for garbage collection by method finalize.
Explanation
The incorrect statement is "Objects are marked for garbage collection by method finalize." In Java, objects are not marked for garbage collection by the finalize method. Instead, the garbage collector determines which objects are eligible for garbage collection based on their reachability. The finalize method is used for performing cleanup actions on an object before it is garbage collected, but it does not directly mark the object for garbage collection.
21.
Static class variables:
Correct Answer
D. Are shared by all objects of a class
Explanation
Static class variables are shared by all objects of a class, meaning that they are not specific to any particular instance of the class. This means that any changes made to a static variable will be reflected across all instances of the class. Static variables are not unique to each object and can be accessed and modified by any object of the class.
22.
Which of the following is false?
Correct Answer
D. A static method can call instance methods directly
Explanation
A static method can call instance methods directly. This is true because a static method can access and call any instance method of its class, as long as it has access to an instance of that class.
23.
Which syntax imports all static members of class Math?
Correct Answer
B. Import static java.lang.Math.*.
Explanation
The correct answer is "import static java.lang.Math.*". This syntax imports all static members of the class Math. The "import static" statement allows us to access static members of a class without specifying the class name each time. In this case, it imports all static members of the Math class, which includes mathematical functions like sqrt(), sin(), cos(), etc.
24.
Instance variables declared final do not or cannot:
Correct Answer
C. Be modified
Explanation
Instance variables declared final cannot be modified once they have been initialized. Once a final variable has been assigned a value, it cannot be changed. This ensures that the value of the variable remains constant throughout the program execution. Declaring a variable as final also prevents any accidental changes to its value, providing a level of safety and immutability to the variable. Therefore, the correct answer is that final instance variables cannot be modified.
25.
A final field should also be declared ________ if it is initialized in its declaration.
Correct Answer
D. Static
Explanation
A final field should also be declared as static if it is initialized in its declaration. This is because a final field is a constant and cannot be changed once it is assigned a value. By declaring it as static, the field becomes a class-level variable that is shared among all instances of the class. This ensures that the value of the final field is consistent across all instances and can be accessed without creating an instance of the class.
26.
Java programmers do not focus on:
Correct Answer
B. Understanding class library implementations
Explanation
Java programmers do not focus on understanding class library implementations. While they may craft new classes, reuse existing classes, test and document their own classes, their main focus is not on understanding how the class library is implemented. The class library is a collection of pre-written classes and methods that programmers can use to build their applications. Java programmers rely on the class library to provide functionality and do not necessarily need to understand the inner workings of the library itself.
27.
Which of the following does not contribute to improved software reusability?
Correct Answer
A. Quickly creating new class libraries without testing them thoroughly
Explanation
Quickly creating new class libraries without testing them thoroughly does not contribute to improved software reusability because without thorough testing, there is a higher chance of introducing bugs or errors in the library. This can lead to unreliable or unstable code, making it difficult to reuse the library effectively. Testing is crucial to ensure the quality and reliability of the software components being reused, and skipping this step can hinder the reusability of the code.
28.
Abstract Data Types:
Correct Answer
D. All of the above.
Explanation
The answer "All of the above" is correct because abstract data types do elevate the importance of data by providing a way to organize and manipulate it effectively. They are also approximations or models of real-world concepts and behaviors, as they represent and mimic real-world entities and their interactions. Additionally, abstract data types capture two notions: data representation, which defines how the data is structured and stored, and operations, which define the actions that can be performed on the data. Therefore, all the statements mentioned in the options are true.
29.
The term “information hiding” refers to:
Correct Answer
B. Hiding implementation details from clients of a class
Explanation
The term "information hiding" refers to the practice of concealing the internal workings and implementation details of a class from its clients. This is done to promote encapsulation and abstraction, allowing the class to change its implementation without affecting the clients that use it. By hiding implementation details, the class provides a clear and simplified interface to its clients, reducing complexity and improving maintainability.
30.
A package is:
Correct Answer
D. All
Explanation
The correct answer is "All". A package is all of the above - it is a directory structure used to organize classes and interfaces, a mechanism for software reuse, and a group of related classes and interfaces. Packages help in organizing code, promoting reusability, and providing a logical structure to the project.
31.
A class within a package must be declared public if
Correct Answer
B. It will be used by classes that are not in the same package
Explanation
When a class within a package needs to be used by classes that are not in the same package, it must be declared as public. This allows other classes outside of the package to access and use the class. By declaring the class as public, it becomes visible to other packages and can be utilized as needed.
32.
Consider the statement package com.deitel.jhtp6.ch08; Which of the following is true?
Correct Answer
B. The statement uses the Sun Microsystems convention of package naming
Explanation
The given statement "package com.deitel.jhtp6.ch08;" uses the Sun Microsystems convention of package naming. This convention suggests that the package name should be in reverse domain name format, starting with a top-level domain such as com, followed by subsequent levels of the domain, and ending with the specific package name. In this case, the package name suggests that it belongs to the Deitel company and is related to the Java How to Program (jhtp6) book, specifically chapter 8.
33.
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.
Correct Answer
C. -d.
Explanation
The javac command-line option "-d" is used to specify the directory where the compiled class files should be placed. When compiling a class in a package, this option causes the javac compiler to create appropriate directories based on the class's package declaration. This ensures that the compiled class file is placed in the correct directory structure that matches the package structure.
34.
The import declaration import java.util.*; is known as a ________.
Correct Answer
D. Type-import-on-demand declaration
Explanation
The import declaration "import java.util.*;" is known as a type-import-on-demand declaration because it allows all types in the "java.util" package to be imported into the current source file. This means that any class, interface, or enum within the "java.util" package can be used without having to specify the package name every time.
35.
The import declaration import *; ________.
Correct Answer
A. Causes a compilation error
Explanation
The import declaration "import *;" causes a compilation error because the asterisk (*) is not a valid syntax for importing all classes in a library. The correct syntax for importing all classes is "import library.*;".
36.
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.
Correct Answer
B. Semicolon (;), colon (:).
Explanation
The classpath consists of a list of directories or archive files, each separated by a semicolon (;) on Windows or a colon (:) on UNIX/Linux/Max OS X.
37.
By default, the classpath consists only of the ________. However, the classpath can be modified by providing the ________ option to the javac compiler.
Correct Answer
D. Current directory, -classpath.
Explanation
The classpath is a parameter that tells the Java Virtual Machine (JVM) where to look for classes and packages. By default, the classpath consists of the current directory. However, it can be modified by providing the -classpath option to the javac compiler. This allows developers to specify additional directories or JAR files to be included in the classpath. Therefore, the correct answer is current directory, -classpath.
38.
When no access modifier is specified for a method or variable, the method or variable:
Correct Answer
C. Has package access
Explanation
When no access modifier is specified for a method or variable, it means that the method or variable has package access. This means that it can be accessed within the same package but not from outside the package. It is not public, private, or static, but rather has a default level of access within its package.