C++ Programming Questions and Answers – Numeric Limits

This section on C++ questions and puzzles focuses on “Numeric Limits”. One shall practice these questions and puzzles to improve their C++ programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These programming puzzles can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ questions and puzzles on “Numeric Limits” along with answers, explanations and/or solutions:

1. To which type does the numeric limits are suitable?
a) Character types
b) Mixed type
c) Arithmetic types
d) Relational types
View Answer

Answer: c
Explanation: Numeric limits provides the information about the properties of arithmetic types.

2. Where does the member should be defined if it is used in the program?
a) Namespace scope
b) Character scope
c) Namespace & Character scope
d) Directional scope
View Answer

Answer: a
Explanation: The member shall still be defined in a namespace scope if it is used in the program.

3. What will the max function in the numeric limit will return for type float?
a) Maximum finite value for a float type
b) Maximum finite value
c) Minimum finite value
d) Minimum float value
View Answer

Answer: a
Explanation: Max function in the numeric limit will return the maximum finite value for a float type.
advertisement
advertisement

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

  1.     #include <iostream>
  2.     #include <limits>
  3.     using namespace std;
  4.     int main () 
  5.     {
  6.         cout << boolalpha;
  7.         cout << numeric_limits<int> :: has_infinity << '\n';
  8.         return 0;
  9.     }

a) 53234
b) false
c) true
d) 32564
View Answer

Answer: b
Explanation: In this program, We are checking whether the integer has limit or not by using has_infinity function.
Output:

Note: Join free Sanfoundry classes at Telegram or Youtube
$ g++ num.cpp
$ a.out
false

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

advertisement
  1.     #include <iostream>
  2.     #include <limits>
  3.     using namespace std;
  4.     int main( )
  5.     {
  6.         cout << numeric_limits<short int> :: max() << endl;
  7.     }

a) 53723
b) 32423
c) 32767
d) 4668
View Answer

Answer: c
Explanation: In this program, We are finding the max range for short int by using max function.
Output:

advertisement
$ g++ num1.cpp
$ a.out
32767

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

  1.     #include <iostream>
  2.     #include <limits>
  3.     using namespace std;
  4.     int main( )
  5.     {
  6.         cout << numeric_limits<float> :: is_exact;
  7.         cout << numeric_limits<double> :: is_exact;
  8.         cout << numeric_limits<long int> :: is_exact;
  9.         cout << numeric_limits<unsigned char> :: is_exact;
  10.     }

a) 0010
b) 0012
c) 1100
d) 0011
View Answer

Answer: d
Explanation: In this program, We are finding whether the types are free of rounding errors by using the function is_exact.
Output:

$ g++ num2.cpp
$ a.out
0011

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

  1.     #include <iostream> 
  2.     #include <limits>
  3.     using namespace std;
  4.     int main( )
  5.     {
  6.         cout << numeric_limits<float> :: digits10 << endl;
  7.         cout << numeric_limits<double> :: digits10 << endl;
  8.         float f = 99999999;
  9.         cout.precision ( 10 );
  10.         cout << f << endl;
  11.     }

a) 6
b) 15
c) 100000000
d)

6 
15
100000000
View Answer
Answer: d
Explanation: In this program, We are finding the number of decimal points that the type can represent without loss of precision.
Output:

$ g++ num3.cpp
$ a.out
6 
15
100000000
 
 

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

  1.     #include <iostream>
  2.     #include <limits>
  3.     using namespace std;
  4.     int main( )
  5.     {
  6.         cout << numeric_limits<float> :: min_exponent << endl;
  7.     }

a) 125
b) -125
c) 123
d) 521
View Answer

Answer: b
Explanation: In this program, We are finding the minimum radix of a type by using min_exponent function.
Output:

$ g++ num4.cpp
$ a.out
-125

9. Which header file is used for the numeric limits in C++?
a) <iostream>
b) <limits>
c) <number>
d) <digit>
View Answer

Answer: b
Explanation: <limits> header file will be used for numeric limits in C++.

10. Pick out the incorrect static function member in numeric limits.
a) denorm_min
b) digits
c) infinity
d) max_finite
View Answer

Answer: d
Explanation: max_finite is the incorrect static function member in numeric limits.

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]

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.