Test Series - cpp

Test Number 70/102

Q: 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
Solution: A pair can be assigned, copied or can be compared. Hence all the above operations can e performed on pairs.
Q: Which operator is used to access the first or second element of a pair?
A. ->
B. .
C. *
D. []
Solution: .(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.
Q: 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]
Solution: 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.
Q: 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]
Solution: 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.
Q: 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);
Solution: Both pair  p2 = p1; and pair  p2(p1); can be used to copy the data of one pair into other pair.
Q: What is any in C++?
A. STL container used to store a single value of any type
B. Exception class in C++
C. Fundamental type provided by C++
D. Template data type
Solution: Any is an STL container provided by C++ to store value or objects of any type.
Q: In how many different ways any-container can be constructed?
A. 1
B. 2
C. 3
D. 4
Solution: There are three basic ways of constructing any variable. They are done using copy initialization, using the constructor or using an assignment operator.
Q: What is the correct syntax of constructing any using copy initialization?
A. any variable_name = value;
B. any variable_name(value);
C. any variable_name; variable_name = value;
D. any variable_name = value;
Solution: To initialize an any variable using copy initialization we use the following syntax:
any variable_name = value;
Q: What is the correct syntax of constructing any using parameterized constructor?
A. any variable_name = value;
B. any variable_name(value);
C. any variable_name; variable_name = value;
D. any variable_name = value;
Solution: To initialize an any variable using parameterized constructor we use the following syntax:
any variable_name(value);
Q: What is the correct syntax of constructing any using assignment operator?
A. any variable_name = value;
B. any variable_name(value);
C. any variable_name; variable_name = value;
D. any variable_name = value;
Solution: To initialize an any variable using assignment operator we use the following syntax:
any variable_name;
variable_name = value;

You Have Score    /10