• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Java Inquisition Exam Question

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone explain this for me:

int []arr = {1,2,3,4};
for ( int i : arr )
{
arr[i] = 0;
}
for ( int i : arr )
{
System.out.println(i);
}

The options:
A Prints 0 0 3 0
B Prints 0 1 2 3
C Prints 0 0 0 0
D Runtime Exception Occurs
E Prints 0 2 0 0
F Compile error
-----------The exam says---
The correct answer is A.
This so happens because arr[3] was made 0 during an iteration of the first "for in loop". So during the final iteration of the first ��for in loop�� , we have 0 instead of 4 in the last index. It can be seen from the second loop why an ArrayIndexOutOfBoundsException was not thrown at runtime. The maximum number in the array is 3 which does not exceed the bound of the loop.

-------

I think it should be: "D" - Runtime exception (ArrayIndexOutofBound)

because it would loop throw all int values 1,2,3,4 and set the corresponding index of those numbers to 0 then I would think it would do soemthing like this---
so before first loop:
@Index / int value
0 / 1
1 / 2
2 / 3
3 / 4

After first loop
0 / 1 (since for (int i:array) -- first value is 1 so it would set the index 1's value not index 0)
1 / 0
2 / 0
3 / 0
then it will try to access array index on 4, which isn't there?
I don't understand the answer:
[ March 10, 2007: Message edited by: Ben Harrison ]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm confused about your question. Is the last paragraph the explanation from the exam or your explanation?
 
Ben Harrison
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry the last portion is from teh exam...they say the answer is a 0 0 3 0. I thought it should be runtime exception...
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well you don't have access to the iterator when you use the extended for loop, but what's happening is that you are changing the elements in the array while you are iterating through it.

The first iteration sets arr[1] = 0, so the array contents are 1 0 3 4.

The second iteration sets arr[0] = 0, so the array contents are 0 0 3 4.

The third iteration sets array[3] = 0, so the array contents are 0 0 3 0.

The last iteration sets array[0] = 0, so the array contents are 0 0 3 0.
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer A) is correct. Let's see, why. The loop will be over the index of the array assigning the value uf arr[loopindex] to i and setting arr[i]=0.

loopindex arr-content before arr-content after

0 1,2,3,4 i=1 1,0,3,4
1 1,0,3,4 i=0 0,0,3,4
2 0,0,3,4 i=3 0,0,3,0
3 0,0,3,0 i=0 0,0,3,0
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strange way of programming. It took me some time before I understood this, but the key is that your are changing the values during the iteration and by the following iteration you get the new values which you use to set the value.

I think this is a bad way of programming. I would never use this.
 
Ben Harrison
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Keith, big help on both my questions!
 
Proudly marching to the beat of a different kettle of fish... while reading this tiny ad
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic