Floating Point Types(C++ Programming ) Questions and Answers
Explanation:-
Answer: Option D. -> char < int < doubleThe char has lesser bytes than int and int has lesser bytes than double whereas int and float
can potentially have same sizes.
Question 2.
What is the output of this program?
1.
#include
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
- not equal
- compile time error
- runtime error
Explanation:-
Answer: Option A. -> equal0.5f results in 0.5 to be stored in floating point representations.
Output:
$ g++ float.cpp
$ a.out
equal
Explanation:-
Answer: Option A. -> F or fNone.
Question 4.
What is the output of the following program?
1.
#include
2.
using namespace std;
3.
int main()
4.
{
5.
float i = 123.0f;
6.
cout
- 123.00
- 1.23
- 123
- compile time error
Explanation:-
Answer: Option C. -> 123The value 123 is printed because of its precision.
$ g++ float.cpp
$ a.out
123
Question 5.
What is the output of this program?
1.
#include
2.
#include
3.
using namespace std;
4.
int main()
5.
{
6.
cout
- 0.11
- 0.10000000000000001
- 0.100001
- compile time error
Explanation:-
Answer: Option B. -> 0.10000000000000001The double had to truncate the approximation due to it’s limited memory, which resulted in a number
that is not exactly 0.1.
Output:
$ g++ float2.out
$ a.out
0.10000000000000001
Explanation:-
Answer: Option C. -> 287.e2To make a floating point literal, we should attach a suffix of ‘f’ or ‘F’ and there should not be any
blank space.
Explanation:-
Answer: Option A. -> -3.4E+38 to +3.4E+38None.
Question 8.
What is the output of this program?
1.
#include
2.
using namespace std;
3.
int main()
4.
{
5.
float num1 = 1.1;
6.
double num2 = 1.1;
7.
if (num1 == num2)
8.
cout
- harvard
- stanford
- compile time error
- runtime error
Explanation:-
Answer: Option A. -> harvardFloat store floating point numbers with 8 place accuracy and requires 4 bytes of Memory. Double
has 16 place accuracy having size of 8 bytes.
Output:
$ g++ float3.cpp
$ a.out
harvard
Explanation:-
Answer: Option A. -> short floatFloating point types occur in only three sizes-float, long double and double.
Explanation:-
Answer: Option C. -> long doubleFloat for single precision, double for double precision and long double for extended precision.