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 for loops in collections 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 "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: 14111
    
  16

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 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: for loops in collections