Java Programming - Language Fundamentals - Discussion
Discussion Forum : Language Fundamentals - General Questions (Q.No. 12)
12.
Which three are valid declarations of a float?
- float f1 = -343;
- float f2 = 3.14;
- float f3 = 0x12345;
- float f4 = 42e7;
- float f5 = 2001.0D;
- float f6 = 2.81F;
Answer: Option
Explanation:
(1) and (3) are integer literals (32 bits), and integers can be legally assigned to floats (also 32 bits). (6) is correct because (F) is appended to the literal, declaring it as a float rather than a double (the default for floating point literals).
(2), (4),and (5) are all doubles.
Discussion:
36 comments Page 4 of 4.
Zubair said:
8 years ago
Hello, @Sulemankhan.
Everything written like float f2=3.14 is always double until it declared like float f2 =3.14f.
Everything written like float f2=3.14 is always double until it declared like float f2 =3.14f.
Ranjith said:
8 years ago
@Sundar.
Super, your explanation was amazing!.
Super, your explanation was amazing!.
Ramesh polaveni said:
8 years ago
Floating point variables have a precision of 6 digits whereas the precision of double is 14 digits.
In the question, f=3.14 which is more than 6 digits after the decimal. So it's not a float value. It belongs to double.
In the question, f=3.14 which is more than 6 digits after the decimal. So it's not a float value. It belongs to double.
(1)
Divya Prakash said:
7 years ago
How f1=-343 is float? Can anyone explain it please?
(2)
Asif Khan said:
7 years ago
f1=-343.
Because float ranges from 4.9e-324 to 3.4e+038.
Because float ranges from 4.9e-324 to 3.4e+038.
Abhishek said:
6 years ago
343 is an integer and in Java automatic type conversion takes place which is also called implicit type conversion through which an integer may be stored as a floating point literal in a float type variable.
In other words;
int a=5;
will store 5 as an integer value in int a.
but:
float k=a;
will convert 5 to 5.0f and store it in k.
In other words;
int a=5;
will store 5 as an integer value in int a.
but:
float k=a;
will convert 5 to 5.0f and store it in k.
(3)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers