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

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.

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

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.

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

Answer: a
Explanation: The program is correct hence the program is successfully executed. However nothing is printed because we have written any print statement.
advertisement

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

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.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn