LearnFrenzy
Sign in Take a free test

Lambda Expressions · Exercise 1 of 4

Lambda Expressions Lambda MCQ

  • 20 questions
  • Page 2 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Twenty multiple-choice questions on C++ lambda expressions: syntax, captures, mutable, init captures, generic lambdas, closure types, std::function and lifetimes. Pick an option to check your answer instantly.

Question 11 Medium

A function returns [&total] { return total; } where total is a local variable. What happens when the returned lambda is called?

A It returns the last value of total
B Undefined behaviour – total no longer exists
C Compile error
D It returns 0
Question page & discussion
Question 12 Medium

Must a lambda capture a global or static variable to use it?

A Yes, always by value
B No – variables with static storage duration are used directly without capturing
C Yes, with [&]
D Globals cannot be used in lambdas
Question page & discussion
Question 13 Medium

When is std::function<void(int)> the right choice?

A Always, instead of auto
B When you need to store callables of different types behind one signature, e.g. a vector of callbacks
C For the fastest calls
D To make lambdas recursive in C++23
Question page & discussion
Question 14 Easy

If no -> type is written, how is a lambda's return type determined?

A It is always void
B Deduced from the return statements (they must agree)
C It is always int
D From the capture list
Question page & discussion
Question 15 Medium

What does the compiler generate for a lambda expression?

A A global function
B A class with captured variables as members and the body as operator()
C A macro
D A thread
Question page & discussion
Question 16 Hard

How does C++23 let a lambda call itself recursively without std::function?

A With the recursive keyword
B With an explicit object parameter: [](this auto self, int n) { … self(n - 1) … }
C By capturing [&] only
D It is impossible
Question page & discussion
Question 17 Hard

Why is passing a lambda comparator to std::sort typically faster than passing a function pointer?

A Lambdas run on the GPU
B The lambda's unique type lets sort be instantiated for it, so the comparison can be inlined
C Function pointers are not allowed
D Lambdas skip comparisons
Question page & discussion
Question 18 Hard

What did C++20 change about using [=] in a member function?

A [=] is removed
B Implicitly capturing this via [=] is deprecated; write [=, this] or [=, *this]
C [=] now copies the object
D Nothing
Question page & discussion
Question 19 Medium

auto f = [n = 0]() mutable { return ++n; }; – what do three calls to f() return?

A 1, 1, 1
B 1, 2, 3
C 0, 1, 2
D Compile error
Question page & discussion
Question 20 Medium

Since C++17, can a lambda be evaluated at compile time, e.g. in a static_assert?

A No
B Yes – lambdas are implicitly constexpr when their body allows it, and can be marked constexpr
C Only generic lambdas
D Only with std::function
Question page & discussion
Up next · Exercise 2Predict the Output20 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links