📢 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