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
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
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
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.
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
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?
#include <iostream>
using namespace std;
int main()
{
float num1 = 1.1;
double num2 = 1.1;
if (num1 == num2)
cout << "stanford";
else
cout << "harvard";
return 0;
}
a) harvard
b) stanford
c) compile time error
d) runtime error
View Answer
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:
$ g++ float3.cpp $ a.out harvard
6. What will be the output of the following C++ code?
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
cout << setprecision(17);
double d = 0.1;
cout << d << endl;
return 0;
}
a) 0.11
b) 0.10000000000000001
c) 0.100001
d) compile time error
View Answer
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?
#include <iostream>
using namespace std;
int main()
{
float i = 123.0f;
cout << i << endl;
return 0;
}
a) 123.00
b) 1.23
c) 123
d) compile time error
View Answer
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
Explanation: Either F or f can be used to indicate single precision values.
9. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
float f1 = 0.5;
double f2 = 0.5;
if (f1 == 0.5f)
cout << "equal";
else
cout << "not equal";
return 0;
}
a) equal
b) not equal
c) compile time error
d) runtime error
View Answer
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
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]
- Apply for C++ Internship
- Check Computer Science Books
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Check C++ Books