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();- A. Nested.show
- B. Compilation error: Nested requires an Outer instance to construct
- C. Compilation error: Outer.Nested is not accessible
- 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
- What is printed? java interface Shape { double area(); static Shape unitSquare() { return () -> 1.0; } } // in main: Shape s =…
- What is printed? java class Shape { protected String name = "Shape"; Shape() { System.out.print(name + " "); } } class Circle extends…
- What is printed? cpp class Account { double balance; public: explicit Account(double b) : balance(b) {} double get() const { return…
- What is printed? java class Base { protected Object clone2() { return new Base(); } } class Sub extends Base { @Override public Object…
- What is printed? java class Shape { double area() { return 0.0; } } class Rectangle extends Shape { double w, h; Rectangle(double w…
- What is printed? java class Vehicle implements Comparable<Vehicle> { int speed; Vehicle(int s) { speed = s; } public int compareTo(Vehicle…