ProgrammingModerateProduct-company placement · Java

What is printed?

What is printed?
class Outer {
    int x = 1;
    class Inner {
        int x = 2;
        void show(int x) {
            System.out.print(x + " " + this.x + " " + Outer.this.x);
        }
    }
}
// in main:
new Outer().new Inner().show(3);
  1. A. 3 2 1
  2. B. 3 3 3
  3. C. 1 2 3
  4. D. 3 2 2

Correct answer: A. 3 2 1

The parameter shadows both fields; this.x is the Inner field and Outer.this.x reaches the enclosing instance's field.

More OOP with C++ and Java questions

Exams that ask this topic