aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Marcus Exam1 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 Exam1" Watch "Marcus Exam1" New topic
Author

Marcus Exam1

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I doubt the answer is wrong for the followng question:
Given the following code what will be output?
public class Pass{
static int j=20;
public static void main(String argv[]){
int i=10;
Pass p = new Pass();
p.amethod(i);
System.out.println(i);
System.out.println(j);
}
public void amethod(int x){
x=x*2;
j=j*2;
}
}
1) Error: amethod parameter does not match variable
2) 20 and 40
3) 10 and 40
4) 10, and 20
The answer is 3, but I chose the 4. Is there anything I am missing? Please advise. thx!
Mike
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
since variable j is a static variable, j=j*2 does alter the value from 20 to 40. That's why the answer for the value of j is 40. while the value of i is unchanged and remain at 10.
Deepak M
Ranch Hand

Joined: Jul 10, 2000
Posts: 124
Originally posted by vasansrini:
since variable j is a static variable, j=j*2 does alter the value from 20 to 40. That's why the answer for the value of j is 40. while the value of i is unchanged and remain at 10.

Even if j is not static, the value of j changes !!!
if u remove static from the declaration, make sure u access j using its qualified name, i.e p.j coz main is static !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Marcus Exam1
 
Similar Threads
Could u explain this code....
static variable?
Help pl!
Moke Ex qstn
Expanations os scjp code