• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Mock exam question

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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........
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic