ProgrammingVery hardMicrosoft/Google SDE MCQ · GATE CS
What is printed?
What is printed?
void g(int&) { cout << "L"; }
void g(int&&) { cout << "R"; }
template <class T> void fwd(T&& x) {
g(x);
g(std::forward<T>(x));
}
int main() {
int i = 0;
fwd(i);
fwd(1);
}- A. LLRR
- B. LRLR
- C. LLLL
- D. LLLR
Correct answer: D. LLLR
Inside fwd, the named parameter x is always an lvalue, so g(x) prints L both times. std::forward restores the category: for fwd(i), T = int& (lvalue, L); for fwd(1), T = int (rvalue, R).
More OOP with C++ and Java questions
- 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"; }…
- Which statement about this class is correct? cpp struct Buf { int p; Buf() : p(new int[8]) { } ~Buf() { delete[] p; } }; void use() { Buf…