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;
}
  1. A. 33
  2. B. 30
  3. C. 10
  4. 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

Exams that ask this topic