This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses 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
View Answer
Explanation: 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
View Answer
Explanation: 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) <stl>
b) <array>
c) <slgorithm>
d) <tuple>
View Answer
Explanation: <tuple> header file is required to use tuples in your program. This header file contains all the related functions about tuples.
4. Which of the following is the correct way of declaring a tuple?
a) tuple tp<type1, type2, type3>;
b) tuple tp = new tuple<type1, type2, type3>;
c) tuple <type1, type2, type3> tp;
d) Tuple <type1, type2, type3> tp;
View Answer
Explanation: The correct syntax of declaring tuple is tuple <type1, type2, type3> tp; Lowercase tuple is used to declare to tuples therefore Tuple <type1, type2, type3> tp; is wrong.
5. Which of the following function is used to initialize a tuple?
a) make()
b) make_pair()
c) make_tuple()
d) make_Tuple()
View Answer
Explanation: make_tuple() function is available under the header file
6. What will be the output of the following C++ code?
#include <iostream> #include <string> #include <tuple> 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
View Answer
Explanation: 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 <iostream> #include <string> #include <tuple> 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
View Answer
Explanation: 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 <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
Explanation: 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
View Answer
Explanation: get() function is provided with
10. What will be the output of the following C++ code?
#include <iostream> #include <string> #include <tuple> 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 HelloView Answer
Explanation: 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.
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.
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Practice Programming MCQs
- Check Programming Books
- Check C++ Books