🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← All practice quizzes

Quiz: Nested and Anonymous Classes

Read all questions with answers & explanations →

5 questions · new set on every retakeNo time limitPass: 60%+10 XP on first pass

Java lets you write a class inside another class. Learn the four kinds — static nested, inner, local and anonymous — how each one relates to the outer object, and when to replace an anonymous class with a lambda.

Log in to take the quiz

Free account · see your score, answers and explanations, earn XP

  1. Question 1

    Which kind of nested class can be created without an object of the outer class?

  2. Question 2

    How do you create an object of the inner (non-static) class Account inside Bank?

  3. Question 3

    What does this print?

    class Outer {
        int x = 10;
        class Inner {
            int x = 20;
            int sum() { return x + Outer.this.x; }
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            Outer o = new Outer();
            Outer.Inner i = o.new Inner();
            o.x = 5;
            System.out.println(i.sum());
        }
    }
    
  4. Question 4

    What does "effectively final" mean for a local variable used inside a lambda?

  5. Question 5

    What does this print?

    public class Main {
        String name = "outer";
    
        void test() {
            Runnable r1 = new Runnable() {
                String name = "anon";
                public void run() { System.out.print(this.name + " "); }
            };
            Runnable r2 = () -> System.out.println(this.name);
            r1.run();
            r2.run();
        }
    
        public static void main(String[] args) {
            new Main().test();
        }
    }
    
🎯 Today’s placement challenge — 10 questions, no login. Can you score 10/10?6 students have taken today’s challengeTake today’s challenge →