aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Marcus Green Exam 3, Q 1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Marcus Green Exam 3, Q 1" Watch "Marcus Green Exam 3, Q 1" New topic
Author

Marcus Green Exam 3, Q 1

rehan hamid
Greenhorn

Joined: Feb 13, 2001
Posts: 16
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;
----------------------
I could not understand y opt) 1,2 are also correct in addition to option 4.
What I understand is / operator has more precedence then assigment so first 1/3 should be evaluated. Then it will become "0.33" that should be treeted as double by default. And double value can not be assigned to float or int. So only option 4 should be correct...
Any explanation???
Rehan
ryan burgdorfer
Ranch Hand

Joined: Jan 24, 2001
Posts: 219
I beleive a double can be assigned to a float or an int...it just loses precision. In the int case, anything after the decimal point is dropped, so it ends up benig 0. And in the float case, there is a chance of losing precision in larger numbers, but in this case it keeps the same value.
Correct me if I am wrong...


<UL TYPE=SQUARE><I><LI>Ryan Burgdorfer<BR><LI>Java Acolyte</I></UL>
rehan hamid
Greenhorn

Joined: Feb 13, 2001
Posts: 16
Originally posted by ryan burgdorfer:
I beleive a double can be assigned to a float or an int...it just loses precision. In the int case, anything after the decimal point is dropped, so it ends up benig 0. And in the float case, there is a chance of losing precision in larger numbers, but in this case it keeps the same value.
Correct me if I am wrong...

I dont think that you can assign more broden data types to less broden data types unless type casted.
Udayan Naik
Ranch Hand

Joined: Oct 18, 2000
Posts: 135
Hi rehan.You are right in stating that a floating point value by default is interpreted as a double in Java.Let's go through the code..

The expression

cannot be evaluated at compile time.
Division will be performed at runtime,and 0.333...3 will be obtained,which will be a double value.But this value is being stored in a variable of type float,and this results in loss of precision.Now float f will have value 0.0.
Same is the case for int
But the expression

can evaluated at compile time ,since 1.01 is a compile time constant.Here the compiler evaluates 1.01 to a double value,and as such throws an Exception.
I hope that helps.
------------------
Udayan Naik
Sun Certified Java 2 Programmer


Udayan Naik<BR>Sun Certified Programmer for the Java 2 Platform
Yoo-Jin Lee
Ranch Hand

Joined: Nov 01, 2000
Posts: 119
Hi,
I believe the reason both 1 and 2 are correct is that / always returns an int. Hence since the size of float is the same as int there's no problem with (1).
cheerios,
Yoo-Jin.
Udayan Naik
Ranch Hand

Joined: Oct 18, 2000
Posts: 135
Yeah,actually both the operands are integers,so integer division is done,result of which is an int.This int can now be stored in a float as well as an int.

My mistake.Sorry for misleading.
------------------
Udayan Naik
Sun Certified Java 2 Programmer
[This message has been edited by Udayan Naik (edited March 03, 2001).]
puneet pruthi
Greenhorn

Joined: Feb 10, 2001
Posts: 14
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;
----------------------
I could not understand y opt) 1,2 are also correct in addition to option 4.
hi friends!!
option 1,2 and 4 r correct .A double cant b assigned to an int as an int of 32 bits cant store double of 64 bits or u have to cast it.
in the first option 1/3 1 is an int and 3 is also taken as an int so the operation involves int/int which returns an int and since an int can b stored in a float,therefore it is correct.
second option is also correct as it also returns an int and the result is stored in an int(ans will b 0).
Third option is false coz 1.01 is double by default and u cant store double in float(1.01 is taken as 1.01d).it will run fine if u rite 1.01f.
fourth is no problem,it will run fine without riting a d suffix.
hope u got that!!
rehan hamid
Greenhorn

Joined: Feb 13, 2001
Posts: 16
THANKS ALL OF YOU!!
Yoo-Jin Lee
Ranch Hand

Joined: Nov 01, 2000
Posts: 119
Puneet,
I don't believe that you can use d to indicate a double just f or l for float and long, respectively.
Just a note.
Yoo-Jin.
tony hutcheson
Ranch Hand

Joined: Dec 08, 2000
Posts: 38
a note to Yoo-Jin Lee: You can use a "d" to designate a double. It is understood to be there if you do not put it. Therefore, it is a valid assignment.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Marcus Green Exam 3, Q 1
 
Similar Threads
about variable declaration
Assigning Values to Primitive Types
Marcus Green mock exam 3 Q1
Marcus Q1
float or double