C++ Programming Multiple Choice Questions & Answers (MCQs) on “Lambda Expressions”.
1. What is lambda expression in C++?
a) A technique of C++ that allows us to write inline functions without a name
b) A technique of C++ that allows us to write overloaded functions
c) A technique of C++ that allows us to write functions that are called more than once
d) A technique of C++ that allows us to write functions without parameters
Answer: a
Clarification: Lambda expression is a technique available in C++ that helps the programmer to write inline functions that will be used once in a program and so there is no need of providing names top them. Hence they are a type of inline functions without names.
2. What is the syntax of defining lambda expression?
a) [capture clause](parameters) -> return_type { body of the function }
b) [parameters](capture clause) -> return_type { body of the function }
c) [parameters:capture clause]() -> return_type { body of the function }
d) [capture clause:parameters]() -> return_type { body of the function }
Answer: a
Clarification: The correct syntax of defining a lambda expression is given below:
[capture clause](parameters) -> return_type { the body of the function }
3. What is the correct statement about lambda expression?
a) The return type of lambda expression can be neglected in some cases
b) The return type of lambda expression must be specified in all cases
c) Lambda expression should be very large functions
d) Lambda expression is also available in C
Answer: a
Clarification: Return type in lambda expression can be ignored in some cases as the compiler will itself figure that out but not in all cases. Lambda expression is used to define small functions, not large functions. Lambda expression is introduced in C++.
4. In how many ways we can capture the external variables in the lambda expression?
a) 1
b) 2
c) 3
d) 4
Answer: c
Clarification: There are three ways in which we can capture the external variables inside the lambda expression namely capture by reference, capture by value and capture by both that is mixed capture.
5. Which of the following operator is used to capture all the external variable by reference?
a) &
b) =
c) *
d) &&
Answer: a
Clarification: The lambda expression uses & operator to capture the external variable by reference.
6. Which of the following operator is used to capture all the external variable by value?
a) &
b) =
c) *
d) &&
Answer: b
Clarification: The lambda expression uses = operator to capture the external variable by value.
7. Which is the correct syntax of capturing a variable ‘X’ by reference and other variable ‘Y’ by value in lambda expression?
a) [&X, Y]
b) [X, &y]
c) [X, Y]
d) [&x, &Y]
Answer: a
Clarification: In order to capture a variable by reference we use & operator whereas when we capture a single variable by value then we just write the name of that variable without any operator preceding it, So the correct way of capturing the variables X and Y, in this case, is [&X, Y].
8. What will be the output of the following C++ code?
#includeusing namespace std; int main() { int x = 5; auto check = []() -> bool { if(x == 0) return false; else return true; }; cout<<check()<<endl; return 0; }
a) 1
b) 0
c) Error
d) Segmentation fault
Answer: c
Clarification: The above code gives an error because x is neither passed as a parameter in lambda expression nor it is declared as a local variable inside the expression. So the only x that will be referred is the outside x but as the lambda expression does not capture any variable, therefore, it is also not allowed to access the external variable x hence as variable x is not defined therefore the program gives the error.
9. What will be the output of the following C++ code?
#includeusing namespace std; int main() { int a = 5; auto check = [](int x) { if(x == 0) return false; else return true; }; cout<<check(a)<<endl; return 0; }
a) 0
b) 1
c) Error
d) Segmentation fault
Answer: b
Clarification: The program is correct. In this program you can observe that we have specified the return type of the expression though also the program runs fine because compiler is able to find out the return type of the expression.
10. What will be the output of the following C++ code?
#includeusing namespace std; int main() { int a = 5; auto check = [=]() { a = 10; }; check(); cout<<"Value of a: "<<a<<endl; return 0; }
a) Value of a: 5
b) Value of a: 10
c) Error
d) Segmentation fault
Answer: c
Clarification: As this lambda expression is capturing the extrenal variable by value therefore the value of a cannot be changes inside the lambda expression hence the program gives error.
11. What will be the output of the following C++ code?
#includeusing namespace std; int main() { int a = 5; auto check = [&]() { a = 10; }; check(); cout<<"Value of a: "<<a<<endl; return 0; }
a) Value of a: 5
b) Value of a: 10
c) Error
d) Segmentation fault
Answer: b
Clarification: As this lambda expression is capturing the extrenal variable by reference therefore the change in value of a will be reflected back outside the expression therefore the value of a will now be 10 and 10 is printed.
12. What will be the output of the following C++ code?
#includeusing namespace std; int main() { int a = 5; int b = 5; auto check = [&a]() { a = 10; b = 10; } check(); cout<<"Value of a: "<<a<<endl; cout<<"Value of b: "<<b<<endl; return 0; }
a) Value of a: 5
b) Value of a: 10
c) Error
d) Segmentation fault
Answer: c
Clarification: As this lambda expression is not capturing variable b but trying to access the external variable b hence the program gives an error.