250+ TOP MCQs on STL – Pair and Answers

C++ Programming Multiple Choice Questions & Answers (MCQs) on “STL – Pair”.

1. What is a pair?
a) Container consisting of two data elements of the same type
b) Container consisting of two data elements of different type
c) Container consisting of one header and two data elements of the same type
d) Container consisting of two data elements can have the same or different type
Answer: d
Clarification: Pair is a container defined in STL which consist of two elements which can be of same or different types.

2. Which header file is required to use pair container in your program?
a)
b)
c) d)
Answer: b
Clarification: Pair container is defined under the header file therefore one should include header before using pair container.

3. Which of the following is the correct syntax of using pair p?
a) pair p;
b) pair p ;
c) pair [type,type] p;
d) pair p [type,type];
Answer: a
Clarification: A pair is declared using the this syntax pair identifier.

4. Which of the following operations can be performed on a pair?
a) Assignment of pairs
b) Copying of one pair to another
c) Comparison of two pairs
d) All of the mentioned
Answer: d
Clarification: A pair can be assigned, copied or can be compared. Hence all the above operations can e performed on pairs.

5. Which operator is used to access the first or second element of a pair?
a) ->
b) .
c) *
d) []
Answer: b
Clarification: .(dot) operator is used to access the first or second element of a pair. For example, if p = (1,2) is a pair then 2 can be accessed by using p.first and 2 can be accessed using p.second.

6. Which of the following is the correct syntax of accessing the first element of a pair p?
a) p.first
b) p.second
c) p[0]
d) p[1]
Answer: a
Clarification: To access the first element of a pair we use first. for example, if p = (1,2) is a pair then we will use p.first to access the first element of the pair.

7. Which of the following is the correct syntax of accessing the second element of a pair p?
a) p.first
b) p.second
c) p[0]
d) p[1]
Answer: b
Clarification: To access the second element of a pair we use second. for example, if p = (1,2) is a pair then we will use p.second to access the second element of the pair.

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

#include   
#include 
 
using namespace std;
 
int main () 
{
  pair <int,int> p(1,2);
  cout<<"Pair(first,second) = ("<<p.first<<","<<p.second<<")n";
  return 0;
}

a) Pair(first,second) = (1,2)
b) Compile-time error
c) Run-time error
d) Assignment is not correct
Answer: a
Clarification: This is a way of assigning a pair therefore the program is correct hence the program runs perfectly and outputs the value as follows.
Output:

$ ./a.out 
Pair(first,second) = (1,2)

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

#include   
#include 
 
using namespace std;
 
int main () 
{
  pair p(1,2);
  cout<<"Pair(first,second) = ("<<p.first<<","<<p.second<<")n";
  return 0;
}

a) Pair(first,second) = (1,2)
b) Compile-time error
c) Run-time error
d) Assignment is not correct
Answer: b
Clarification: A pair always expects tempalte arguments i.e. types of first and second during declaration of pair. In this program as we have not mentioned the template arguments i.e. types of first and second therefore the program gives and error.

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

#include   
#include 
 
using namespace std;
 
int main () 
{
  pair <int,int>p;
  p = make_pair(1,2);
  cout<<"Pair(first,second) = ("<<p.first<<","<<p.second<<")n";
  return 0;
}

a) Pair(first,second) = (1,2)
b) Compile-time error
c) Run-time error
d) Assignment is not correct
Answer: a
Clarification: make_pair() is a function provied to define the values for a pair. Hence the program is correct therefore the program runs successfully.
Output:

$ ./a.out 
Pair(first,second) = (1,2)

11. Which of the following is correct way of copying the values of pair p1 into other pair p2?
a) pair p2 = p1;
b) pair p2(p1);
c) Both pair p2 = p1; and pair p2(p1);
d) Pair p2.copy(p1);
Answer: c
Clarification: Both pair p2 = p1; and pair p2(p1); can be used to copy the data of one pair into other pair.

12. What happens if a pair is not initialized?
a) Both first and second part is initialized to zero or null
b) Both first and second part is initialized a garbage value
c) First is initialized to zero or null and second is initialized a garbage value
d) Second is initialized to zero or null and first is initialized a garbage value
Answer: a
Clarification: If a pair is not initialized then by default both parts of the pair is initialized to zero.

13. Which of the following Operator cannot be used with pairs?
a) +
b) ==
c) =
d) !=
Answer: a
Clarification: We can use only assignment and logical operators with pairs.

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

#include   
#include 
#include 
 
using namespace std;
 
int main () 
{
  pair <int,int> p1(1,2);
  pair <int,int> p2(3,4);
  cout<<"Pair(first,second) = ("<<p1.first<<","<<p1.second<<")n";
  p1.swap(p2);
  cout<<"Pair(first,second) = ("<<p1.first<<","<<p1.second<<")n";
  return 0;
}

a)

Pair(first,second) = (1,2)
Pair(first,second) = (3,4)

b)

Pair(first,second) = (3,4)
Pair(first,second) = (1,2)

c)

Pair(first,second) = (1,2)
Pair(first,second) = (1,2)

d)

Pair(first,second) = (3,4)
Pair(first,second) = (3,4)

View Answer

Answer: a
Clarification: Inititally the pair p1 = (1,2) therefore Pair(first,second) = (1,2) is printed and when we have used swap function to swap p1 with p2 the p1 and p2 is swapped therefore next time Pair(first,second) = (3,4) is printed.
Output:

$ ./a.out 
Pair(first,second) = (1,2)
Pair(first,second) = (3,4)

 
 

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

#include   
#include 
#include 
 
using namespace std;
 
int main () 
{
  pair <int,int> p1(1,2);
  pair <int,int> p2(3,4);
  if(p1 <= p2)
  	cout<<"P1 is small";
  else
  	cout<<"P2 is small";
  return 0;
}

a) P1 is small
b) P2 is small
c) Error
d) Segmentation fault
Answer: a
Clarification: As both the elements are small in p1 pair, therefore, the pair p1 is considered small hence the output is as follows.
Output:

$ ./a.out 
P1 is small

Leave a Reply

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