LearnFrenzy
Sign in Take a free test

Lambda Expressions · Exercise 3 of 4

Lambda Expressions Find the Error

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Each C++ program contains a lambda mistake that the compiler rejects. Choose the option that names the real problem.

Question 1 Easy

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v{3, 8, 12, 5};
    int limit = 6;
    auto n = std::count_if(v.begin(), v.end(), [](int x) { return x > limit; });
    std::cout << n << '\n';
    return 0;
}
A count_if needs a comparator
B limit is a local variable and the capture list [] is empty; capture it with [limit] (or [&limit])
C x must be a reference
D limit must be const
Question page & discussion
Question 2 Easy

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

int main() {
    int count = 0;
    auto tick = [count] { ++count; return count; };
    std::cout << tick() << '\n';
    return 0;
}
A count must be global
B By-value captures are const inside the lambda; add mutable (to change the copy) or capture by reference [&count] (to change the original)
C Lambdas cannot return values
D tick needs parameters
Question page & discussion
Question 3 Medium

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

int run(int (*op)(int), int x) { return op(x); }

int main() {
    int bonus = 10;
    std::cout << run([bonus](int x) { return x + bonus; }, 5) << '\n';
    return 0;
}
A run must be a template
B Only lambdas without captures convert to function pointers; this one captures bonus – take std::function<int(int)> or a template parameter instead
C bonus must be a reference
D x must be long
Question page & discussion
Question 4 Medium

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>
#include <memory>

int main() {
    auto config = std::make_unique<int>(42);
    auto task = [config] { return *config; };
    std::cout << task() << '\n';
    return 0;
}
A make_unique needs a type
B unique_ptr cannot be copied, so it cannot be captured by value; move it in with an init capture [config = std::move(config)]
C task must be mutable
D *config is invalid
Question page & discussion
Question 5 Medium

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

int main() {
    auto discount = [](int amount) {
        if (amount > 1000) return amount * 0.9;
        return amount;
    };
    std::cout << discount(1500) << '\n';
    return 0;
}
A amount must be double
B One return gives double and the other int, so the return type cannot be deduced; add -> double or make both returns the same type
C Lambdas cannot contain if
D 0.9 must be 0.9f
Question page & discussion
Question 6 Medium

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

int main() {
    auto fact = [](int n) { return n <= 1 ? 1 : n * fact(n - 1); };
    std::cout << fact(5) << '\n';
    return 0;
}
A Lambdas cannot use ?:
B fact's type is deduced from its initialiser, so the name cannot be used inside its own initialiser; use C++23 this auto self, std::function, or pass the lambda to itself
C n must be long
D fact must be global
Question page & discussion
Question 7 Medium

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

int main() {
    bool fast = true;
    auto pick = [](int x) { return x * 2; };
    if (!fast) pick = [](int x) { return x * 3; };
    std::cout << pick(10) << '\n';
    return 0;
}
A fast must be const
B Each lambda has its own unique type, so a different lambda cannot be assigned to pick; use std::function<int(int)> (or a function pointer for captureless lambdas)
C x must be unsigned
D Lambdas cannot be inside if
Question page & discussion
Question 8 Easy

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <functional>
#include <iostream>
#include <string>

int main() {
    std::function<int(int)> f = [](const std::string& s) { return static_cast<int>(s.size()); };
    std::cout << f(5) << '\n';
    return 0;
}
A std::function cannot hold lambdas
B The lambda takes a std::string, but std::function<int(int)> calls it with an int, which does not convert to std::string
C size() returns int
D f must be auto
Question page & discussion
Question 9 Hard

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

class Account {
    int balance_ = 500;
public:
    auto checker() const {
        return [balance_](int amount) { return amount <= balance_; };
    }
};

int main() {
    Account a;
    std::cout << a.checker()(300) << '\n';
    return 0;
}
A checker must not be const
B Data members cannot be captured by name; capture [this], [*this], or an init capture [b = balance_]
C amount must be long
D Lambdas cannot be returned
Question page & discussion
Question 10 Hard

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <concepts>
#include <iostream>
#include <string>

int main() {
    auto half = [](std::integral auto x) { return x / 2; };
    std::cout << half(10) << '\n';
    std::cout << half(std::string("ten")) << '\n';
    return 0;
}
A Lambdas cannot use concepts
B The parameter is constrained with std::integral, and std::string is not integral, so the second call is rejected
C half(10) is ambiguous
D x / 2 is invalid for int
Question page & discussion
Up next · Exercise 4Lambda Programs10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links