C++ Programming Questions and Answers – Floating Point Types

This section on advanced C++ programming questions focuses on “Floating Point Types”. One shall practice these advanced C++ questions to improve their C++ programming skills needed for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive exams. These questions 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 advanced C++ questions come with the detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of advanced C++ programming questions on “Floating Point Types” along with answers, explanations and/or solutions:

1. Which of the following is not one of the sizes of the floating point types?
a) short float
b) float
c) long double
d) double
View Answer

Answer: a
Explanation: Floating point types occur in only three sizes-float, long double and double.

2. Which of the following is a valid floating-point literal?
a) f287.333
b) F287.333
c) 287.e2
d) 287.3.e2
View Answer

Answer: c
Explanation: To make a floating point literal, we should attach a suffix of ‘f’ or ‘F’ and there should not be any blank space.

3. What is the range of the floating point numbers?
a) -3.4E+38 to +3.4E+38
b) -3.4E+38 to +3.4E+34
c) -3.4E+38 to +3.4E+36
d) -3.4E+38 to +3.4E+32
View Answer

Answer: a
Explanation: This is the defined range of floating type number sin C++. Also range for +ve and -ve side should be same so the answer is -3.4E+38 to +3.4E+38.
advertisement
advertisement

4. Which of three sizes of floating point types should be used when extended precision is required?
a) float
b) double
c) long double
d) extended float
View Answer

Answer: c
Explanation: Float for single precision, double for double precision and long double for extended precision.

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

Note: Join free Sanfoundry classes at Telegram or Youtube
  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         float num1 = 1.1;
  6.         double num2 = 1.1;
  7.         if (num1 == num2)
  8.            cout << "stanford";
  9.         else
  10.            cout << "harvard";
  11.         return 0;
  12.     }

a) harvard
b) stanford
c) compile time error
d) runtime error
View Answer

Answer: a
Explanation: Float store floating point numbers with 8 place accuracy and requires 4 bytes of Memory. Double has 16 place accuracy having the size of 8 bytes.
Output:

advertisement
$ g++ float3.cpp
$ a.out
harvard

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

advertisement
  1.     #include <iomanip>
  2.     #include <iostream>
  3.     using namespace std;
  4.     int main()
  5.     {
  6.         cout << setprecision(17);
  7.         double d = 0.1;
  8.         cout << d << endl;
  9.         return 0;
  10.     }

a) 0.11
b) 0.10000000000000001
c) 0.100001
d) compile time error
View Answer

Answer: b
Explanation: The double had to truncate the approximation due to its limited memory, which resulted in a number that is not exactly 0.1.
Output:

$ g++ float2.out
$ a.out
0.10000000000000001

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         float i = 123.0f;
  6.         cout << i << endl;
  7.         return 0;
  8.     }

a) 123.00
b) 1.23
c) 123
d) compile time error
View Answer

Answer: c
Explanation: The value 123 is printed because of its precision.

$ g++ float.cpp
$ a.out
123

8. Which is used to indicate single precision value?
a) F or f
b) L or l
c) Either F or for L or l
d) Neither F or for L or l
View Answer

Answer: a
Explanation: Either F or f can be used to indicate single precision values.

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         float f1 = 0.5;
  6.         double f2 = 0.5;
  7.         if (f1 == 0.5f)
  8.             cout << "equal";
  9.         else
  10.             cout << "not equal";
  11.         return 0;
  12.     }

a) equal
b) not equal
c) compile time error
d) runtime error
View Answer

Answer: a
Explanation: 0.5f results in 0.5 to be stored in floating point representations.
Output:

$ g++ float.cpp
$ a.out
equal

10. Which is correct with respect to the size of the data types?
a) char > int < float
b) int < char > float
c) char < int < float
d) char < int < double
View Answer

Answer: d
Explanation: The char has less bytes than int and int has less bytes than double whereas int and float can potentially have same sizes.

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.