ProgrammingModerateMicrosoft/Google SDE MCQ · GATE CS
What is printed?
What is printed?
int main() {
int a = 1, b = 2;
int& r = a;
r = b;
r = 5;
cout << a << b;
}- A. 15
- B. 25
- C. 52
- D. 12
Correct answer: C. 52
A reference cannot be reseated: r = b assigns 2 to a, and r = 5 then assigns 5 to a. b is never changed.
More OOP with C++ and Java questions
- What is printed? cpp void g(int&) { cout << "L"; } void g(int&&) { cout << "R"; } template <class T> void fwd(T&& x) { g(x)…
- Which line fails to compile? cpp struct B { void hi() { } }; class D1 : B { }; // note: class struct D2 : B { }; // note: struct int…
- Which line fails 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()…
- What is printed? cpp int main() { vector<int> a(3, 5); vector<int> b{3, 5}; vector<int> c; c.reserve(10); cout << a.size() << b.size() <<…
- Which line fails to compile? cpp struct S { int v = 1; static int count; static int get() { return v; } // line 1 static int all() {…
- What is printed? cpp struct B { virtual void f() { cout << "B"; } void g() { f(); } }; struct D : B { void f() override { cout << "D"; }…