C++ Programming Questions and Answers – Min and Max

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Min and Max”.

1. Which keyword is used to declare the min and max functions?
a) iostream
b) string
c) algorithm
d) iterator
View Answer

Answer: c
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

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

Answer: d
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
advertisement

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

  1.     #include <iostream>   
  2.     #include <algorithm>  
  3.     using namespace std;
  4.     int main () 
  5.     {
  6.         cout << "max(1, 2) == " << max(1, 2) << '\n';
  7.         cout << "max('a', 'z') == " << max('a', 'z') << '\n';
  8.         return 0;
  9.     }

a) 2z
b) 2a
c) Error
d) 2y
View Answer

Answer: a
Explanation: In this program, We found the max value in the given value by using max function.
Output:

Free 30-Day Python Certification Bootcamp is Live. Join Now!
$ g++ max.cpp
$ a.out
max(1, 2) == 2
max('a', 'z') == z

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

  1.     #include <iostream>
  2.     #include <algorithm>
  3.     using namespace std;
  4.     bool myfn(int i, int j) 
  5.     {
  6.         return i < j;
  7.     }
  8.     int main () 
  9.     {
  10.         int myints[ ] = {3, 7, 2, 5, 6, 4, 9};
  11.         cout <<  *min_element(myints, myints + 7, myfn) << '\n';
  12.         cout << *max_element(myints, myints + 7, myfn) << '\n';
  13.         return 0;
  14.     }

a) 2 9
b) 2 7
c) 3 9
d) 3 5
View Answer

Answer: a
Explanation: In this program, We found out the minimum value and maximum value
of a range.
Output:

advertisement
$ g++ max1.cpp
$ a.out
2 
9

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

  1.     #include <iostream>
  2.     #include <algorithm>
  3.     #include <vector>
  4.     using namespace std;
  5.     int main () 
  6.     {
  7.         int myints[] = {10, 20, 30, 30, 20, 10, 10, 20};
  8.         vector<int> v(myints, myints + 8);
  9.         sort (v.begin(), v.end());
  10.         vector<int> :: iterator low, up;
  11.         low = lower_bound (v.begin(), v.end(), 20);
  12.         up = upper_bound (v.begin(), v.end(), 20);
  13.         cout << (low - v.begin()) << ' ';
  14.         cout << (up - v.begin()) << '\n';
  15.         return 0;
  16.     }

a) 3 6
b) 2 5
c) 2 6
d) 2 4
View Answer

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

  1.     #include <iostream>
  2.     #include <algorithm>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         cout << min(2, 1) << ' ';
  7.         cout << min('m','m') << '\n';
  8.         return 0;
  9.     }

a) Error
b) Runtime error
c) 1 m
d) 5 m
View Answer

Answer: c
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?

  1.     #include <iostream>
  2.     #include <algorithm>
  3.     #include <vector>
  4.     using namespace std;
  5.     bool mygreater (int i,int j) 
  6.     {
  7.         return (i > j);
  8.     }
  9.     int main () 
  10.     {
  11.         int myints[] = {10, 20, 30, 30, 20, 10, 10, 20};
  12.         vector<int> v(myints, myints + 8);
  13.         pair<vector<int> :: iterator, vector<int> :: iterator> bounds;
  14.         sort (v.begin(), v.end());
  15.         bounds = equal_range (v.begin(), v.end(), 20);
  16.         cout  << (bounds.first - v.begin());
  17.         cout << " and " << (bounds.second - v.begin()) << '\n';
  18.         return 0;
  19.     }

a) 3 and 6
b) 2 and 5
c) 3 and 5
d) 2 and 4
View Answer

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

Answer: c
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

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

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.