File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Marcus Exam Q#54 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 "Marcus Exam Q#54" Watch "Marcus Exam Q#54" New topic

Marcus Exam Q#54

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Question 54)

What will happen when you attempt to compile and run the following code?
public class Inc{
public static void main(String argv[]){
Inc inc = new Inc();
int i =0;
i = i++;
void fermin(int i){

1) Compile time error
2) Output of 2
3) Output of 1
4) Output of 0
The answer is 4 , it prints 0
If someone can explain in a bit detail why it is printing zero..
why not 1???
Jakob Bosshard

Joined: May 30, 2000
Posts: 21
Yeah, that's a tricky one:
the call to the method does not affect i, because you pass only a copy of the primitive. (I think you got that one)
i = i++;
Actually, you would never write such a statement but rather
i += 1; //sets i to 1
i++; //sets i to 1
The explanation is as follows:
i is assigned to itself before i is incremented (post-increment). The machine
caculates the right side first and then assigns the result to the left hand side which is i. Statement finished. That means that the increment which takes place after the assignment
has no effect at all. I think that is the way the compiler
handles such statements.
Note that this would work:
i = ++i; //i is now 1
Does it help or am I too confusing this morning?

Joined: Jun 09, 2000
Posts: 7
Your are exactly correct, It prints 0 because in the line
you are passing the copy of the 'i' so any thing made to 'i' is not going to make effect at 'i' in the PSVM().
Next your are incrementing i=i++; this is actually like it first assign the value and then it increment. It takes the LHS i as the (temp) and assign 0 to i; While you print 'i' you get the answer as 0.
Hope i am correct, Anything wrong in this please reply me,.
Anand Iyer

Joined: Jun 13, 2000
Posts: 26

1. i=0;
2. i=i++;
3. System.out.println(i);
This i=i++ thing has been explained earlier but I still am kind of stuck with this thinking:
AFAIK(As far as I know), the value is assigned then post increment is done.. So in the memory..this is how
variable i should be:
Due to assignment line 2) i=0
Due to the post incremental nature ..i=1
So..should not the i=1 be printed? Basically, before line 3 is executed, i should be having a value of 1.
(I know the results show otherwise.. Where is my thinking wrong??)

Marcus Green
arch rival

Joined: Sep 14, 1999
Posts: 2813
I always provide some explanation with answers, the explanation for this one is given as
"The method fermin only receives a copy of the variable i and any modifications to it are not reflected in the version in the calling method. The post increment operator ++ effectivly modifes the value of i after the initial value has been assiged to the left hand side of the equals operator. This can be a very tricky conept to understand"

SCWCD: Online Course, 50,000+ words and 200+ questions
sidhartha mohanty

Joined: Jun 13, 2000
Posts: 1
wali...u r right.the answer is :0.The given problem has a small trap .there r two i's. 1st one is local to main(); 2nd one is in fermin(). each one is local two their repective 1 is printed instead of 2.this is the only ambiguity in the problem.
but there is no ambiguity between 0 n 1.u can alwys compile n run ur code two see the ans
Carl Trusiak

Joined: Jun 13, 2000
Posts: 3340
The confusion comes in because of
int i = 0;
i = i++;
The print out of 0 deminstrates the order of operations that occur in the JVM.
first the value of i (0) is stored for passing thru the assignment operation.
second i is incremented by 1
finally the stored value is assigned to i (0).
This occurs because equations are evaluated right to left.
o hope this doesn't muddle things more.

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Anand Iyer

Joined: Jun 13, 2000
Posts: 26
Thanks Carl. It makes more sense now to me..
I agree. Here's the link:
subject: Marcus Exam Q#54
It's not a secret anymore!