aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Arrays 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 "Arrays" Watch "Arrays" New topic
Author

Arrays

Manpreet Kaur
Greenhorn

Joined: Jul 03, 2008
Posts: 8
Hi Ranchers,

The following question is from John Meyers mock exam:

int[] arr = {1,2,3,4,5};

for(int i : arr)
{
arr[i] = 0;
}

for(int i : arr)
{
System.out.println(arr[i])

}

What wil be the output of the program ?

a) 0030
b) 0000

The coirrect answer is 0030.

Can anyone explain how ?
Mihai Fonoage
Ranch Hand

Joined: Feb 25, 2006
Posts: 39
To be honest, I expected this code to give you an ArrayIndexOutOfBounds exception due to the fact that at some point you will have "arr[5] = 0". Are you sure this is the right code for the answer you provided?

Mihai Fonoage
vaibhav mishra
Ranch Hand

Joined: Jun 18, 2008
Posts: 168
(offtopic) frankly similar question which made me fall in love with javaranch (offtopic)

PS:I assumed that code
<blockquote>code:
<pre name="code" class="core">System.out.println(arr[i])</pre>
</blockquote>where you leave semicolon is infact your mistake and I inserted it there in my explanation otherwise program won't even compile.

the answer you get here is not correct
examine the code through some debugger
search this forum , there are couple of threads explaining exact question

[b]1st iteration of for loop[b]
i=arr[0]
so i is 1 now
and by this

arr[i]=0
the arr changes to
arr={1,0,3,4,5}



[b]2nd iteration of for loop[b]
i=arr[1]

remember array got updated in previous iteration

so i is 0 now
and by this code

arr[i]=0

the arr changes to
arr={0,0,3,4,5}

[b]3rd iteration of for loop[b]
i=arr[2]

remember array got updated in previous iteration but arr[2] do not

so i is 3 now
and by this code

arr[i]=0

the arr changes to
arr={0,0,3,0,5}

[b]4th iteration of for loop[b]
i=arr[3] // arr[3] current value is 0

remember array got updated in previous iteration

so i is 0 now
and by this code

arr[i]=0

the arr changes to
arr={0,0,3,0,5}

or simple words this iteration do not update the array

[b]5th iteration of for loop[b]
i=arr[4] // arr[4] current value is 5

so i is 0 now
and by this code

arr[i]=0

here we get excetion so probably you wrote this question incorrectly or your source of question is not reliable

I explained because it shows a good point which you might miss


SCJP
Milind Patil
Greenhorn

Joined: Oct 19, 2003
Posts: 26
Hi Manpreet,

Check out this.
Doubt in Array


Milind B. Patil - SCJP-1.4, SCWCD-5.0, SCBCD-5.0
Manpreet Kaur
Greenhorn

Joined: Jul 03, 2008
Posts: 8
Excellent Vaibhav. Thanks. And thank you Milind for the link..
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Hi milind

Thanks for the link.

But I still didn't get it!

How does the enhanced loop work.

What's the initalisation and the condition part.

I thought it's

i=0 and i<arr.length by Default for (int i : arr) (any enhanced loop)

What do you mean by "value will be the same as the current array element"
as mentioned in K&B


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
The coirrect answer is 0030.


How is that the correct answer..

I am getting
ArrrayIndexOutOfBoundException
Christian Polychroniadis
Greenhorn

Joined: May 18, 2008
Posts: 8
Hi, Nabila Mohammad

Please change the array initialization, to {1,2,3,4} instead of {1,2,3,4,5}

if you included 5, you will get that error.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

Did you try it out, Christian? If you change it to {1, 2, 3, 4} then you will still get an ArrayIndexOutOfBoundsException.

Note that:

for (int i : arr)

is not the same as:

for (int i = 0; i < arr.length; i++)

Instead, it is the same as this:

<blockquote>code:
<pre name="code" class="core">int i;
for (int k = 0; k < arr.length; k++) {
i = arr[k];
</pre>
</blockquote>

Look at it carefully. Do you see now what's happening? i is not the index into the array, but the array element itself.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Originally posted by Christian Polychroniadis:
Hi, Nabila Mohammad

Please change the array initialization, to {1,2,3,4} instead of {1,2,3,4,5}

if you included 5, you will get that error.



After changing it to {1,2,3,4} I am getting 0000 .
Phal Ach
Ranch Hand

Joined: May 09, 2008
Posts: 54
I just went through link given by Milind and there the question is same but the value in arr in {1,2,3,4}. If I solve it on paper it gives me the answer 0030 which is the correct answer. But if I try to code it and solve it on my laptop, it gives the answer 0000. Where am I going wrong? Or the JVM is wrong?? Please help.
Meena Ajay
Ranch Hand

Joined: May 28, 2008
Posts: 38

Hi All,
I changed the arr contents to {1,2,3,4} and tried to solve the problem and here is what i got.
I had the same doubt as phal had.When I solved the problem, I found that 0030 must be the answer.But when I executed the program , i found the answer as 0000.
Then I introducted the following portion in the code ,before the enhanced for loop to print the array elements given at the end.
<blockquote><font size="1" face="Verdana, Arial">code:</font><hr><pre name="code" class="core"><font size="2">
for (int k=0;k<arr.length ;k++ )
{
System.out.println(arr[k]);
}</font></pre><hr></blockquote>


Then I understood that the for loop given at the end of the program, actually gets the elements of array one by one , assigns it to i and then again gets the content in a[i].

Now as per the program, after the first for loop, the array contents are {0,0,3,0}

Now Consider the execution of
<blockquote><font size="1" face="Verdana, Arial">code:</font><hr><pre name="code" class="core"><font size="2">for(int i : arr)
{
System.out.println(arr[i]);
}</font></pre><hr></blockquote>
1.First iteration i = a[0], so i becomes 0 and we print a[i] i.e, a[0] = 0
2.First iteration i = a[1], so i becomes 0 and we print a[i] i.e, a[0] = 0
3.First iteration i = a[2], so i becomes 3 and we print a[i] i.e, a[3] = 0
4.First iteration i = a[3], so i becomes 0 and we print a[i] i.e, a[0] = 0

That is how we get {0,0,0,0} printed in our console
If the print statement directly prints 'i', instead of a[i], then we would get 0030
Please let me know if my understanding is correct.
Thanks.
[ July 15, 2008: Message edited by: Meena Subramanian ]

Cheers, Meena
OCPJP 6
vaibhav mishra
Ranch Hand

Joined: Jun 18, 2008
Posts: 168

I just went through link given by Milind and there the question is same but the value in arr in {1,2,3,4}. If I solve it on paper it gives me the answer 0030 which is the correct answer. But if I try to code it and solve it on my laptop, it gives the answer 0000. Where am I going wrong? Or the JVM is wrong?? Please help.
you are doing it wrong
when printing loop you might be using this code
<blockquote>code:
<pre name="code" class="core">
System.out.println(arr[j]);
</pre>
</blockquote>
instead you should use this
<blockquote>code:
<pre name="code" class="core">System.out.println(j);</pre>
</blockquote>
I hope this might show you what exactly is your mistake

this code
<blockquote>code:
<pre name="code" class="core">
for(int i : arr)
{
System.out.println(arr[i]);
}
</pre>
</blockquote>
is same as
<blockquote>code:
<pre name="code" class="core">for(int i=0 ; i<arr.length;i++)
{
System.out.println(arr[arr[j]]);
}
</pre>
</blockquote>
which is not clearly code for printing our array






1.First iteration i = a[0], so i becomes 0 and we print a[i] i.e, a[0] = 0
2.First iteration i = a[1], so i becomes 0 and we print a[i] i.e, a[0] = 0
3.First iteration i = a[2], so i becomes 3 and we print a[i] i.e, a[3] = 0
4.First iteration i = a[3], so i becomes 0 and we print a[i] i.e, a[0] = 0


this explains

[ July 15, 2008: Message edited by: vaibhav mishra ]
[ July 15, 2008: Message edited by: vaibhav mishra ]
Christian Polychroniadis
Greenhorn

Joined: May 18, 2008
Posts: 8
OK, I'm going to explain, first you must change the array declaration to {1,2,3,4}, and that's what happen in the first for,


initial array, value, final array
iteration 1: {1,2,3,4} a[1]=0 {1,0,3,4}
iteration 2: {1,0,3,4} a[0]=0 {0,0,3,4}
iteration 3: {0,0,3,4} a[3]=0 {0,0,3,0}
iteration 4: {0,0,3,0} a[0]=0 {0,0,3,0}

The problem is the second for, if you see it, it do something like this:

print a[0] three times, and a[3] once, that's why you get 0 0 0 0.

If you want to print all the content of the array you need change it:

for(int i=0;i<arr.length;i++)
System.out.println(arr[i]);

that's how you get 0 0 3 0

Now I think is clear.
Phal Ach
Ranch Hand

Joined: May 09, 2008
Posts: 54
Thanks guys for your detailed explanation. It really helped.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

Hey, you are right, Christian. I didn't look into it far enough.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Arrays