C++ MCQs - Tuple
This section focuses on the "Tuple" in C++ programming langauge. These Multiple Choice Questions (MCQ) should be practiced to improve the C++ programming skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.
1. Which statement is true about tuple?
A. A tuple is an object that can hold a number of elements.
B. The elements can be of different data types.
C. The elements of tuples are initialized as arguments in order in which they will be accessed.
D. All of the above
View Answer
Ans : D
Explanation: A tuple is an object that can hold a number of elements. The elements can be of different data types. The elements of tuples are initialized as arguments in order in which they will be accessed.
2. What will be output for the following code?
#include<iostream>
#include<tuple>
using namespace std;
int main()
{
tuple <char,int,float> geek(20,'g',17.5);
cout << ""The size of tuple is : "";
cout << tuple_size<decltype(geek)>::value << endl;
return 0;
}
A. The size of tuple is : 3
B. The size of tuple is : 4
C. The size of tuple is : 5
D. The size of tuple is : 6
View Answer
Ans : A
Explanation: the output for the foollowing code is : 3
3. Which function concatenates two tuples and returns a new tuple?
A. tie
B. tuple_cat
C. tuple_con
D. concat
View Answer
Ans : B
Explanation: tuple_cat() : This function concatenates two tuples and returns a new tuple.
4. Which returns the number of elements present in the tuple?
A. tie
B. size
C. tuple_size
D. size_tuple
View Answer
Ans : C
Explanation: tuple_size : It returns the number of elements present in the tuple.
5. Which of the following is correct about tuple_size?
A. Returns the number of elements in a tuple
B. Returns the maximum sized type element
C. Returns the total number of bits used by the tuple
D. Returns the sum of non-string values
View Answer
Ans : A
Explanation: tuple_size is used to get the number of elements inside a tuple. For example the tuple_size of tp = {1, 4, “hello”} is 3.
6. How many variants of tie() function is there?
A. 1
B. 2
C. 3
D. 4
View Answer
Ans : B
Explanation: There are two variants of tie() function one with ignore word and other without ignore word. The ignore word is used to ignore the unpacking of a particular element
7. ___________ word is used to ignore the unpacking of some elements of a tuple.
A. stop
B. ignore
C. cancel
D. remain
View Answer
Ans : B
Explanation: Ignore word is used to ignore the unpacking of some elements of a tuple.
8. What will be output for the following code?
#include <iostream>
#include <string>
#include <tuple>
using namespace std;
int main()
{
tuple <int, char, string> tp;
tp = make_tuple(""Hello"", 4, 'c');
return 0;
}
A. Nothing is printed
B. Compile-time error
C. Run-time error
D. Exception occurs
View Answer
Ans : B
Explanation: In this case the order of initialization is different from the order of declaration therefore the program gives compile error.
9. Which of the following is the correct way of declaring a tuple?
A. tuple tp;
B. tuple tp = new tuple;
C. tuple tp;
D. Tuple tp;
View Answer
Ans : C
Explanation: The correct syntax of declaring tuple is tuple tp; Lowercase tuple is used to declare to tuples therefore Tuple tp; is wrong.
10. make_tuple function is used for?
A. used to access the tuple values and modify them
B. used to assign tuple with values
C. used to returns the number of elements present in the tuple
D. None of the above
View Answer
Ans : B
Explanation: make_tuple() :make_tuple() is used to assign tuple with values. The values passed should be in order with the values declared in tuple.
Discussion