public abstract class AbstractTest { public int getNum() { return 45; } public abstract class Bar { public int getNum() { return 38; } } public static void main(String[] args) { AbstractTest t = new AbstractTest() { public int getNum() { return 22; } }; AbstractTest.Bar f = t.new Bar() { public int getNum() { return 57; } }; System.out.println(f.getNum() + " " + t.getNum()); } } What is the result?

  1. An exception occurs at runtime.

  2. Compilation fails.

  3. 57 22

  4. 45 57


Correct Option: D

AI Explanation

To answer this question, we need to understand the concept of overriding methods in Java.

In the given code, we have an abstract class AbstractTest which has a method getNum() that returns the value 45. It also has an inner abstract class Bar which also has a method getNum() that returns the value 38.

In the main method, we create an instance of AbstractTest using an anonymous class and override the getNum() method to return the value 22.

AbstractTest t = new AbstractTest() {
    public int getNum() {
        return 22;
    }
};

Next, we create an instance of Bar using the new keyword and the t object. We again override the getNum() method to return the value 57.

AbstractTest.Bar f = t.new Bar() {
    public int getNum() {
        return 57;
    }
};

Finally, we print the result of f.getNum() which is 57 and t.getNum() which is 22.

System.out.println(f.getNum() + " " + t.getNum());

Therefore, the output of the code is 57 22.

The correct answer is option D) 45 57.

Find more quizzes: