File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Java Inquisition Exam Question 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 "Java Inquisition Exam Question" Watch "Java Inquisition Exam Question" New topic
Author

Java Inquisition Exam Question

Ben Harrison
Greenhorn

Joined: Jan 26, 2006
Posts: 27
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

Joined: Feb 07, 2005
Posts: 2367
I'm confused about your question. Is the last paragraph the explanation from the exam or your explanation?
Ben Harrison
Greenhorn

Joined: Jan 26, 2006
Posts: 27
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Jan 10, 2007
Posts: 122
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

Joined: Dec 28, 2006
Posts: 893
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.


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Ben Harrison
Greenhorn

Joined: Jan 26, 2006
Posts: 27
Thanks Keith, big help on both my questions!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Inquisition Exam Question
 
Similar Threads
Why will I get this output?
Very Interesting Question
Advance For Loop Dought
Array Iteration Confusion
for each