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
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>
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];
Answer: A
Explanation:
: A pair is declared using the this syntax pair <type, type> 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
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. []
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.
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
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. 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. = ("<
A. Pair(first,seconD. = (1,2)
B. Compile-time error
C. Run-time error
D. Assignment is not correct
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.
8. 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
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.
9. What will be the output of the following C++ code?
#include <iostream>
#include <utility>
using namespace std;
int main ()
{
pair 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
Explanation:
: make_pair() is a function provied to define the values for a pair. Hence the program is correct therefore the program runs successfully.
10. 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);
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.
11. 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
Explanation:
: If a pair is not initialized then by default both parts of the pair is initialized to zero.
12. Which of the following Operator cannot be used with pairs?
A. +
B. ==
C. =
D. !=
Answer: A
Explanation:
: We can use only assignment and logical operators with pairs.
13. What will be the output of the following C++ code?
#include<iostream>
#include <utility>
#include <string>
using namespace std;
int main ()
{
pair p1(1,2);
pair 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
Explanation:
: As both the elements are small in p1 pair, therefore, the pair p1 is considered small hence the output is as follows.