Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mock exam question

 
Tanuja Vaid
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic