Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Just Basic

 
agrah upadhyay
Ranch Hand
Posts: 579
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


Why Output As 0?
Thank U.
############################3333
Agrah upadhyay
3rd year
B.tech
 
Kj Reddy
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember the intrinsic variables are passed by value. So the result is 0.
See my comments


public class Inc{
public static void main(String argv[]){
Inc inc = new Inc();
int i =0;
inc.fermin(i); // this wont change the value of i
i = i++; // i is assigned to "0" here and i++ wont change the i value here
System.out.println(i); // so it prints "0"
}
void fermin(int i){
i++;
}
}
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by agrah upadhyay:
What will happen when you attempt to compile and run the following code?


This question seems to test your understanding of pass-by-value and post-increment operators.

Passing i to fermin() is done by value. That is, which fermin receives is the value of i. It has no connection back to the i that was used to get this value. Therefore, anything fermin() does to it's "local i," which is a separate copy, has no effect on the i in main().

Secondly, when i is assigned the value i++, it is given the value of i before the ++ operator changes it. The increment value follows the assignment operation and is discarded once it's done.
 
Marcus Green
arch rival
Rancher
Posts: 2813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's always worth considering if mock exam questions come with answers, and ideally explanations. Fortunatly this one which comes from

http://www.jchq.net/mockexams/exam3.htm


does. You can tell because of the big letters at the top that say

It really does come with answers

This is a long document (around 35 pages), if you cannot see the answers then you have not got the entire document, try reloading it until you can see the answer to the final question and the words "End of document". The answers contain references to approximately which objective the question relates to. If you have a query about any of these questions, please, please include the full answer with the question. You can jump back from an answer to the question by pressing the back button on your web browser.


The text given as an explanation for this question is as follows

"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 concept to understand"

I'd be very interested if anyone thinks I should expand on this explanation.

Marcus

(edited repetition glitch)
[ September 15, 2005: Message edited by: Barry Gaunt ]
 
agrah upadhyay
Ranch Hand
Posts: 579
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank U Mr. green
But why it Is so Difficult 2 Understand.
Plz Explain more Claerly
Thank U


Agrah Upadhyay
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by agrah upadhyay:
Thank U Mr. green
But why it Is so Difficult 2 Understand.
Plz Explain more Claerly
Thank U


Agrah Upadhyay



It may help if you yourself write more clearly. English is understood here on JavaRanch.
 
Marcus Green
arch rival
Rancher
Posts: 2813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm fairly confident I understand the english of Agrahs question. What I don't understand is he doesn't understand. I am not being awkward here, it is one of the principles of learning and teaching in that in order to teach I am hugely helped by an understanding of what you currently understand.

Note it doesn't matter if your current understanding is incorrect, if so I may be able to correct it. However simply asking for an explanation means I have to second guess your current understanding which is somewhere between very difficult and impossible. This summer I completed a Post Graduate Teaching Qualification specialising in Adult education, so this is an issue of great interest. So Agrah, what is your current understanding and what parts do not make sense?

Agrah, please persist with this, I eventually want to see a post from you that says, "ahh I understand"...
 
Amit Goel
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just break down the statement i = i++; in the following three statements to make it atomic operation..

temp = i;
i = i + 1;
i = temp;

this breaking down makes i always vaulate to 0 (zero).

for eg: j = i++;
if you print i in this case it will be 1 and j will be 0(zero).

temp = i;
i = i + 1;
j = temp;

I hope this helps.

Thanx
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic