LearnFrenzy
Sign in Take a free test

C++ Programming · Topic 21 of 27

Lambda Expressions

  • 4 exercises
  • 60 questions
  • ~60 min
  • Answers explained

Interview Questions

Lambda Expressions · Study material

C++ Lambda Expressions – Interview Questions

1. What is a lambda expression?

An anonymous function object defined inline, optionally capturing variables from the enclosing scope.

2. What is the syntax of a lambda?

[captures](parameters) mutable/constexpr -> return_type { body }.

3. What is the difference between capturing by value and by reference?

By value copies the variable when the lambda is created; by reference refers to the original, seeing later changes and requiring it to stay alive.

4. What do [=] and [&] mean?

Capture every used local variable by value, or by reference, respectively.

5. Why is mutable needed?

The closure's operator() is const by default, so by-value captures cannot be modified without mutable.

6. What is a closure?

The run-time object created from a lambda expression, holding its captured state.

7. What is the type of a lambda?

A unique, unnamed class type generated by the compiler; two identical lambdas still have different types.

8. Can a lambda be converted to a function pointer?

Yes, if it captures nothing.

9. What is a generic lambda?

A lambda with auto parameters (C++14); its operator() is a template.

10. What is an init capture?

[name = expression] (C++14): creates a new member initialised from any expression, e.g. moving a unique_ptr in.

11. What is the difference between [this] and [*this]?

[this] captures the pointer (shares the object, may dangle); [*this] (C++17) captures a copy of the object.

12. How do you write a recursive lambda?

C++23: [](this auto self, int n) { … self(n - 1) … }. Earlier: std::function or pass the lambda as a parameter to itself.

13. When should you use std::function?

When you must store callables of different types behind one signature (callbacks, maps of handlers).

14. What are the costs of std::function?

Type erasure: possible heap allocation for large closures and an indirect call that prevents inlining.

15. Can lambdas be constexpr?

Yes; since C++17 lambdas are implicitly constexpr when possible, and can be marked constexpr explicitly.

16. What happens if a lambda captures a local by reference and is returned?

The reference dangles after the function returns; calling the lambda is undefined behaviour.

17. Can a lambda capture a structured binding?

Yes, since C++20.

18. What is an immediately invoked lambda?

A lambda called right after it is defined, [&] { … }(), often used to initialise const variables.

19. What does a lambda compile into?

A class with captured variables as members and an operator() containing the body.

20. Why are lambdas often faster than function pointers in std::sort?

Each lambda has a unique type, so the algorithm is instantiated for it and the call can be inlined; a function pointer is usually called indirectly.

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About C++ Programming

C++ adds object-oriented programming, templates and the powerful Standard Template Library (STL) to C. It powers game engines, browsers, trading systems and most competitive programming. This section takes you from basic syntax to classes, inheritance, polymorphism, modern C++ (C++11 to C++23) and the STL – every concept with runnable examples and practice questions checked on a real compiler.

Why it matters

  • OOP concepts – classes, inheritance, polymorphism, encapsulation – are asked in almost every interview
  • C++ with the STL is the most popular language for competitive programming and DSA rounds
  • Questions on constructors, virtual functions, references and smart pointers decide technical rounds
  • Modern C++ skills are in demand at product companies, fintech and game studios

Who it is for

  • School students (Class 11–12 computer science with C++) preparing for board exams and practicals
  • College students (B.Tech, BCA, MCA) learning OOP and data structures for semester exams and labs
  • Freshers preparing for placements, coding tests and C++ technical interviews
  • Competitive programmers who want to master the STL

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • MCQs that check your understanding of every concept
  • "Predict the output" puzzles – the trickiest part of written tests
  • "Find the error" programs that train you to spot bugs quickly
  • Complete programs with solutions and real output, verified on a compiler

Asked in

  • Campus placements
  • Product company interviews
  • Coding rounds & DSA
  • Competitive programming
  • GATE CS
  • University exams

How to use this section

  1. Read the study material of a topic and run the examples yourself.
  2. Practise the four exercises – MCQs, output, errors, programs – and read every explanation.
  3. Test yourself with the timed online test, then revise the topics you got wrong.

Search LearnFrenzy

Esc

Quick links