aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes for loops in collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "for loops in collections" Watch "for loops in collections" New topic
Author

for loops in collections

Rachel Glenn
Ranch Hand

Joined: Oct 24, 2012
Posts: 95
I have this sample question from a mock exam:

Given:

- list is a reference to a valid collection
- getCollection() returns a reference to a valid collection

Which two are valid? (Choose two.)
A. for(Object o ; list)
B. for(Object o : list.iterator())
C. for(Object o : getCollection())
D. for(Iterator i ; list.iterator() ; i.hasNext() )
E. for(Iterator i = list.iterator(); i.hasNext(); )



I understand that answer E is one of the correct options. But it seems to me that options A AND C are also valid. Can someone please explain? (Option C is the other correct option.)
Zhenyi Luo
Ranch Hand

Joined: Sep 03, 2012
Posts: 33
Rachel Glenn wrote:I have this sample question from a mock exam:

Given:

- list is a reference to a valid collection
- getCollection() returns a reference to a valid collection

Which two are valid? (Choose two.)
A. for(Object o ; list)
B. for(Object o : list.iterator())
C. for(Object o : getCollection())
D. for(Iterator i ; list.iterator() ; i.hasNext() )
E. for(Iterator i = list.iterator(); i.hasNext(); )



I understand that answer E is one of the correct options. But it seems to me that options A AND C are also valid. Can someone please explain? (Option C is the other correct option.)


A should be for(Object o : list) instead of for(Object o ; list) , so answer is CE
Rachel Glenn
Ranch Hand

Joined: Oct 24, 2012
Posts: 95
Zhenyi Luo wrote:
Rachel Glenn wrote:I have this sample question from a mock exam:

Given:

- list is a reference to a valid collection
- getCollection() returns a reference to a valid collection

Which two are valid? (Choose two.)
A. for(Object o ; list)
B. for(Object o : list.iterator())
C. for(Object o : getCollection())
D. for(Iterator i ; list.iterator() ; i.hasNext() )
E. for(Iterator i = list.iterator(); i.hasNext(); )



I understand that answer E is one of the correct options. But it seems to me that options A AND C are also valid. Can someone please explain? (Option C is the other correct option.)


A should be for(Object o : list) instead of for(Object o ; list) , so answer is CE


OOOPS!! THANK YOU!

BUT, if A was for(Object o: list), then it would also have been correct ?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

Rachel Glenn wrote:BUT, if A was for(Object o: list), then it would also have been correct ?

Yes.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: for loops in collections