Ch8 Java

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Bing22
B
Bing22
Community Contributor
Quizzes Created: 3 | Total Attempts: 4,680
Questions: 38 | Attempts: 1,417

SettingsSettingsSettings
Java Quizzes & Trivia

Questions and Answers
  • 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.

    • A.

      Public constructors.

    • B.

      public instance variables.

    • C.

      public methods

    • D.

      All of the above.

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

    Rate this question:

  • 2. 

    The static method ________ of class String returns a formatted String.

    • A.

      Printf

    • B.

      Format

    • C.

      FormatString

    • D.

      ToFormatString

    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.

    Rate this question:

  • 3. 

    Which statement is false?

    • A.

      The actual data representation used within the class is of no concern to the class’s clients

    • B.

      Clients generally care about what the class does but not how the class does it

    • C.

      Clients are usually involved in a class’s implementation

    • D.

      Hiding the implementation reduces the possibility that clients will become dependent on class-implementation details

    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.

    Rate this question:

  • 4. 

    Which of the following should usually be private?

    • A.

      Methods.

    • B.

      Constructors

    • C.

      Variables (or fields).

    • D.

      All of the above.

    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.

    Rate this question:

  • 5. 

    Which of the following statements is true?

    • A.

      Methods and instance variables can both be either public or private.

    • B.

      Information hiding is achieved by restricting access to class members via keyword public.

    • C.

      The private members of a class are directly accessible to the client of a class.

    • D.

      None of the above 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.

    Rate this question:

  • 6. 

    When should a program explicitly use the this reference?

    • A.

      Accessing a private variable

    • B.

      Accessing a public variable

    • C.

      Accessing a local variable.

    • D.

      Accessing a field that is shadowed by a local variable

    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.

    Rate this question:

  • 7. 

    Having a this reference allows:

    • A.

      A method to refer explicitly to the instance variables and other methods of the object on which the method was called.

    • B.

      A method to refer implicitly to the instance variables and other methods of the object on which the method was called.

    • C.

      An object to reference itself.

    • D.

      All of the above.

    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.

    Rate this question:

  • 8. 

     A constructor cannot:

    • A.

      Be overloaded.

    • B.

      Initialize variables to their defaults.

    • C.

      Specify return types or return values.

    • D.

      Have the same name as the class.

    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.

    Rate this question:

  • 9. 

    8.5 Q2: Constructors:

    • A.

      Initialize instance variables.

    • B.

      When overloaded, can have identical argument lists.

    • C.

      When overloaded, are selected by number, types and order of types of parameters.

    • D.

      A and c.

    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.

    Rate this question:

  • 10. 

     A programmer-defined constructor that has no arguments is called a ________.

    • A.

      Zero-argument constructor.

    • B.

      No-argument constructor.

    • C.

      Default constructor

    • D.

      Main constructor.

    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.

    Rate this question:

  • 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?

    • A.

      A compilation error occurs.

    • B.

      A runtime error occurs.

    • C.

      A logic error occurs.

    • D.

      Nothing happens. The program compiles and runs.

    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.

    Rate this question:

  • 12. 

    When implementing a method, use the class’s set and get methods to access the class’s ________ data.

    • A.

      Public

    • B.

      Private

    • C.

      Protected

    • D.

      All of the above

    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.

    Rate this question:

  • 13. 

    Which statement is false?

    • A.

      The compiler always creates a default constructor for a class.

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

      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.

      None.

    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.

    Rate this question:

  • 14. 

    Set methods are also commonly called ________ methods and get methods are also commonly called ________ methods.

    • A.

      Query, mutator.

    • B.

      Accessor, mutator.

    • C.

      Mutator, accessor.

    • D.

      Query, accessor.

    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.

    Rate this question:

  • 15. 

    Using public set methods provides data integrity if:

    • A.

      The instance variables are public.

    • B.

      The instance variables are private

    • C.

      The methods perform validity checking

    • D.

      Both b and c.

    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.

    Rate this question:

  • 16. 

     Composition is sometimes referred to as a(n) ________.

    • A.

      Is-a relationship.

    • B.

      Has-a relationship.

    • C.

      Many-in-one relationship.

    • D.

      One-to-many relationship

    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.

    Rate this question:

  • 17. 

    : enum types are implicitly ________ and enum constants are implicitly ________.

    • A.

      Final, static.

    • B.

      Static, static.

    • C.

      Static, final.

    • D.

      Final, final

    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.

    Rate this question:

  • 18. 

     Which statement is false?

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

      Any attempt to create an object of an enum type with operator new results in a compilation error.

    • C.

      An enum constructor cannot be overloaded

    • D.

      An enum constructor can specify any number of parameters

    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.

    Rate this question:

  • 19. 

    Which method returns an array of the enum’s constants?

    • A.

      Values

    • B.

      GetValues

    • C.

      Constants

    • D.

      GetConstants

    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.

    Rate this question:

  • 20. 

    Which of the following is false?

    • A.

      Method finalize does not take parameters and has return type void.

    • B.

      Memory leaks using Java are rare because of automatic garbage collection

    • C.

      Objects are marked for garbage collection by method finalize.

    • D.

      The garbage collector reclaims unused memory

    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.

    Rate this question:

  • 21. 

    Static class variables:

    • A.

      Are final.

    • B.

      Are public

    • C.

      Are private

    • D.

      Are shared by all objects of a class

    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.

    Rate this question:

  • 22. 

    Which of the following is false?

    • A.

      A static method must be used to access private static instance variables

    • B.

      A static method has no this reference

    • C.

      A static method can be accessed even when no objects of its class have been instantiated

    • D.

      A static method can call instance methods directly

    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.

    Rate this question:

  • 23. 

     Which syntax imports all static members of class Math?

    • A.

      Static import java.lang.Math.*.

    • B.

      Import static java.lang.Math.*.

    • C.

      static import java.lang.Math

    • D.

      Import static java.lang.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.

    Rate this question:

  • 24. 

     Instance variables declared final do not or cannot:

    • A.

      Cause syntax errors if used as a left-hand value.

    • B.

      Be initialized

    • C.

      Be modified

    • D.

      None

    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.

    Rate this question:

  • 25. 

    A final field should also be declared ________ if it is initialized in its declaration.

    • A.

      Private

    • B.

      Public

    • C.

      Protected

    • D.

      Static

    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.

    Rate this question:

  • 26. 

     Java programmers do not focus on:

    • A.

      Crafting new classes and reusing existing classes.

    • B.

      Understanding class library implementations

    • C.

      Carefully testing classes they design

    • D.

      Carefully documenting classes they design

    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.

    Rate this question:

  • 27. 

     Which of the following does not contribute to improved software reusability?

    • A.

      Quickly creating new class libraries without testing them thoroughly

    • B.

      Licensing schemes and protection mechanisms

    • C.

      Descriptions of classes that allow programmers to determine whether a class fits their needs

    • D.

      Cataloging schemes and browsing mechanisms

    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.

    Rate this question:

  • 28. 

    Abstract Data Types:

    • A.

      Elevate the importance of data

    • B.

      Are only approximations or models of real-world concepts and behaviors

    • C.

      Capture two notions, data representation and operations

    • D.

      All of the above.

    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.

    Rate this question:

  • 29. 

     The term “information hiding” refers to:

    • A.

      Public methods

    • B.

      Hiding implementation details from clients of a class

    • C.

      Accessing static class members

    • D.

      The process of releasing an object for garbage collection

    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.

    Rate this question:

  • 30. 

    A package is:

    • A.

      A directory structure used to organize classes and interfaces.

    • B.

      A mechanism for software reuse

    • C.

      A group of related classes and interfaces

    • D.

      All

    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.

    Rate this question:

  • 31. 

     A class within a package must be declared public if

    • A.

      It will be used only by other classes in the same package

    • B.

      It will be used by classes that are not in the same package

    • C.

      It is in the same directory as the other classes in the package.

    • D.

      It has a unique name

    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.

    Rate this question:

  • 32. 

     Consider the statement    package com.deitel.jhtp6.ch08;    Which of the following is true?

    • A.

      The statement declares a package that exists at deitel.com

    • B.

      The statement uses the Sun Microsystems convention of package naming

    • C.

      The statement should be placed inside the class declaration

    • D.

      The statement will generate a compile time error

    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.

    Rate this question:

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

    • A.

      -p.

    • B.

      -a.

    • C.

      -d.

    • D.

      -dir.

    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.

    Rate this question:

  • 34. 

    The import declaration import java.util.*; is known as a ________.

    • A.

      Single-type-import declaration.

    • B.

      All-type-import declaration

    • C.

      Multiple-import declaration

    • D.

      Type-import-on-demand declaration

    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.

    Rate this question:

  • 35. 

     The import declaration import *; ________.

    • A.

      Causes a compilation error

    • B.

      Imports all classes in the library.

    • C.

      Imports the default classes in the library

    • D.

      Imports the classes in package java.lang

    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.*;".

    Rate this question:

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

    • A.

      Colon (:), semicolon (;).

    • B.

      Semicolon (;), colon (:).

    • C.

      Comma (,), semicolon (;).

    • D.

      Semicolon (;), comma (,).

    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.

    Rate this question:

  • 37. 

     By default, the classpath consists only of the ________. However, the classpath can be modified by providing the ________ option to the javac compiler.

    • A.

      Root directory of the package, -d.

    • B.

      Current directory, -d.

    • C.

      Root directory of the package, -classpath

    • D.

      Current directory, -classpath.

    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.

    Rate this question:

  • 38. 

    When no access modifier is specified for a method or variable, the method or variable:

    • A.

      Is public

    • B.

      Is private

    • C.

      Has package access

    • D.

      Is static

    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.

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Mar 21, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Dec 12, 2010
    Quiz Created by
    Bing22
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.