LearnFrenzy
Sign in Take a free test

Lambda Expressions · Exercise 1 of 4

Lambda Expressions Lambda MCQ

  • 20 questions
  • Page 1 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 1 Easy

What is a lambda expression?

A A macro
B An anonymous function object that can capture variables from its surrounding scope
C A pointer to a member
D A kind of loop
Question page & discussion
Question 2 Easy

In [limit](int x) -> bool { return x < limit; }, what is [limit]?

A The parameter list
B The capture list
C The return type
D An array
Question page & discussion
Question 3 Easy

When is the variable x copied in auto f = [x] { return x; };?

A Each time f is called
B When the lambda (closure) is created
C At program start
D Never
Question page & discussion
Question 4 Easy

What does the capture [&] do?

A Captures nothing
B Captures every local variable used in the body by reference
C Captures only this
D Captures by value
Question page & discussion
Question 5 Medium

Why must you write mutable to modify a by-value capture?

A To capture by reference
B The closure's operator() is const by default, so its copied members cannot change without mutable
C To make it thread-safe
D To allow recursion
Question page & discussion
Question 6 Medium

Do auto a = []{ return 1; }; and auto b = []{ return 1; }; have the same type?

A Yes
B No – every lambda expression has its own unique closure type
C Only in C++20
D Only if declared const
Question page & discussion
Question 7 Medium

Which lambda can be assigned to int (*fp)(int)?

A [x](int a) { return a + x; }
B [](int a) { return a * 2; }
C [&](int a) { return a; }
D [=](int a) { return a; } using a local
Question page & discussion
Question 8 Easy

What is [](auto a, auto b) { return a + b; }?

A An error
B A generic lambda (C++14): its call operator is a template
C A lambda taking std::any
D A variadic macro
Question page & discussion
Question 9 Medium

What does [p = std::move(ptr)] allow?

A Capturing by reference
B Moving a move-only object such as unique_ptr into the lambda as a new member
C Capturing globals
D Making the lambda recursive
Question page & discussion
Question 10 Hard

What is the difference between capturing [this] and [*this]?

A None
B [this] captures the object's pointer (shared, may dangle); [*this] captures a copy of the object (C++17)
C [*this] is a syntax error
D [this] copies the object
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links