ProgrammingVery hardProduct-company placement · C++
What is printed?
What is printed?
int main() {
int x = 0;
auto f = [x]() mutable { return ++x; };
f(); f();
cout << f() << x;
}- A. 33
- B. 30
- C. 10
- D. Compile-time error: x is read-only in the lambda
Correct answer: B. 30
A by-value capture is a member of the closure object; mutable lets calls modify that copy, which persists between calls (1, 2, 3). The outer x is never changed.
More OOP with C++ and Java questions
- What is printed? cpp int main() { std::function<int()> f; try { cout << f(); } catch (const badfunctioncall&) { cout << "E"; } }
- What is printed? cpp struct W { W(int, int) { cout << "2"; } W(initializerlist<int>) { cout << "L"; } }; int main() { W a(1, 2); W b{1…
- Two classes hold sharedptr members pointing to each other (a parent owns its child and the child owns its parent). What happens when the…
- OrderService creates new MySqlRepository() inside its own constructor. It is refactored to receive a Repository interface through its…
- An interface Machine declares print(), scan() and fax(). A SimplePrinter class implements it and has to make scan() and fax() throw…
- A GUI toolkit must create a matching Button, Checkbox and Window for either a Windows or a Mac theme, guaranteeing that all widgets come…