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

Advance For Loop Dought

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given:

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

and the options

a.Prints 0 1 2 3
b.Prints 0 0 0 0
c.Prints 0 0 3 0
d.Prints 0 2 0 0
e.Compile error
f.Runtime Exception occurs.

I think the answer should have been 'b' but the correct answer is 'c',can someone please tell me how the answer is 'c' and not 'b'.

Thanks in Advance.

Regards,
Bhavesh Thakkar
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
the array values changes like this
i values are i=1 i=0 i=3 i=0
{1,2,3,4},{1,0,3,4},{0,0,3,4},{0,0,3,0},{0,0,3,0}
 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by karnatam narendraprasad:
hi
the array values changes like this
i values are i=1 i=0 i=3 i=0
{1,2,3,4},{1,0,3,4},{0,0,3,4},{0,0,3,0},{0,0,3,0}



Yes. I think the i in the for() loop is not the index of the array, instead, it's the element of the array.
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bhavesh,

Option c is the right answer.


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


Lets iterate the loop to see how we get this answer.
1) On first go, value of i is 1 (because enhanced for loop, makes the iterator iterate on the elements of the array or the collection) therefore arr[1] will become 0, so after this arr will be {1,0,3,4}
2) In second round, value of i is 0 because the next element of the array is 0 thus making arr[0] = 0 and now we have arr as {0,0,3,4}
3) On third iteration, value of i is 3 this will update arr[3] to 0 after which arr looks like {0,0,3,0}
4) In last iteration, again value of i is 0 so arr[0] will be 0 and it was already 0.
Thus when the loop is over final value in arr will be {0,0,3,0}

I hope this explains why choice c was right.

Thanks,
-Rancy
 
Bhavesh Thakkar
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey thanks everyone i think my dought is clear now.

Regards,
Bhavesh Thakkar
 
So there I was, trapped in the jungle. And at the last minute, I was saved by this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic