What will be the result of compiling and running the following code:

public final class QTest {
    public static void main(String[] args) {
        new QTest() {
            public void show() {
                System.out.print("2");
            }
        }.show();
    }
    public void show() {
        System.out.print("1");
    }
}
Explanation
Compilation error will occur, because there is an attempt to instantinate an anonimous inner class, which is a subclass of final class QTest. (Classes declared with a final modifier can't have subclasses)

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes