Floating Point Types(C++ Programming ) Questions and Answers

Question 1.

Which is correct with respect to size of the datatypes?


  1.    char > int < float
  2.    int < char > float
  3.    char < int < float
  4.    char < int < double
Explanation:-
Answer: Option D. -> char < int < double

The 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
  1.    equal
  2.    not equal
  3.    compile time error
  4.    runtime error
Explanation:-
Answer: Option A. -> equal

0.5f results in 0.5 to be stored in floating point representations.
Output:
$ g++ float.cpp
$ a.out
equal



Question 3.

Which is used to indicate single precision value?


  1.    F or f
  2.    L or l
  3.    either a or b
  4.    neither a or b
Explanation:-
Answer: Option A. -> F or f

None.



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
  1.    123.00
  2.    1.23
  3.    123
  4.    compile time error
Explanation:-
Answer: Option C. -> 123

The 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
  1.    0.11
  2.    0.10000000000000001
  3.    0.100001
  4.    compile time error
Explanation:-
Answer: Option B. -> 0.10000000000000001

The 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



Question 6.

Which of the following is a valid floating point literal?


  1.    f287.333
  2.    F287.333
  3.    287.e2
  4.    287.3.e2
Explanation:-
Answer: Option C. -> 287.e2

To make a floating point literal, we should attach a suffix of ‘f’ or ‘F’ and there should not be any 

blank space.



Question 7.

What is the range of the floating point numbers?


  1.    -3.4E+38 to +3.4E+38
  2.    -3.4E+38 to +3.4E+34
  3.    -3.4E+38 to +3.4E+36
  4.    -3.4E+38 to +3.4E+32
Explanation:-
Answer: Option A. -> -3.4E+38 to +3.4E+38

None.



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
  1.    harvard
  2.    stanford
  3.    compile time error
  4.    runtime error
Explanation:-
Answer: Option A. -> harvard

Float 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



Question 9.

Which of the following is not one of the sizes of the floating point types?


  1.    short float
  2.    float
  3.    long double
  4.    double
Explanation:-
Answer: Option A. -> short float

Floating point types occur in only three sizes-float, long double and double.



Question 10.

Which of three sizes of floating point types should be used when extended precision is required?


  1.    float
  2.    double
  3.    long double
  4.    extended float
Explanation:-
Answer: Option C. -> long double

Float for single precision, double for double precision and long double for extended precision.