Menu
Question Index
...

What is the output of the following program?

public class Dhoni {
    static Dhoni dhoni;
    public static void main(String... k) {
        Animal a;
        System.out.print((a = new Cat()).prey().getClass().getSimpleName() + " ");
        System.out.print((a = new Rabbit()).prey().getClass().getSimpleName() + " ");
        System.out.print((a = new Lion()).prey().getClass().getSimpleName() + " ");
    }
}
abstract class Animal {abstract Animal prey(); }
class Cat extends Animal {
    Cat prey() { // LINE B
        return this;
    }
}
class Rabbit extends Animal {    Animal prey() { return this; } }
abstract class Lion extends Animal { // LINE A
    Animal prey() { return this; }
}


Cat Rabbit Lion
Cat Animal Animal
Compilation Error at LINE A, since a class with no abstract methods can not be declared as abstract
Compilation Error at LINE B, since overridden methods return type can not be changed
Compilation Error because of some other reason

Doubts

Problems

Topic: Static and Non Static Variables - Static and Non Static Methods

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App