This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ints and arrays........ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ints and arrays........" Watch "ints and arrays........" New topic
Author

ints and arrays........

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
What will be the result of attempting to compile and run the following class?

Options:
1)The code will print i=1 iArr[0]=1;
2)The code will print i=1 iArr[0]=2;
3)The code will print i=2 iArr[0]=1
4)The code will print i=2 iArr[0]=2
5)The code will not compile
Answer :
2)The code will print i=1 iArr[0]=2
In this case, iArr[0] =2 (Agreed), but why is the value of i still 1, rather it should be incremented to 2??
Sonir
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
because i is passed by value not by reference


<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
i is passed by value and your array is passed by reference.
So once you get out of the incr method(), value of i is 1.


Younes
By constantly trying one ends up succeeding. Thus: the more one fails the more one has a chance to succeed.
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
What if you do this???


Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
Essouabni, the variable i is not defined in the scope of method void incr(int n), it is local to main()
You will get a compile error in your case.
Originally posted by Essouabni Younes:

<hr></blockquote>[/QB]
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
Right, I just wanted to see if you were aware of it.
Sorry, I didn't pay attention
[ January 25, 2002: Message edited by: Essouabni Younes ]
 
 
subject: ints and arrays........
 
Similar Threads
Incrementor operator
pass by value
Question Mock
a confused concept
can anyone explain this?