This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Just Basic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Just Basic" Watch "Just Basic" New topic
Author

Just Basic

agrah upadhyay
Ranch Hand

Joined: Sep 01, 2005
Posts: 579
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

Joined: Sep 20, 2003
Posts: 1704
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

Joined: Oct 25, 2000
Posts: 7292

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.


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
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 ]

SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
agrah upadhyay
Ranch Hand

Joined: Sep 01, 2005
Posts: 579
Thank U Mr. green
But why it Is so Difficult 2 Understand.
Plz Explain more Claerly
Thank U


Agrah Upadhyay
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
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

Joined: Dec 07, 2000
Posts: 50
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


Amit<br /> <br />The Less I have, The more I gain..Off the Beaten Path, I Reign.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Just Basic