250+ TOP MCQs on Tuples – 2 and Answers

C++ Programming Multiple Choice Questions & Answers (MCQs) 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
Answer: a
Clarification: 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.

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

a) 11
b) 5
c) 4
d) 3
Answer: d
Clarification: 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
Answer: b
Clarification: 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 
#include 
#include 
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)

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
Clarification: 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
Answer: c
Clarification: 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
Answer: b
Clarification: 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
Answer: b
Clarification: Ignore word is used to ignore the unpacking of some elements of a tuple.

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

#include 
#include 
#include 
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
Clarification: 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 
#include 
#include 
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

b)

y: Hello
x: 6

c) Error
d) Segmentation fault
Answer: a
Clarification: 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
Answer: c
Clarification: 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 
#include 
#include 
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)
Answer: a
Clarification: 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.

Leave a Reply

Your email address will not be published. Required fields are marked *