C++ Programming Questions and Answers – STL – Pair

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses 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
View Answer

Answer: d
Explanation: 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) <algorihtm>
b) <utility>
c) <pair>
d) <utitityPair>
View Answer

Answer: b
Explanation: Pair container is defined under the header file <utility> therefore one should include header before using pair container.

3. Which of the following is the correct syntax of using pair p?
a) pair <type,type> p;
b) pair p <type,type>;
c) pair [type,type] p;
d) pair p [type,type];
View Answer

Answer: a
Explanation: A pair is declared using the this syntax pair <type, type> identifier.
advertisement
advertisement

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
View Answer

Answer: d
Explanation: 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) []
View Answer

Answer: b
Explanation: .(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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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]
View Answer

Answer: a
Explanation: 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]
View Answer

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

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

#include <iostream>  
#include <utility>
 
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
View Answer

Answer: a
Explanation: 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)
advertisement

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

#include <iostream>  
#include <utility>
 
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
View Answer

Answer: b
Explanation: 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 <iostream>  
#include <utility>
 
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
View Answer

Answer: a
Explanation: 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 <type,type> p2 = p1;
b) pair <type,type> p2(p1);
c) Both pair <type,type> p2 = p1; and pair <type,type> p2(p1);
d) Pair <int,int> p2.copy(p1);
View Answer

Answer: c
Explanation: Both pair <type,type> p2 = p1; and pair <type,type> 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
View Answer

Answer: a
Explanation: 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) !=
View Answer

Answer: a
Explanation: We can use only assignment and logical operators with pairs.

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

#include <iostream>  
#include <utility>
#include <string>
 
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
Explanation: 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 <iostream>  
#include <utility>
#include <string>
 
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
View Answer

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

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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

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.