C++ Programming Multiple Choice Questions & Answers (MCQs) on “Tuples – 1”.
1. What are the tuples in C++?
a) Objects that can hold more than one element of different types
b) Objects that can hold a single element of complex type
c) Objects that can hold more than one element of the same types
d) Objects that can hold a single element of fundamental type
Answer: a
Clarification: Object that can hold more than one elements having different types. For example, an object holding int, float and char types.
2. Which of the following is correct about tuples?
a) A tuple can hold more than one element
b) A tuple can hold elements having different types
c) Elements of tuples are initialized in order
d) All of the mentioned
Answer: d
Clarification: A tuple can hold more than one element of different types. The order of initialization must be the same as the order of declaration.
3. Which header file is required to use tuples in your program?
a)
b)
c)
d)
Answer: d
Clarification:
4. Which of the following is the correct way of declaring a tuple?
a) tuple tp
b) tuple tp = new tuple
c) tuple
d) Tuple
Answer: c
Clarification: The correct syntax of declaring tuple is tuple
5. Which of the following function is used to initialize a tuple?
a) make()
b) make_pair()
c) make_tuple()
d) make_Tuple()
Answer: c
Clarification: make_tuple() function is available under the header file
6. What will be the output of the following C++ code?
#include#include #include using namespace std; int main() { tuple <int, char, string> tp = {"Hello", 1, 's'}; return 0; }
a) Nothing is printed
b) Compile-time error
c) Run-time error
d) Exception occurs
Answer: b
Clarification: As the order of initialization is different from the order of declaration therefore the program gives compile error.
7. What will be the output of the following C++ code?
#include#include #include using namespace std; int main() { tuple <int, char, string> tp; tp = make_tuple(4, '1', "Hello"); return 0; }
a) Nothing is printed
b) Compile-time error
c) Run-time error
d) Exception occurs
Answer: a
Clarification: The program is correct hence the program is successfully executed. However nothing is printed because we have written any print statement.
8. What will be the output of the following C++ code?
#include#include #include 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
Answer: b
Clarification: In this case the order of initialization is different from the order of declaration therefore the program gives compile error.
9. What is the use of get() function in tuples?
a) To access an element of a tuple
b) To print an element of a tuple
c) To check whether the element of the tuple is empty
d) To delete an element
Answer: a
Clarification: get() function is provided with
10. What will be the output of the following C++ code?
#include#include #include using namespace std; int main() { tuple <int, char, string> tp; tp = make_tuple(4, '1', "Hello"); cout<<get<0>(tp)<<endl; cout<<get<1>(tp)<<endl; cout<<get<2>(tp)<<endl; return 0; }
a)
1 Hello 4
b)
4 Hello 1
c)
Hello 4 1
d)
4 1 Hello
View Answer
Answer: d
Clarification: As the tuple contains int, char and string in 0, 1 and 2 position respectively therefore the get<0>, get<1> and get<2> prints 4, 1 and Hello respectively.