ProgrammingHardProduct-company placement · GATE CS
What is printed?
What is printed?
struct S {
void f() { cout << "N"; }
void f() const { cout << "C"; }
};
int main() {
S a;
const S b{};
const S& r = a;
S* p = &a;
a.f(); b.f(); r.f(); p->f();
}- A. NCNN
- B. NCCN
- C. NNNN
- D. NCCC
Correct answer: B. NCCN
Overloading on const uses the static type of the object expression. r refers to a non-const object, but through a const S& only the const member can be called. p is a pointer to non-const S, so it gets the non-const version.
More OOP with C++ and Java questions
- What is printed? cpp int a = 1, b = 2; int& r = a; r = b; r = 5; cout << a << b;
- 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…