Java Ch9

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,686
Questions: 23 | Attempts: 1,607

SettingsSettingsSettings
Java Quizzes & Trivia

Java ch9


Questions and Answers
  • 1. 

     Which of the following statements is false? 

    • A.

      A subclass is generally larger than its superclass.

    • B.

      A superclass object is a subclass object.

    • C.

      The class following the extends keyword in a class declaration is the direct superclass of the class being declared.

    • D.

      Java uses interfaces to provide the benefits of multiple inheritance.

    Correct Answer
    B. A superclass object is a subclass object.
    Explanation
    The given statement "A superclass object is a subclass object" is false. In object-oriented programming, a superclass is a class from which other classes, called subclasses, inherit properties and methods. A subclass can have additional properties and methods that are not present in the superclass. Therefore, a superclass object cannot be a subclass object because it does not have the additional properties and methods defined in the subclass.

    Rate this question:

  • 2. 

     Inheritance is also known as the

    • A.

      “knows-a” relationship.

    • B.

      “has-a” relationship.

    • C.

      “uses-a” relationship.

    • D.

      “is-a” relationship.

    Correct Answer
    D. “is-a” relationship.
    Explanation
    The correct answer is "is-a" relationship because inheritance in object-oriented programming represents a relationship between classes where one class inherits the properties and behaviors of another class. This relationship implies that the inheriting class is a specialized version of the inherited class, indicating an "is-a" relationship.

    Rate this question:

  • 3. 

     Which of the following is not a superclass/subclass relationship?

    • A.

      Ford/Taurus.

    • B.

      University/Brown University

    • C.

      Sailboat/Tugboat

    • D.

      Country/USA

    Correct Answer
    C. Sailboat/Tugboat
    Explanation
    The relationship between a sailboat and a tugboat is not a superclass/subclass relationship because they are not related in terms of inheritance or specialization. A superclass/subclass relationship implies that one class is a more specific version or specialization of another class. In this case, a sailboat and a tugboat are different types of boats with different purposes and characteristics, rather than one being a specialized version of the other.

    Rate this question:

  • 4. 

    : An advantage of inheritance is that:

    • A.

      All methods can be inherited

    • B.

      All instance variables can be uniformly accessed by subclasses and superclasses.

    • C.

      Objects of a subclass can be treated like objects of their superclass.

    • D.

      None of the above.

    Correct Answer
    C. Objects of a subclass can be treated like objects of their superclass.
    Explanation
    Inheritance allows objects of a subclass to be treated like objects of their superclass. This means that a subclass can inherit the properties and behaviors of its superclass, allowing it to access and use the same methods and variables. This promotes code reuse and allows for more efficient and organized programming, as subclasses can inherit and build upon the functionality of their superclass without having to redefine it.

    Rate this question:

  • 5. 

    Which of the following keywords allows a subclass to access a superclass method even when the subclass has overridden the superclass method?

    • A.

      base.

    • B.

      This

    • C.

      Public

    • D.

      Super

    Correct Answer
    D. Super
    Explanation
    The keyword "super" allows a subclass to access a superclass method even when the subclass has overridden the superclass method. By using "super" keyword, the subclass can invoke the superclass method and perform additional operations or modifications on it. This is useful when the subclass wants to extend the functionality of the superclass method without completely overriding it.

    Rate this question:

  • 6. 

    Using the protected keyword gives a member:

    • A.

      Public access.

    • B.

      B. package access.

    • C.

      C. private access.

    • D.

      D. block scope.

    Correct Answer
    B. B. package access.
    Explanation
    The protected keyword in Java gives a member package access, meaning that it can be accessed by any class within the same package. This means that the member is not accessible to classes in different packages, but can be accessed by any class within the same package, including subclasses.

    Rate this question:

  • 7. 

    Superclass methods with this level of access cannot be called from subclasses.

    • A.

      A. private.

    • B.

      B. public.

    • C.

      C. protected.

    • D.

      D. package.

    Correct Answer
    A. A. private.
    Explanation
    Superclass methods with private access cannot be called from subclasses. The private access modifier restricts access to only within the same class, so subclasses cannot access these methods.

    Rate this question:

  • 8. 

     Every class in Java, except ________, extends an existing class.

    • A.

      A. Integer.

    • B.

      B. Object.

    • C.

      C. String.

    • D.

      D. Class.

    Correct Answer
    B. B. Object.
    Explanation
    Every class in Java, except Object, extends an existing class. The Object class is the root of the class hierarchy in Java and serves as the base class for all other classes. This means that all other classes directly or indirectly inherit from the Object class. Therefore, the correct answer is b. Object.

    Rate this question:

  • 9. 

    Overriding a method differs from overloading a method because:

    • A.

      A. Overloaded methods have the same signature.

    • B.

      B. Overridden methods have the same signature.

    • C.

      C. Both of the above.

    • D.

      D. Neither of the above.

    Correct Answer
    B. B. Overridden methods have the same signature.
    Explanation
    Overriding a method means providing a different implementation of a method in a subclass, while keeping the same method signature as the method in the superclass. In other words, the overridden method in the subclass has the same name, return type, and parameters as the method in the superclass. On the other hand, overloading a method means having multiple methods with the same name but different parameters in the same class. Therefore, the correct answer is b. Overridden methods have the same signature.

    Rate this question:

  • 10. 

    9.4.2 Q1: To avoid duplicating code, use ________, rather than ________.

    • A.

      A. inheritance, the “copy-and-past” approach.

    • B.

      B. the “copy-and-past” approach, inheritance.

    • C.

      C. a class that explicitly extends Object, a class that does not extend Object.

    • D.

      D. a class that does not extend Object, a class that explicitly extends Object.

    Correct Answer
    A. A. inheritance, the “copy-and-past” approach.
    Explanation
    Inheritance allows for code reuse by creating a new class that inherits the properties and methods of an existing class. This avoids duplicating code by allowing the new class to inherit the functionality of the existing class. On the other hand, the "copy-and-paste" approach involves manually copying and pasting code from one place to another, which can lead to code duplication and make maintenance and updates more difficult. Therefore, using inheritance instead of the "copy-and-paste" approach is recommended to avoid duplicating code.

    Rate this question:

  • 11. 

    Consider the classes below, declared in the same file:class A {   int a;   public A()    {      a = 7;   }}class B extends A {   int b;   public B()    {b = 8;   }    }Which of the statements below is false?

    • A.

      A. Both variables a and b are instance variables.

    • B.

      B. After the constructor for class B executes, the variable a will have the value 7.

    • C.

      C. After the constructor for class B executes, the variable b will have the value 8.

    • D.

      D. A reference of type A can be treated as a reference of type B.

    Correct Answer
    D. D. A reference of type A can be treated as a reference of type B.
    Explanation
    Inheritance allows a reference of a superclass type to refer to an object of a subclass type. Therefore, a reference of type A can be treated as a reference of type B. This is known as upcasting.

    Rate this question:

  • 12. 

    9.4.3 Q2: Which of the following is the superclass constructor call syntax?

    • A.

      A. keyword super, followed by a dot (.) .

    • B.

      B. keyword super, followed by a set of parentheses containing the superclass constructor arguments.

    • C.

      C. keyword super, followed by a dot and the superclass constructor name.

    • D.

      D. None of the above.

    Correct Answer
    B. B. keyword super, followed by a set of parentheses containing the superclass constructor arguments.
    Explanation
    The correct answer is b. This is because when calling a superclass constructor, the syntax is "keyword super" followed by a set of parentheses containing the superclass constructor arguments. This allows the subclass to pass any necessary arguments to the superclass constructor. The other options are not the correct syntax for calling a superclass constructor.

    Rate this question:

  • 13. 

    Which superclass members are inherited by all subclasses of that superclass?

    • A.

      A. private instance variables and methods.

    • B.

      B. protected instance variables and methods.

    • C.

      C. private constructors.

    • D.

      D. protected constructors.

    Correct Answer
    B. B. protected instance variables and methods.
    Explanation
    All subclasses of a superclass inherit the protected instance variables and methods of that superclass. Protected members are accessible within the same package and also by subclasses of the superclass, regardless of the package they are in. This allows subclasses to access and use the protected members inherited from the superclass. Private instance variables and methods are not inherited by subclasses, as they are only accessible within the class they are declared in. Private constructors are also not inherited, as constructors are not inherited at all.

    Rate this question:

  • 14. 

    9.4.4 Q2: Which statement is true when a superclass has protected instance variables?

    • A.

      A. A subclass object can assign an invalid value to the superclass’s instance variables, thus leaving an object in an inconsistent state.

    • B.

      B. Subclass methods are more likely to be written so that they depend on the superclass’s data implementation.

    • C.

      C. We may need to modify all the subclasses of the superclass if the superclass implementation changes.

    • D.

      D. All of the above.

    Correct Answer
    D. D. All of the above.
    Explanation
    When a superclass has protected instance variables, it means that these variables can be accessed by the subclass. This allows the subclass object to assign invalid values to the superclass's instance variables, potentially leaving the object in an inconsistent state. Additionally, subclass methods are more likely to be written in a way that depends on the superclass's data implementation, meaning that any changes to the superclass's implementation may require modifications to all the subclasses. Therefore, all of the statements mentioned in options a, b, and c are true, making option d the correct answer.

    Rate this question:

  • 15. 

    9.4.5 Q1: private fields of a superclass can be accessed in a subclass

    • A.

      A. by calling private methods declared in the superclass.

    • B.

      B. by calling public or protected methods declared in the superclass.

    • C.

      C. directly.

    • D.

      D. All of the above.

    Correct Answer
    B. B. by calling public or protected methods declared in the superclass.
    Explanation
    Private fields of a superclass cannot be accessed directly in a subclass because they are not visible outside the class. However, they can be accessed indirectly by calling public or protected methods declared in the superclass. These methods can provide access to the private fields by returning their values or modifying them. Therefore, option b is the correct answer.

    Rate this question:

  • 16. 

    Failure to prefix the superclass method name with the keyword super and a dot (.) separator when referencing the superclass’s method causes ________.

    • A.

      A. a compile-time error.

    • B.

      B. a syntax error.

    • C.

      C. infinite recursion.

    • D.

      D. a runtime error.

    Correct Answer
    C. C. infinite recursion.
    Explanation
    When referencing the superclass's method, if the keyword "super" and a dot (.) separator are not used to prefix the superclass method name, it will cause infinite recursion. This means that the method will keep calling itself indefinitely, leading to a stack overflow error.

    Rate this question:

  • 17. 

    9.5 Q1: When a subclass constructor calls its superclass constructor, what happens if the superclass’s constructor does not assign a value to an instance variable?

    • A.

      A. A syntax error occurs.

    • B.

      B. A compile-time error occurs.

    • C.

      C. A run-time error occurs.

    • D.

      D. The program compiles and runs because the instance variables are initialized to their default values.

    Correct Answer
    D. D. The program compiles and runs because the instance variables are initialized to their default values.
    Explanation
    If the superclass's constructor does not assign a value to an instance variable, the instance variable will be initialized to its default value. In Java, instance variables are automatically assigned default values if no explicit value is given. For example, numeric types are initialized to 0, boolean types are initialized to false, and object references are initialized to null. Therefore, the program will compile and run without any errors because the instance variables will still have valid default values.

    Rate this question:

  • 18. 

    9.6 Q1: Which of the following statements is (are) true?A.    We can use inheritance to customize existing software.B.    A superclass specifies commonality.C.    A superclass can be modified without modifying subclassesD.    A subclass can be modified without modifying its superclass.

    • A.

      A. All of the above.

    • B.

      B. None of the above.

    • C.

      C. A, B and C.

    • D.

      D. A, B and D.

    Correct Answer
    A. A. All of the above.
    Explanation
    Inheritance allows us to customize existing software by creating subclasses that inherit properties and behaviors from a superclass. A superclass specifies commonality, meaning it contains shared properties and behaviors that can be inherited by subclasses. A superclass can be modified without affecting its subclasses, as long as the modifications do not change the inherited properties and behaviors. Similarly, a subclass can be modified without modifying its superclass, as long as the modifications are specific to the subclass and do not affect the inherited properties and behaviors. Therefore, all of the statements A, B, and D are true.

    Rate this question:

  • 19. 

    9.6 Q2: Which of the following is an example of a functionality that should not be “factored out” to a superclass?

    • A.

      A. Both ducks and geese are birds that know how to start flying from the water.

    • B.

      B. All vehicles know how to start and stop.

    • C.

      C. All animals lay eggs, except for mammals.

    • D.

      D. All paints have a color.

    Correct Answer
    C. C. All animals lay eggs, except for mammals.
    Explanation
    This functionality should not be factored out to a superclass because it is specific to a certain group of animals (mammals) and does not apply to all animals in general. Factoring it out to a superclass would result in incorrect behavior for mammals, as they do not lay eggs.

    Rate this question:

  • 20. 

    9.7 Q1: The default implementation of method clone of Object performs a ________.

    • A.

      A. empty copy.

    • B.

      B. deep copy.

    • C.

      C. full copy.

    • D.

      D. shallow copy.

    Correct Answer
    D. D. shallow copy.
    Explanation
    The default implementation of the clone() method in the Object class performs a shallow copy. A shallow copy creates a new object and copies the non-static fields of the original object to the new object. However, if the field is a reference type, the reference is copied, not the actual object. This means that both the original object and the cloned object will refer to the same objects. Any changes made to the shared objects will be reflected in both the original and cloned objects.

    Rate this question:

  • 21. 

    9.7 Q2: The default equals implementation determines:

    • A.

      A. whether two references refer to the same object in memory.

    • B.

      B. whether two references have the same type.

    • C.

      C. whether two objects have the same instance variables.

    • D.

      D. whether two objects have the same instance variable values.

    Correct Answer
    A. A. whether two references refer to the same object in memory.
    Explanation
    The default equals implementation in Java determines whether two references refer to the same object in memory. It checks if the memory addresses of the two objects are the same, indicating that they are the same object. It does not compare the values of the instance variables or the types of the objects.

    Rate this question:

  • 22. 

    9.8 Q1: Class ________ represents an image that can be displayed on a JLabel.

    • A.

      A. Image.

    • B.

      B. Icon.

    • C.

      C. ImageIcon.

    • D.

      D. IconImage.

    Correct Answer
    C. C. ImageIcon.
    Explanation
    The correct answer is c. ImageIcon. The ImageIcon class represents an image that can be displayed on a JLabel. It is used to create an icon from an image file and can be set as the icon for a JLabel component.

    Rate this question:

  • 23. 

    9.8 Q2: Which method changes the text the label displays?

    • A.

      A. changeText.

    • B.

      B. setText.

    • C.

      C. changeLabel.

    • D.

      D. setLabel.

    Correct Answer
    B. B. setText.
    Explanation
    The method that changes the text the label displays is "setText". This method allows the programmer to update the text content of a label component in a graphical user interface. It takes a string as an argument and replaces the current text with the new specified text.

    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
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.