aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes John Meyer's SCJP 5 mock exam doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "John Meyer Watch "John Meyer New topic
Author

John Meyer's SCJP 5 mock exam doubt

Rizwan Patel
Ranch Hand

Joined: Jan 03, 2008
Posts: 59
First of all thanks for the mock exam provided.

I have gone through this test and have confusion in the below problem.



Can anyone explain why this happen ?

when we changes the first for loop as


scjp 1.5, OCPJWSD 5
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14672
    
  11

Take each loop step by step :

i = arr[0] <- 1
arr[1] = 0;
i = arr[1] <- 0 (it's been updated above)
arr[0] = 0;
i = arr[2] <- 3
arr[3] = 0;
i = arr[3] <- 0 (it's been updated above)
arr[0] = 0;

Result:0,0,3,0

Your for-loop is different. You are incrementing i from 0 to the length of the array. The enhanced for loop sets i to the value of each element of the array. It should look like


[My Blog]
All roads lead to JavaRanch
Rizwan Patel
Ranch Hand

Joined: Jan 03, 2008
Posts: 59
Thanks alot for this nice explaination.
I was making mistake in assuming both the for loops are equals.
Now it is much clear. Once again Thank you
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: John Meyer's SCJP 5 mock exam doubt
 
Similar Threads
Array Iteration Confusion
Arrays
for each loop and array
array doubt
enhanced for loop problem -- from a mock