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)
Answer: b
Clarification: Pair container is defined under the header file
3. Which of the following is the correct syntax of using pair p?
a) pair
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
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)