Here is a listing of tough C++ programming questions on “Min and Max” along with answers, explanations and/or solutions:
1. Which keyword is used to declare the min and max functions?
a) iostream
b) string
c) algorithm
d) iterator
View Answer
Explanation: Algorithm header file contains the supporting files needed for the execution of these functions.
2. What kind of functions are min and max in c++?
a) Type specific
b) Variable specific
c) Type & Variable specific
d) Iterator
View Answer
Explanation: The min/max functions are type specific but they will not force everything to be converted to/from floating point. The functions that will force everything to be converted to/from floating point are fmin/fmax.
3. How many parameters are needed for minmax function?
a) 1
b) 2
c) 3
d) All of the mentioned
View Answer
Explanation: The “minmax” function can take the following:
1 parameter: An initializer_list object.
2 parameters: Values to compare.
2 parameters: An initializer_list object. and comparison function
3 parameters: Values to compare. and comparison function
4. What will be the output of the following C++ code?
#include <iostream>
#include <algorithm>
using namespace std;
int main ()
{
cout << "max(1, 2) == " << max(1, 2) << '\n';
cout << "max('a', 'z') == " << max('a', 'z') << '\n';
return 0;
}
a) 2z
b) 2a
c) Error
d) 2y
View Answer
Explanation: In this program, We found the max value in the given value by using max function.
Output:
$ g++ max.cpp $ a.out max(1, 2) == 2 max('a', 'z') == z
5. What will be the output of the following C++ code?
#include <iostream>
#include <algorithm>
using namespace std;
bool myfn(int i, int j)
{
return i < j;
}
int main ()
{
int myints[ ] = {3, 7, 2, 5, 6, 4, 9};
cout << *min_element(myints, myints + 7, myfn) << '\n';
cout << *max_element(myints, myints + 7, myfn) << '\n';
return 0;
}
a) 2 9
b) 2 7
c) 3 9
d) 3 5
View Answer
Explanation: In this program, We found out the minimum value and maximum value
of a range.
Output:
$ g++ max1.cpp $ a.out 2 9
6. What will be the output of the following C++ code?
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main ()
{
int myints[] = {10, 20, 30, 30, 20, 10, 10, 20};
vector<int> v(myints, myints + 8);
sort (v.begin(), v.end());
vector<int> :: iterator low, up;
low = lower_bound (v.begin(), v.end(), 20);
up = upper_bound (v.begin(), v.end(), 20);
cout << (low - v.begin()) << ' ';
cout << (up - v.begin()) << '\n';
return 0;
}
a) 3 6
b) 2 5
c) 2 6
d) 2 4
View Answer
Explanation: In this program, We are finding the upper bound and lower bound values by using lower_bound and upper_bound methods.
Output:
$ g++ max2.cpp $ a.out 3 6
7. What will be the output of the following C++ code?
#include <iostream>
#include <algorithm>
using namespace std;
int main ()
{
cout << min(2, 1) << ' ';
cout << min('m','m') << '\n';
return 0;
}
a) Error
b) Runtime error
c) 1 m
d) 5 m
View Answer
Explanation: In this program, We are finding the minimum value by using min method.
Output:
$ g++ max3.cpp $ a.out 1 m
8. What will be the output of the following C++ code?
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool mygreater (int i,int j)
{
return (i > j);
}
int main ()
{
int myints[] = {10, 20, 30, 30, 20, 10, 10, 20};
vector<int> v(myints, myints + 8);
pair<vector<int> :: iterator, vector<int> :: iterator> bounds;
sort (v.begin(), v.end());
bounds = equal_range (v.begin(), v.end(), 20);
cout << (bounds.first - v.begin());
cout << " and " << (bounds.second - v.begin()) << '\n';
return 0;
}
a) 3 and 6
b) 2 and 5
c) 3 and 5
d) 2 and 4
View Answer
Explanation: In this program, We are finding out the equal range in the vector.
Output:
$ g++ max4.cpp $ a.out 3 and 6
9. Which function is used to return the minimum element in the range?
a) min
b) minimum
c) min_element
d) max_element
View Answer
Explanation: The min_element is used to compare the range of elements and it can find out the minimum element.
10. Which operator is used to compare the values to find min and max?
a) <
b) >
c) <<
d) >>
View Answer
Explanation: Less than(<) operator is sufficient to compare any two elements in heap and construct respective min or max heap accordingly.
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]
- Apply for C++ Internship
- Apply for Computer Science Internship
- Practice Programming MCQs
- Practice Computer Science MCQs
- Check C++ Books