ProgrammingModerateProduct-company placement · C++
What is printed?
What is printed?
int main() {
int x = 1;
auto byVal = [x] { return x; };
auto byRef = [&x] { return x; };
x = 5;
cout << byVal() << byRef();
}- A. 55
- B. 11
- C. 51
- D. 15
Correct answer: D. 15
A by-value capture copies x when the lambda is created (1); a by-reference capture reads the current value at call time (5).
More OOP with C++ and Java questions
- Base b points to an object that is really a Base (not a Derived). What does staticcast<Derived>(b) do?
- What is printed? cpp struct S { int a = 1; int b = 2; S() : a(10) { } S(int) { } }; int main() { S x; S y(0); cout << x.a << x.b << y.a; }
- What is printed? cpp struct B { virtual void f() { cout << "B"; } }; struct D : B { void f() override { cout << "D"; } }; int main() { D…
- What is printed? java class Util { static int say(String s) { System.out.print(s); return 1; } } interface I { int X = Util.say("I"); }…
- What is printed? java class P { static int v = 5; static { System.out.print("P"); } } class C extends P { static { System.out.print("C")…
- What is printed? java class C { static { System.out.print("init "); } } // in main: C[] a = new C[2]; Class<?> k = C.class…