• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

John Meyer's SCJP 5 mock exam doubt

 
Ranch Hand
Posts: 59
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rizwan Patel
Ranch Hand
Posts: 59
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic