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);- A. 321
- B. 333
- C. 6
- 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
- What is printed? java class P { int v; P(int v) { this.v = v; } public boolean equals(P o) { return o.v == v; } } // in main: List<P> list…
- Which lines fail to compile? cpp struct M { int v; M(int x) : v(x) { } M operator+(const M& o) const { return M(v + o.v); } }; int main()…
- Why does the C++ standard reject a copy constructor declared as T(T other) for a class T?
- A drawing program uses the Visitor pattern: Shape has accept(Visitor v), and Circle implements it as v.visit(this). Visitor has overloads…
- How many of these statements about Java (9 or later) are correct? 1. An interface can declare instance fields. 2. An abstract class can…
- A UI component registers itself as an observer of a long-lived global EventBus when it is created but never unregisters. Screens using…