๐Ÿ“ข Too many exams? Donโ€™t know which one suits you best? Book Your Free Expert ๐Ÿ‘‰ call Now!


    Question

    Consider the following Java code: class Animal

    { ย  ย  String type = "Generic Animal"; ย  ย  void eat() { ย  ย  ย  ย  System.out.println("Animal eats food."); ย  ย  } } class Dog extends Animal { ย  ย  String type = "Dog"; ย  ย  void bark() { ย  ย  ย  ย  System.out.println("Dog barks."); ย  ย  } } public class TestInheritance { ย  ย  public static void main(String[] args) { ย  ย  ย  ย  Dog myDog = new Dog(); ย  ย  ย  ย  System.out.println(myDog.type); ย  ย  ย  ย  myDog.eat(); ย  ย  ย  ย  myDog.bark(); ย  ย  } } What will be the output of this code?
    A Dog\nAnimal eats food.\nDog barks. Correct Answer Incorrect Answer
    B Generic Animal\nAnimal eats food.\nDog barks. Correct Answer Incorrect Answer
    C Dog\nDog barks.\nAnimal eats food. Correct Answer Incorrect Answer
    D Generic Animal\nDog barks.\nAnimal eats food. Correct Answer Incorrect Answer
    E A compile-time error. Correct Answer Incorrect Answer

    Solution

    The correct answer is A

    Practice Next
    More IT Operating System Questions
    ask-question