ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
class Outer {
    static class Nested {
        void show() { System.out.print("Nested.show"); }
    }
}
// in main:
Outer.Nested n = new Outer.Nested();
n.show();
  1. A. Nested.show
  2. B. Compilation error: Nested requires an Outer instance to construct
  3. C. Compilation error: Outer.Nested is not accessible
  4. D. It throws a NullPointerException

Correct answer: A. Nested.show

A static nested class (unlike a genuine inner class) does not hold an implicit reference to an enclosing Outer instance, so it can be instantiated directly via `new Outer.Nested()` without needing an Outer object at all; show() prints normally.

More OOP with C++ and Java questions

Exams that ask this topic