Two Laptop Bag*
The moose likes Mock Exam Errata and the fly likes Marcus Green exam #3 question 54 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "Marcus Green exam #3 question 54" Watch "Marcus Green exam #3 question 54" New topic
Author

Marcus Green exam #3 question 54

Karlon Wu
Greenhorn

Joined: Jul 09, 2001
Posts: 4
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, i.e., i=0. But I still cannot understand why
{int i=0; i=i++;} will be 0. Can somebody explain why? Thank you.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Karlon
Check out this thread for a good explaination http://www.javaranch.com/ubb/Forum24/HTML/009768.html


Dave
Karlon Wu
Greenhorn

Joined: Jul 09, 2001
Posts: 4
Thanks, Dave. I'll get the exam after 3 days. It really helps me.
easwaran ram
Greenhorn

Joined: Jul 14, 2001
Posts: 2

you should understand the difference between assignments and uninar incremental operator.
when you put
i=0;
i=i+10;
it assignes value to i a value 0+10=10
where as
i=i++
it assignes value
i=i and then increments,
ie i=0
when you pass as a parameter it passes only the copy of the value, hence the original value never changes. that is why you get 0 as output in this code.

Originally posted by Dave Vick:
Karlon
Check out this thread for a good explaination http://www.javaranch.com/ubb/Forum24/HTML/009768.html


easwaran
easwaran ram
Greenhorn

Joined: Jul 14, 2001
Posts: 2
check this
public class Inc{
public static void main(String argv[]){
Inc inc = new Inc();
int i =0;
i=i+10;
i+=++i;
inc.fermin(i);
System.out.println(i);
i+=++i;
System.out.println(i);
}
void fermin(int i){
i+=100;
}
}
Originally posted by Dave Vick:
Karlon
Check out this thread for a good explaination http://www.javaranch.com/ubb/Forum24/HTML/009768.html

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Marcus Green exam #3 question 54
 
Similar Threads
No increment at all??
NO.54 from Marcus green - Exam3
lost++
Marcus Exam Q#54
Just Basic