C++ Lambda Expressions Quiz
40 in-depth questions covering C++ lambda expressions, capture lists, mutable lambdas, returning lambdas, storing lambdas in std::function, and lambda recursion techniques — with 16 code examples to solidify understanding.
Question 1
What is a lambda expression in C++?
Question 2
What is the difference between capture by value and capture by reference?
int x = 42;
auto value_capture = [x]() { return x; }; // Copy of x
x = 100; // Doesn't affect lambda
int y = 42;
auto ref_capture = [&y]() { return y; }; // Reference to y
y = 100; // Affects lambda resultQuestion 3
What is a mutable lambda?
int x = 0;
auto lambda = [x]() mutable { return ++x; }; // Can modify copy
// Without mutable: error - cannot modify const copy
// auto bad_lambda = [x]() { return ++x; };Question 4
What is std::function and how does it relate to lambdas?
#include <functional>
std::function<int(int)> func = [](int x) { return x * 2; };
// Can store any callable with matching signature
func(5); // Returns 10Question 5
What is capture by default?
int x = 1, y = 2, z = 3;
auto capture_all_value = [=]() { return x + y + z; }; // All by value
auto capture_all_ref = [&]() { return x + y + z; }; // All by reference
auto mixed = [x, &y, z]() { return x + y + z; }; // Mixed captureQuestion 6
What is a generic lambda?
auto generic_lambda = [](auto x, auto y) { return x + y; };
// Works with different types
generic_lambda(1, 2); // int + int
generic_lambda(1.5, 2.5); // double + double
generic_lambda("a", "b"); // const char* + const char*Question 7
What is the difference between lambda and function object (functor)?
auto lambda = [](int x) { return x * 2; };
class Functor {
public:
int operator()(int x) const { return x * 2; }
};
Functor functor;
// Both callable: lambda(5), functor(5)Question 8
What is init capture (C++14)?
int x = 42;
auto lambda = [y = x * 2]() { return y; }; // Initialize capture
// Equivalent to: int y = x * 2; then [y]
// Can also move objects:
std::unique_ptr<int> ptr = std::make_unique<int>(42);
auto move_lambda = [p = std::move(ptr)]() { return *p; };Question 9
What is lambda recursion and how is it implemented?
std::function<int(int)> fibonacci = [&](int n) -> int {
if (n <= 1) return n;
return fibonacci(n-1) + fibonacci(n-2); // Recursive call
};
// Alternative with Y combinator (advanced)
auto Y = [](auto f) {
return [f](auto x) { return f(f, x); }; // Not shown fully
};Question 10
What is the difference between lambda and regular function return types?
auto explicit_return = [](int x) -> double { return x * 2.5; };
auto auto_return = [](int x) { return x * 2.5; }; // Returns double
auto reference_return = [](int& x) -> int& { return x; }; // ReferenceQuestion 11
What is a constexpr lambda (C++17)?
constexpr auto square = [](int x) constexpr { return x * x; };
constexpr int result = square(5); // Compile-time evaluation
static_assert(square(5) == 25); // Compile-time assertionQuestion 12
What is the difference between lambda capture and parameter passing?
int factor = 2;
auto capture_lambda = [factor](int x) { return x * factor; }; // Capture
auto param_lambda = [](int x, int factor) { return x * factor; }; // Parameter
// Usage:
capture_lambda(5); // Uses captured factor
param_lambda(5, 3); // Uses passed factorQuestion 13
What is lambda composition?
auto add = [](int x, int y) { return x + y; };
auto multiply = [](int x, int y) { return x * y; };
auto add_then_multiply = [add, multiply](int x, int y, int z) {
return multiply(add(x, y), z);
};
// Or using std::function composition
std::function<int(int)> twice = [](int x) { return x * 2; };
std::function<int(int)> plus_three = [](int x) { return x + 3; };Question 14
What is the difference between lambda and bind expression?
using namespace std::placeholders;
auto lambda_add = [](int x, int y) { return x + y; };
auto bind_add = std::bind([](int x, int y) { return x + y; }, _1, _2);
// Or: auto bind_add = std::bind(std::plus<int>(), _1, _2);
// Both callable: lambda_add(1, 2), bind_add(1, 2)Question 15
What is a lambda with variadic parameters?
template<typename... Args>
auto make_logger = [](Args&&... args) {
// Log all arguments
((std::cout << args << " "), ...);
std::cout << std::endl;
};
make_logger("Hello", 42, 3.14); // Variadic lambdaQuestion 16
What is the difference between lambda closure and function closure?
Question 17
What is lambda lifetime and capture lifetime?
auto make_lambda() {
int local = 42;
return [&local]() { return local; }; // Dangling reference!
}
auto lambda = make_lambda();
// lambda() is undefined behavior - local destroyedQuestion 18
What is the difference between lambda and inline function?
Question 19
What is lambda template (C++20)?
auto lambda_template = []<typename T>(T value) {
return value;
};
lambda_template(42); // T = int
lambda_template("hello"); // T = const char*
// Equivalent to template lambda with autoQuestion 20
What is the difference between lambda copy and lambda move?
auto lambda = [x = std::make_unique<int>(42)]() { return *x; };
auto copy1 = lambda; // Copy: both point to same int
copy1(); // OK
std::unique_ptr<int> p = std::make_unique<int>(42);
auto move_lambda = [x = std::move(p)]() { return *x; }; // MoveQuestion 21
What is the difference between lambda and coroutine?
Question 22
What is lambda hoisting?
auto lambda = []() {
// Lambda defined at global scope
return 42;
};
void func() {
// Lambda accessible here
lambda();
}Question 23
What is the difference between lambda and function try block?
auto safe_lambda = []() {
try {
risky_operation();
} catch (...) {
handle_error();
}
};
// Function try block:
void func() try {
risky_operation();
} catch (...) {
handle_error();
}Question 24
What is lambda memoization?
auto fibonacci = [](int n) {
static std::map<int, int> cache;
if (cache.count(n)) return cache[n];
if (n <= 1) return cache[n] = n;
return cache[n] = fibonacci(n-1) + fibonacci(n-2);
};Question 25
What is the difference between lambda and functor performance?
Question 26
What is lambda currying?
auto add = [](int x) {
return [x](int y) { return x + y; };
};
auto add_five = add(5); // Returns lambda
int result = add_five(3); // 8
// Or more generally:
auto curry = [](auto f, auto x) {
return [f, x](auto y) { return f(x, y); };
};Question 27
What is the difference between lambda and method reference?
Question 28
What is lambda lifting?
auto lambda = [x = 42]() { return x; };
// Lambda captures x by value
// After lifting (conceptual):
struct Lambda {
int x;
int operator()() const { return x; }
};
// Compiler generates equivalent classQuestion 29
What is the difference between lambda and block scope?
{
int x = 42;
auto lambda = [x]() { return x; }; // Captures x
lambda(); // Can use x
} // x destroyed, lambda still holds copy
// vs block scope variable:
{
int y = 42;
} // y destroyed immediatelyQuestion 30
What is lambda inlining?
Question 31
What is the difference between lambda and delegate?
Question 32
What is lambda partial application?
auto multiply = [](int x, int y) { return x * y; };
auto double_func = [multiply](int x) { return multiply(2, x); };
// Partially applies first argument
double_func(5); // Returns 10Question 33
What is the difference between lambda and function object size?
Question 34
What is lambda coroutine (C++20)?
auto generator = []() -> std::generator<int> {
for (int i = 0; i < 10; ++i) {
co_yield i; // Suspend and return value
}
};
for (int value : generator()) {
std::cout << value << " ";
}Question 35
What is the difference between lambda and macro?
Question 36
What is lambda statefulness?
auto counter = [count = 0]() mutable {
return ++count; // Modifies captured state
};
counter(); // 1
counter(); // 2
counter(); // 3Question 37
What is the difference between lambda and bind performance?
Question 38
What is lambda template argument deduction (C++20)?
auto lambda = []<typename T>(T value) {
return value;
};
lambda(42); // T = int
lambda("hello"); // T = const char*
// Equivalent to template lambda with autoQuestion 39
What is the difference between lambda and function pointer conversion?
auto lambda = [](int x) { return x * 2; };
using FuncPtr = int(*)(int);
FuncPtr ptr = lambda; // Error: lambda not convertible
// But stateless lambda can be:
auto stateless = +[](int x) { return x * 2; }; // Unary + convertsQuestion 40
What are the fundamental principles for effective lambda usage in C++?
