• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

for loops in collections

 
Rachel Glenn
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rachel Glenn wrote:BUT, if A was for(Object o: list), then it would also have been correct ?

Yes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic