OCA C5 Mock Test Flashcards
6 Questions
100 Views

OCA C5 Mock Test Flashcards

Created by
@WellRunHydrogen

Questions and Answers

Which of the following may only be hidden and not overridden? (Choose all that apply)

  • Public variables (correct)
  • Public instance methods
  • Protected instance methods
  • Private variables (correct)
  • Private instance methods (correct)
  • Static methods (correct)
  • Which statements are true for both abstract classes and interfaces? (Choose all that apply)

  • Both can contain public static final variables. (correct)
  • Both can contain default methods.
  • Both can be extended using the extend keyword. (correct)
  • Neither can be instantiated directly. (correct)
  • Both inherit java.lang.Object.
  • Both can contain static methods. (correct)
  • All methods within them are assumed to be abstract.
  • What is the output of the following code?

    The code will not compile due to an invalid definition of an abstract method.

    What happens when the following code is executed?

    <p>The code doesn't compile due to an incompatible override of the getNumberOfGills method.</p> Signup and view all the answers

    Which of the following statements can be inserted in the blank so that the code will compile successfully? (Choose all that apply)

    <p>new Snake()</p> Signup and view all the answers

    What is the result of the following code snippet?

    <p>Information about the code is incomplete.</p> Signup and view all the answers

    Study Notes

    Method Overriding and Hiding

    • Private instance methods in subclasses are hidden, not overridden, making option A correct.
    • Static methods cannot be overridden; they are only hidden, confirming option D.
    • Public and protected instance methods can be overridden, which excludes options B and C.
    • Variables, regardless of access modifiers, may only be hidden, verifying options E and F.

    Abstract Classes vs. Interfaces

    • Both abstract classes and interfaces can contain public static final variables, confirming option B.
    • Interfaces can include concrete methods since Java 8, making option A incorrect.
    • The extends keyword can be used with both, validating option C.
    • Only interfaces can have default methods, which disproves option D.
    • Both can contain static methods, affirming option E.
    • Neither abstract classes nor interfaces can be instantiated directly, thus option F is correct.
    • Interfaces do not inherit java.lang.Object directly, invalidating option G.

    Abstract Method Compilation Issues

    • Abstract methods must not contain a body, such as in line 2 with public abstract void dive() {}.
    • If the body were removed from line 2, the code still wouldn't compile due to the incorrect implementation in subclass Orca.
    • The method dive(int depth) in Orca is considered an overload, not an override of the abstract method in Whale.

    Method Overriding Compatibility

    • The getNumberOfGills(int input) in ClownFish fails to compile due to incompatible return types; it attempts to override with String as a return type instead of int.
    • The method getNumberOfGills() in line 5 is an overload with different parameters and compiles correctly.

    Object Compatibility in Method Parameters

    • The parameter for setSnake(Snake snake) can accept:
      • A new instance of Cobra (option A) since it extends Snake.
      • A new Snake instance (option C) since Snake is not abstract.
      • Null (option F) is acceptable for any object type.
    • GardenSnake (option B) is unrelated to Snake, and generating it results in a compile error.
    • Object (option D) and String (option E) are not valid types as they are not subclasses of Snake.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Description

    Test your knowledge on Object-Oriented principles with these OCA C5 mock test flashcards. This quiz focuses on the nuances of method visibility and overriding in Java. Enhance your understanding of private, protected, and public methods with this engaging set of questions.

    Use Quizgecko on...
    Browser
    Browser