C++ Programming Questions and Answers – Lambda Expressions

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses 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
View Answer

Answer: a
Explanation: 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 }
View Answer

Answer: a
Explanation: 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
View Answer

Answer: a
Explanation: 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++.
advertisement
advertisement

4. In how many ways we can capture the external variables in the lambda expression?
a) 1
b) 2
c) 3
d) 4
View Answer

Answer: c
Explanation: 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.
Note: Join free Sanfoundry classes at Telegram or Youtube

5. Which of the following operator is used to capture all the external variable by reference?
a) &
b) =
c) *
d) &&
View Answer

Answer: a
Explanation: 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) &&
View Answer

Answer: b
Explanation: The lambda expression uses = operator to capture the external variable by value.
advertisement

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]
View Answer

Answer: a
Explanation: 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?

advertisement
#include<iostream>
using 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
View Answer

Answer: c
Explanation: 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?

#include<iostream>
using 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
View Answer

Answer: b
Explanation: 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?

#include<iostream>
using 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
View Answer

Answer: c
Explanation: 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?

#include<iostream>
using 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
View Answer

Answer: b
Explanation: 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?

#include<iostream>
using 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
View Answer

Answer: c
Explanation: As this lambda expression is not capturing variable b but trying to access the external variable b hence the program gives an error.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.