Test Series - cpp

Test Number 101/102

Q: 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
Solution: 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.
Q: 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 }
Solution: The correct syntax of defining a lambda expression is given below:
[capture clause](parameters) -> return_type 
{ 
	the body of the function 
}
Q: 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
Solution: 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++.
Q: In how many ways we can capture the external variables in the lambda expression?
A. 1
B. 2
C. 3
D. 4
Solution: 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.
Q: Which of the following operator is used to capture all the external variable by reference?
A. &
B. =
C. *
D. &&
Solution: The lambda expression uses & operator to capture the external variable by reference.
Q: Which of the following operator is used to capture all the external variable by value?
A. &
B. =
C. *
D. &&
Solution: The lambda expression uses = operator to capture the external variable by value.
Q: 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]
Solution: 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].
Q: What are command line arguments?
A. Arguments passed to main() function
B. Arguments passed to any function
C. Arguments passed to class functions
D. Arguments passed to structure functions
Solution: Command line arguments are the arguments that passed to the main function when the program is starting its execution.
Q: To use command line arguments in C++, how many parameters are passed to the main function?
A. 1
B. 2
C. 3
D. 4
Solution: 2 arguments are needed to be passed to main() function while using command line arguments. The first one represents a number of strings in the argument list and the second list represents the list of string arguments.
Q: Which character is used to separate different arguments?
A. #
B. $
C. space
D. |
Solution: Command line arguments are separated by space. So if you write
./output This is a single parameter
then they will interpreted as 5 command line arguments as shown : [“./output”, “This”, “is”, “single”, “parameter”].

You Have Score    /10