ProgrammingModerateProduct-company placement · GATE CS
What is printed?
What is printed?
int a = 1, b = 2; int& r = a; r = b; r = 5; cout << a << b;
- A. 15
- B. 12
- C. 55
- D. 52
Correct answer: D. 52
A reference is bound once, at initialisation; r = b copies b's value (2) into a, it does not rebind r. r = 5 then sets a to 5, and b stays 2.
More OOP with C++ and Java questions
- What is printed? cpp struct E { virtual const char n() const { return "E"; } virtual ~E() { } }; struct F : E { const char n() const…
- What is printed? (E and F are as shown.) cpp struct E { virtual ~E() { } }; struct F : E { }; int main() { try { try { throw F(); } catch…
- What happens? java class T { static String s() { return "S"; } String i() { return "I"; } } // in main: T t = null…
- What is printed? java class O { int x = 1; class I { int x = 2; void p(int x) { System.out.print(x + "" + this.x + O.this.x); } } } // in…
- 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()…