The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in Array" Watch "Doubt in Array" New topic

Doubt in Array

Lata Bagga
Ranch Hand

Joined: Jun 13, 2007
Posts: 35
Hello Friends,

I have a doubt in the following code:

The answer is c.

I am not able to understand how the code works and gives the output as c.

Vishwanath Krishnamurthi
Ranch Hand

Joined: Jun 04, 2007
Posts: 331
Hi Lata,

for the first value in the array:
arr[1]<-- 0
so arr becomes,

for the next value in the array: [Remb: The array is changed now]

arr[0]<-- 0;
and arr is now,

for the next value in the array:


and for the last value in the array:
so, arr remains {0,0,3,0}


Lata Bagga
Ranch Hand

Joined: Jun 13, 2007
Posts: 35
Thanks a lot Vishwanath for such a nice explanation. I got it now.
Akshay Dashrath

Joined: Jan 10, 2008
Posts: 11
I did'nt understand this . What happens to arr[2] and why does arr[0] come twice?

ishmayel vemuru
Ranch Hand

Joined: Jun 13, 2007
Posts: 46
Hi Vishwa..
Thanks for your Reply..
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 802

this is very nice question
thanks to Vishwa for fantastic explanation

SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
Nice question :-)

SCJP Java 6 (98%) - Story, SCJA (88%) - Story
Milan Sutaria
Ranch Hand

Joined: Jul 10, 2008
Posts: 122
its a nice Question Lata and a nice explaination by murthi too.

For those who did not get it .... try reading Arrays
[ July 15, 2008: Message edited by: Milan Sutaria ]

SCJP 6 83%
I agree. Here's the link:
subject: Doubt in Array
It's not a secret anymore!