C++ Programming Questions and Answers – Tuples – 2

«
»

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Tuples – 2”.

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

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

2. 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<<tuple_size<decltype(tp)>::value;
	return 0;
}

a) 11
b) 5
c) 4
d) 3
View Answer

Answer: d
Explanation: As the number of elements in the tuple is 3 therefore the tuple_size of the tuple is 3 hence the output is 3.

3. Which of the following is correct about swap()?
a) Swaps first element of both tuples
b) Swaps two tuples
c) Swaps elements of a tuple alternatively
d) Swaps last elements of two tuples
View Answer

Answer: b
Explanation: swap() function is used to swap two tuples. For example t1 = {1,2} and t2 = {‘a’,’b’} then after swapping both the tuples becomes t1 = {‘a’,’b’} and t2 = {1,2}.

4. 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> tp1;
	tuple <int, char, string> tp2;
	tp1 = make_tuple(6, 'a', "Hello");
	tp2 = make_tuple(9, 'z', "World");
	cout<<"("<<get<0>(tp1)<<", "<<get<1>(tp1)<<", "<<get<2>(tp1)<<")"<<endl;
	cout<<"("<<get<0>(tp2)<<", "<<get<1>(tp2)<<", "<<get<2>(tp2)<<")"<<endl;
	tp1.swap(tp2);
	cout<<"("<<get<0>(tp1)<<", "<<get<1>(tp1)<<", "<<get<2>(tp1)<<")"<<endl;
	cout<<"("<<get<0>(tp2)<<", "<<get<1>(tp2)<<", "<<get<2>(tp2)<<")"<<endl;
	return 0;
}

a)

(6, a, Hello)
(9, z, World)
(9, z, World)
(6, a, Hello)
advertisement

b)

(6, a, Hello)
(9, z, World)
(6, a, Hello)
(9, z, World)

c)

(9, z, World)
(6, a, Hello)
(9, z, World)
(6, a, Hello)

d)

(6, a, Hello)
(6, a, Hello)
(9, z, World)
(9, z, World)
View Answer
Answer: a
Explanation: In this program initially the tuples were as given which is printed and after swapping the elements of tuples are swapped therefore the tuples are swapped.
 
 

5. What is the use of tie() function?
a) Used to replace elements
b) Used to delete elements
c) Used to unpack the values of a tuple
d) Used to check whether two tuples are the same or not
View Answer

Answer: c
Explanation: tie() function of header file is used to unpack the elements of a tuple into different variables.

6. How many variants of tie() function is there?
a) 1
b) 2
c) 3
d) 4
View Answer

Answer: 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. Which word is used to stop the unpacking of a value in a tuple?
a) stop
b) ignore
c) cancel
d) remain
View Answer

Answer: b
Explanation: Ignore word is used to ignore the unpacking of some elements of a tuple.
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> tp1;
	tp1 = make_tuple(6, 'a', "Hello");
	int x;
	char y;
	string z;
	tie(x,y,z) = tp1;
	cout<<"x: "<<x<<"\ny: "<<y<<"\nz: "<<z<<endl;
	return 0;
}

a)

Segmentation fault
z: Hello

b)

x: 6
z: Hello
y: a

c) Error
d)

x: 6
y: a
View Answer
Answer: d
Explanation: We have used the tie() function to unpack the values of the tuple. The values are then stored into x, y and z respectively.
 
 

9. 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> tp1;
	tp1 = make_tuple(6, 'a', "Hello");
	int x;
	string y;
	tie(x,ignore,y) = tp1;
	cout<<"x: "<<x<<"\ny: "<<y<<endl;
	return 0;
}

a)

x: 6
y: Hello
advertisement

b)

y: Hello
x: 6

c) Error
d) Segmentation fault
View Answer

Answer: a
Explanation: In this we are ignoring the unpacking of char and therefore the char is not unpacked and we are prinitng x and y.

10. What is the use of tuple_cat() function?
a) Takes the union of two tuples
b) Takes the intersection of two tuples
c) Concatenates two tuples
d) Removes elements of the second tuple from first
View Answer

Answer: c
Explanation: tuple_cat() function of is used to concatenate two tuples. The new tuple will contain all the elements of both the tuples.

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

#include <iostream>
#include <string>
#include <tuple>
using namespace std;
int main()
{
	tuple <int, string> tp1;
	tuple <int, string> tp2;
	tp1 = make_tuple(0, "Hello");
	tp2 = make_tuple(1, "World");
	auto tp3 = tuple_cat(tp1, tp2);
	cout<<"("<<get<0>(tp3)<<", "<<get<1>(tp3)<<", "<<get<2>(tp3)<<", 
        "<<get<3>(tp3)<<")"<<endl;
	return 0;
}

a) (0, Hello, 1, World)
b) ()
c) (0, 1)
d) (Hello, World)
View Answer

Answer: a
Explanation: The tuple_cat() function concatenates the two tuples therefore the two tuples of the program are concatenated and the output contains all the elements of both the tuples.

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