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 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.
C++ programming questions on “Floating Point Types” along with answers, explanations and/or solutions:
Q 1. Which of the following is not one of the sizes of the floating point types?
A. double
B. float
C. long double
D. short float
Show Answer
Answer:-D. short floatExplanation
Floating point types occur in only three sizes-float, long double and double.Q 2. What is the range of the floating point numbers?
A. -3.4E+38 to +3.4E+36
B. -3.4E+38 to +3.4E+34
C. -3.4E+38 to +3.4E+38
D. -3.4E+38 to +3.4E+32
Show Answer
Answer:-C. -3.4E+38 to +3.4E+38Explanation
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.Q 3. Which of the following is a valid floating-point literal?
A. f287.333
B. F287.333
C. 287.e2
D. 287.3.e2
Show Answer
Answer:-C. 287.e2Explanation
To make a floating point literal, we should attach a suffix of ‘f’ or ‘F’ and there should not be any blank space.Q 4. Which of three sizes of floating point types should be used when extended precision is required?
A. long double
B. double
C. float double
D. extended float
Show Answer
Answer:-A. long doubleExplanation
Float for single precision, double for double precision and long double for extended precision.Q 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. Stanford
B. Harvard
C. compile time error
D. runtime error
Show Answer
Answer:-B. HarvardExplanation
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 harvardQ 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
Show Answer
Answer:-B. 0.10000000000000001Explanation
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.10000000000000001Q 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. 123
C. 1.23
D. compile time error
Show Answer
Answer:-B. 123Explanation
The value 123 is printed because of its precision. $ g++ float.cpp $ a.out 123Q 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
Show Answer
Answer:-A. F or fExplanation
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
Show Answer
Answer:-A. equalExplanation
0.5f results in 0.5 to be stored in floating point representations. Output: $ g++ float.cpp $ a.out equalQ 10. Which is correct with respect to the size of the data types?
A. char > int < float
B. char < int < double
C. char < int < float
D. int < char > float
Leave a Reply