• 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

Marcus Exam Q#54

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
inc.fermin(i);
i = i++;
System.out.println(i);
}
void fermin(int i){
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???
Wali
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Yeah, that's a tricky one:
inc.fermin(i);
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
or
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?
SEEYA
Jakob
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!,
Your are exactly correct, It prints 0 because in the line
inc.fermin(i);
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,.
------------------
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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??)
Confused,
Anand

 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 method.so 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
regards
sidhartha
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The confusion comes in because of
int i = 0;
i = i++;
System.out.println(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.
 
Anand Iyer
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Carl. It makes more sense now to me..
 
reply
    Bookmark Topic Watch Topic
  • New Topic