C++ Programming Questions and Answers – Tuples – 1

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

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

Answer: d
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

Answer: d
Explanation: <tuple> header file is required to use tuples in your program. This header file contains all the related functions about tuples.
advertisement
advertisement

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

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

Answer: c
Explanation: make_tuple() function is available under the header file which is used to initialize a tuple.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

Answer: b
Explanation: As the order of initialization is different from the order of declaration therefore the program gives compile error.
advertisement

7. What will be the output of the following C++ code?

advertisement
#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

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

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

Answer: a
Explanation: get() function is provided with header file to access an element of a tuple.

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
Hello
View Answer
Answer: d
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.

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.