ProgrammingModerateProduct-company placement · GATE CS

What is printed?

What is printed?
class O {
    int x = 1;
    class I {
        int x = 2;
        void p(int x) { System.out.print(x + "" + this.x + O.this.x); }
    }
}
// in main:
new O().new I().p(3);
  1. A. 321
  2. B. 333
  3. C. 6
  4. D. 123

Correct answer: A. 321

The plain name x is the parameter (3), this.x is the inner object's field (2) and O.this.x is the enclosing instance's field (1). The empty string makes the + concatenate rather than add.

More OOP with C++ and Java questions

Exams that ask this topic