Java Ch10

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,682
Questions: 27 | Attempts: 1,658

SettingsSettingsSettings
Java Quizzes & Trivia

Java ch10


Questions and Answers
  • 1. 

    10.1 Q1: Polymorphism enables you to:

    • A.

      A. program in the general.

    • B.

      B. program in the specific.

    • C.

      C. absorb attributes and behavior from previous classes.

    • D.

      D. hide information from the user. d. hide information from the user.

    Correct Answer
    A. A. program in the general.
    Explanation
    Polymorphism allows you to program in the general, meaning that you can write code that can work with objects of different types without needing to know the specific type at compile time. This allows for code reusability and flexibility, as the same code can be used with different types of objects as long as they adhere to a common interface or superclass. Polymorphism enables you to write more generic and flexible code, making it easier to maintain and extend your programs.

    Rate this question:

  • 2. 

    10.1 Q2: Which of the following statements about interfaces is false?

    • A.

      A. An interface describes a set of methods that can be called on an object, providing a default implementation for the methods.

    • B.

      B. An interface describes a set of methods that can be called on an object, not providing concrete implementation for the methods.

    • C.

      C. Interfaces are useful when attempting to assign common functionality to possibly unrelated classes.

    • D.

      D. Once a class implements an interface, all objects of that class have an is-a relationship with the interface type.

    Correct Answer
    A. A. An interface describes a set of methods that can be called on an object, providing a default implementation for the methods.
    Explanation
    Option a is false because an interface does not provide a default implementation for the methods. It only describes the set of methods that should be implemented by the classes that implement the interface. The classes implementing the interface are responsible for providing their own implementation for the methods.

    Rate this question:

  • 3. 

    10.2 Q1: For which of the following would polymorphism not provide a clean solution?

    • A.

      A. A billing program where there is a variety of client types that are billed with different fee structures.

    • B.

      B. A maintenance log program where data for a variety of types of machines is collected and maintenance schedules are produced for each machine based on the data collected.

    • C.

      C. A program to compute a 5% savings account interest for a variety of clients.

    • D.

      D. An IRS program that maintains information on a variety of taxpayers and determines who to audit based on criteria for classes of taxpayers.

    Correct Answer
    C. C. A program to compute a 5% savings account interest for a variety of clients.
    Explanation
    Polymorphism allows objects of different classes to be treated as objects of a common superclass. In option c, a program to compute a 5% savings account interest for a variety of clients, there is no need for different classes or different behavior based on the type of client. The calculation of the interest is the same for all clients, so there is no need for polymorphism in this scenario.

    Rate this question:

  • 4. 

    10.2 Q2: Polymorphism allows for specifics to be dealt with during:

    • A.

      A. execution.

    • B.

      B. compilation.

    • C.

      C. programming.

    • D.

      D. debugging.

    Correct Answer
    A. A. execution.
    Explanation
    Polymorphism allows for specifics to be dealt with during execution. This means that during the runtime of a program, the appropriate method or behavior is chosen based on the actual type of the object being operated on, rather than the declared type. This allows for flexibility and extensibility in code, as different objects can be treated in a generic way, while still executing specific behaviors based on their individual types.

    Rate this question:

  • 5. 

    10.3 Q1: Which statement best describes the relationship between superclass and subclass types?

    • A.

      A. A subclass reference cannot be assigned to a superclass variable and a superclass reference cannot be assigned to a subclass variable.

    • B.

      B. A subclass reference can be assigned to a superclass variable and a superclass reference can be assigned to a subclass variable.

    • C.

      C. A superclass reference can be assigned to a subclass variable, but a subclass reference cannot be assigned to a superclass variable.

    • D.

      D. A subclass reference can be assigned to a superclass variable, but a superclass reference cannot be assigned to a subclass variable.

    Correct Answer
    D. D. A subclass reference can be assigned to a superclass variable, but a superclass reference cannot be assigned to a subclass variable.
    Explanation
    In object-oriented programming, a subclass is a specialized version of a superclass. Therefore, a subclass reference can be assigned to a superclass variable because the subclass inherits all the properties and methods of the superclass. However, a superclass reference cannot be assigned to a subclass variable because the subclass may have additional properties and methods that are not present in the superclass. Thus, option d correctly describes the relationship between superclass and subclass types.

    Rate this question:

  • 6. 

    10.4 Q1: A(n)          class cannot be instantiated.

    • A.

      A. final.

    • B.

      B. concrete.

    • C.

      C. abstract.

    • D.

      D. polymorphic.

    Correct Answer
    C. C. abstract.
    Explanation
    An abstract class cannot be instantiated because it is meant to be a base class for other classes. It is designed to be inherited by other classes and provides a blueprint for those classes to follow. Abstract classes contain one or more abstract methods, which are meant to be implemented by the subclasses. Therefore, an abstract class cannot be instantiated on its own as it is incomplete and lacks the necessary implementation details for its methods.

    Rate this question:

  • 7. 

    10.4 Q2: Non-abstract classes are called:

    • A.

      A. real classes.

    • B.

      B. instance classes.

    • C.

      C. implementable classes.

    • D.

      D. concrete classes.

    Correct Answer
    D. D. concrete classes.
    Explanation
    Non-abstract classes are called concrete classes because they provide specific implementations for all their methods and can be instantiated to create objects. Unlike abstract classes, concrete classes can be directly used to create objects and are fully implemented with all their methods defined. They are considered concrete because they provide the actual implementation details and can be used to create instances or objects in a program.

    Rate this question:

  • 8. 

    10.5 Q1: It is a UML convention to denote the name of an abstract class in:

    • A.

      A. bold.

    • B.

      B. italics.

    • C.

      C. a diamond.

    • D.

      D. there is no convention of the UML to denote abstract classes—they are listed just as any other class.

    Correct Answer
    B. B. italics.
    Explanation
    In UML, the convention to denote the name of an abstract class is to use italics. This helps to visually distinguish abstract classes from concrete classes.

    Rate this question:

  • 9. 

    10.5 Q2: If the superclass contains only abstract method declarations, the superclass is used for:

    • A.

      A. implementation inheritance.

    • B.

      B. interface inheritance.

    • C.

      C. Both. d. Neither.

    • D.

      C. Both.

    Correct Answer
    B. B. interface inheritance.
    Explanation
    If the superclass contains only abstract method declarations, it means that the superclass is an interface rather than a concrete class. In Java, interfaces are used to define a contract of methods that a class implementing the interface must provide. Therefore, if the superclass contains only abstract method declarations, it is used for interface inheritance, where other classes can implement the interface and provide their own implementation for those abstract methods.

    Rate this question:

  • 10. 

    10.5.1 Q1: Which of the following could be used to declare abstract method method1 in abstract class Class1 (method1 returns an int and takes no arguments)?

    • A.

      A. public int method1();

    • B.

      B. public int abstract method1();

    • C.

      C. public abstract int method1();

    • D.

      D. public int nonfinal method1();

    Correct Answer
    C. C. public abstract int method1();
    Explanation
    The correct answer is c. public abstract int method1(). This is because in an abstract class, abstract methods are declared using the "abstract" keyword. The method1() in this case is abstract because it is declared with the "abstract" keyword and it has no implementation. Additionally, the return type "int" and the absence of any arguments match the requirements stated in the question.

    Rate this question:

  • 11. 

    10.5.1 Q2: Which of the following statements about abstract superclasses is true?

    • A.

      A. abstract superclasses may contain data.

    • B.

      B. abstract superclasses may not contain implementations of methods.

    • C.

      C. abstract superclasses must declare all methods as abstract.

    • D.

      D. abstract superclasses must declare all data members not given values as abstract.

    Correct Answer
    A. A. abstract superclasses may contain data.
    Explanation
    Abstract superclasses are classes that cannot be instantiated and are meant to be extended by subclasses. They can contain data members, just like any other class. The purpose of an abstract superclass is to provide a blueprint for its subclasses, so it can have data members that are common to all subclasses. The subclasses can then inherit these data members and use them as needed. Therefore, option a is correct.

    Rate this question:

  • 12. 

    Consider the abstract superclass below:public abstract class Foo{   private int a;   public int b;   public Foo( int aVal, int bVal )   {      a = aVal;      b = bVal;   } // end Foo constructor   public abstract int calculate();} // end class FooAny concrete subclass that extends class Foo:

    • A.

      A. Must implement a method called calculate.

    • B.

      B. Will not be able to access the instance variable a.

    • C.

      C. Neither (a) nor (b).

    • D.

      D. Both (a) and (b)

    Correct Answer
    D. D. Both (a) and (b)
    Explanation
    The correct answer is d. Both (a) and (b). This is because any concrete subclass that extends class Foo must implement a method called calculate, as stated in the abstract superclass. Additionally, the instance variable "a" is private, so it cannot be accessed by any subclass.

    Rate this question:

  • 13. 

    10.5.5 Q1: Consider classes A, B and C, where A is an abstract superclass, B is a concrete class that inherits from A and C is a concrete class that inherits from B. Class A declares abstract method originalMethod, implemented in class B. Which of the following statements is true of class C?

    • A.

      A. Method originalMethod cannot be overridden in class C—once it has been implemented in concrete class B, it is implicitly final.

    • B.

      B. Method originalMethod must be overridden in class C, or a syntax error will occur.

    • C.

      C. If method originalMethod is not overridden in class C but is called by an object of class C, an error occurs.

    • D.

      D. None of the above.

    Correct Answer
    D. D. None of the above.
  • 14. 

    10.5.6 Q1: When a superclass variable refers to a subclass object and a method is called on that object, the proper implementation is determined at execution time. What is the process of determining the correct method to call?

    • A.

      A. execution time binding.

    • B.

      B. execution binding.

    • C.

      C. just-in-time binding.

    • D.

      D. late binding.

    Correct Answer
    D. D. late binding.
    Explanation
    The process of determining the correct method to call when a superclass variable refers to a subclass object at execution time is known as late binding. Late binding allows the program to dynamically determine the appropriate implementation of the method based on the actual type of the object being referred to. This allows for flexibility and polymorphism in object-oriented programming.

    Rate this question:

  • 15. 

    10.5.6 Q2: Every object in Java knows its own class and can access this information through method         .

    • A.

      A. getClass.

    • B.

      B. getInformation.

    • C.

      C. objectClass.

    • D.

      D. objectInformation.

    Correct Answer
    A. A. getClass.
    Explanation
    In Java, every object knows its own class and can access this information through the method "getClass". This method returns a Class object which represents the runtime class of the object. It can be used to retrieve information about the object's class, such as its name, superclass, interfaces, annotations, and more. Therefore, option a, "getClass", is the correct answer.

    Rate this question:

  • 16. 

    10.5.7 Q1: Assigning a subclass reference to a superclass variable is safe:

    • A.

      A. because the subclass object has an object of its superclass.

    • B.

      B. because the subclass object is an object of its superclass.

    • C.

      C. only when the superclass is abstract.

    • D.

      D. only when the superclass is concrete.

    Correct Answer
    B. B. because the subclass object is an object of its superclass.
    Explanation
    Assigning a subclass reference to a superclass variable is safe because the subclass object is an object of its superclass. This means that the subclass inherits all the properties and behaviors of its superclass, so it can be treated as an object of its superclass without any issues.

    Rate this question:

  • 17. 

    10.6 Q1: Classes and methods are declared final for all but the following reasons:

    • A.

      A. final methods allow inlining the code.

    • B.

      B. final methods and classes prevent further inheritance.

    • C.

      C. final methods are static.

    • D.

      D. final methods can improve performance.

    Correct Answer
    C. C. final methods are static.
    Explanation
    Final methods and classes prevent further inheritance because they cannot be overridden or extended by subclasses. This is one of the main reasons why classes and methods are declared final. Final methods allow inlining the code, which means that the method code is directly inserted into the calling code, improving performance. However, the statement that final methods are static is incorrect. Final methods can be either static or non-static, but the final keyword is used to prevent the method from being overridden, not to specify whether it is static or not.

    Rate this question:

  • 18. 

    10.6 Q2: All of the following methods are implicitly final except:

    • A.

      A. a method in an abstract class.

    • B.

      B. a private method.

    • C.

      C. a method declared in a final class.

    • D.

      D. static method.

    Correct Answer
    A. A. a method in an abstract class.
    Explanation
    The correct answer is a method in an abstract class. This is because methods in an abstract class are meant to be overridden by the subclasses, so they are not implicitly final. In contrast, private methods, methods declared in a final class, and static methods are implicitly final and cannot be overridden.

    Rate this question:

  • 19. 

    10.6 Q3: Declaring a method final means:

    • A.

      A. it will prepare the object for garbage collection.

    • B.

      B. it cannot be accessed from outside its class.

    • C.

      C. it cannot be overloaded.

    • D.

      D. it cannot be overridden.

    Correct Answer
    D. D. it cannot be overridden.
    Explanation
    Declaring a method as final means that it cannot be overridden by any subclass. This is useful when a method in a superclass should not be modified or extended in any way by its subclasses. By declaring a method as final, it ensures that the implementation of the method in the superclass is the final and definitive version that cannot be changed or overridden by any subclass.

    Rate this question:

  • 20. 

    10.7 Q1: An interface may contain:

    • A.

      A. private static data and public abstract methods.

    • B.

      B. only public abstract methods.

    • C.

      C. public static final data and public abstract methods.

    • D.

      D. private static data and public final methods.

    Correct Answer
    C. C. public static final data and public abstract methods.
    Explanation
    An interface in Java can contain public static final data and public abstract methods. Public static final data represents constants that can be accessed by other classes. Public abstract methods are methods without a body that must be implemented by classes that implement the interface. The combination of these two elements allows an interface to define constants and specify the behavior that implementing classes must follow.

    Rate this question:

  • 21. 

    Which of the following does not complete the sentence correctly?An interface         .

    • A.

      A. forces classes that implement it to declare all the interface methods.

    • B.

      B. can be used in place of an abstract class when there is no default implementation to inherit.

    • C.

      C. is declared in a file by itself and is saved in a file with the same name as the interface followed by the .java extension.

    • D.

      D. can be instantiated.

    Correct Answer
    D. D. can be instantiated.
    Explanation
    An interface cannot be instantiated because it is an abstract concept that defines a set of methods that implementing classes must provide. It cannot be used to create objects directly.

    Rate this question:

  • 22. 

    10.7.1 Q1: The UML distinguishes an interface from other classes by placing the word “interface” in          above the interface name.

    • A.

      A. italics.

    • B.

      B. carets.

    • C.

      C. guillemets.

    • D.

      D. bold.

    Correct Answer
    C. C. guillemets.
    Explanation
    The UML distinguishes an interface from other classes by placing the word "interface" in guillemets above the interface name.

    Rate this question:

  • 23. 

    10.7.2 Q1: Interfaces can have          methods.

    • A.

      0

    • B.

      1

    • C.

      2

    • D.

      D. any number of

    Correct Answer
    D. D. any number of
    Explanation
    Interfaces in programming languages can have any number of methods. This means that an interface can define any number of abstract methods that must be implemented by any class that implements the interface. This allows for flexibility and customization in the implementation of the interface, as different classes can choose to implement different subsets of the methods defined in the interface. Therefore, the correct answer is "d. any number of".

    Rate this question:

  • 24. 

    10.7.3 Q1: Which keyword is used to specify that a class will define the methods of an interface?

    • A.

      A. uses.

    • B.

      B. implements.

    • C.

      C. defines.

    • D.

      D. extends.

    Correct Answer
    B. B. implements.
    Explanation
    The keyword "implements" is used to specify that a class will define the methods of an interface. When a class implements an interface, it is required to provide implementations for all the methods declared in that interface. This allows the class to fulfill the contract defined by the interface and ensures that the class can be used interchangeably with other classes that implement the same interface. The "implements" keyword is a fundamental concept in object-oriented programming and is used to achieve interface implementation and polymorphism.

    Rate this question:

  • 25. 

    10.7.3 Q2: Which of the following is not possible?

    • A.

      A. A class that implements two interfaces.

    • B.

      B. A class that inherits from two classes.

    • C.

      C. A class that inherits from one class, and implements an interface.

    • D.

      D. All of the above are possible.

    Correct Answer
    B. B. A class that inherits from two classes.
    Explanation
    A class can implement multiple interfaces (option a) and inherit from one class while implementing an interface (option c). However, a class cannot inherit from two classes simultaneously (option b). This is because multiple inheritance is not allowed in most programming languages, including Java.

    Rate this question:

  • 26. 

    10.7.4 Q1: A class that implements an interface but does not declare all of the interface’s methods must be declared:

    • A.

      A. public.

    • B.

      B. interface.

    • C.

      C. abstract.

    • D.

      D. final.

    Correct Answer
    C. C. abstract.
    Explanation
    When a class implements an interface but does not declare all of the interface's methods, it means that the class is not providing concrete implementations for all the methods required by the interface. In this case, the class must be declared as abstract because it cannot be instantiated directly. Abstract classes are meant to be extended by other classes that will provide the missing implementations for the interface's methods.

    Rate this question:

  • 27. 

    10.7.7 Q1: Constants declared in an interface are implicitly         .

    • A.

      A. private.

    • B.

      B. static.

    • C.

      C. abstract.

    • D.

      D. All of the above.

    Correct Answer
    B. B. static.
    Explanation
    Constants declared in an interface are implicitly static. In Java, constants in an interface are implicitly static and final, meaning they cannot be changed and are associated with the class rather than an instance of the class. The static keyword allows the constants to be accessed without creating an instance of the interface.

    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 22, 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.