This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Marcus Q1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Marcus Q1" Watch "Marcus Q1" New topic

Marcus Q1


Joined: Jun 15, 2000
Posts: 25
Here is a question from Marcus
Which of the following are legal statements?
1) float f=1/3;
2) int i=1/3;
3) float f=1.01;
4) double d=999d;
The correct answer is 1,2,4.
I compiled & got it correct too.But i have a small doubt.When u say float f=1/3,why does'nt it consider 1/3 which 0.3333 as double.We know that the default type for decimal type is double.Why does'nt it give an error for No.2 option too?I have the same doubt here.Can anyone please make it clear for me?
Savithri Devaraj
Ranch Hand

Joined: Jun 26, 2000
Posts: 103
I think it is considering 1/3 as integer division in both 1. and 2. The result integer 0 is assigned to float in case 1 and to int in case 2.
Raj Devaraj
Ranch Hand

Joined: Jun 29, 2000
Posts: 44
In case of 1, f = 1/3 is converted to 1.0f/3.0f
Michal Harezlak
Ranch Hand

Joined: Jul 06, 2000
Posts: 185
Originally posted by Raj Devaraj:
In case of 1, f = 1/3 is converted to 1.0f/3.0f

i am afraid it is not true. try this:
float ff = 1/3;
float f=1.0f/3.0f;


Joined: Jun 19, 2000
Posts: 11
I believe Savithri is right.
In float f = 1/3; The right hand side is first caculated.
The result integer 0 is thenprompted as 0f and assigned
to the left hand side.
Ranch Hand

Joined: Jun 28, 2000
Posts: 93
I think Savuthiri is right . I executed this in JBuilder 2 and works out.
Michael: I too hope that Devaraj's statement is not true.
I agree. Here's the link:
subject: Marcus Q1
Similar Threads
about variable declaration
Marcus Green Exam 3, Q 1
Double and float types - Mock exam question
About the data type
float or double