wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mock exam question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Mock exam question" Watch "Mock exam question" New topic
Author

Mock exam question

Tanuja Vaid
Ranch Hand

Joined: Mar 07, 2002
Posts: 51
Here is the code......
public class MyTest{
int x=30;
public static void main(String args[]){
int x=20;
MyTest ta= new MyTest();
ta.method(x);
System.out.println("The x value is" +x);
}
void method(int y){
int x=y*y;
}
}
The correct answer is 20.Shouldn't it print 400?
Can somebody explain why?
Also when
int a=10;
float f=10;
How does a==f returns true
Val your input will be greatly appreciated........
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
note that the result of the computation in method() is assigned ot a local variable. Moreover, there is no way you could change the value of an primitive variable argument pass to a method since the argument is copied.
1. int a=10;
2. float f=10;
3. System.out.println(a==f);
On line 2, f's value is converted to 10.0f
On line 3, a is promoted to a float and then the comparison is carried out. Since both a and f have the same value (10.0f), the comparison yields true.
[ March 17, 2002: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
wood burning stoves
 
subject: Mock exam question