Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Inquisition Exam Question

 
Ben Harrison
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • 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 ]
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • 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
  • 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: 2409
  • Mark post as helpful
  • send pies
  • 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.
 
Anton Uwe
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • 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
 
Remko Strating
Ranch Hand
Posts: 893
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Keith, big help on both my questions!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic