Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Collection code test

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will the program print when it is compiled and run?
ithis snippet of code of SCJP book :

select the one correct answer
Select the one correct answer.
(a) A collection coming up.
60
(b) A collection coming up.
A collection coming up.
A collection coming up.
60
(c) The program does not compile.
(d) None of the above.
i compile and execute this code the correct answer is (a) but i don't understand why it print once  A collection coming up.
 
Master Rancher
Posts: 3322
31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hm, when I compile and run that, I get:

A collection coming up.
60

which seems to make a lot more sense.  Is it possible something has been copied incorrectly?
 
emma roberts
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i get:
A collection coming up.
60
but i don't undrestand why 'Acollection coming up' is printed once
 
Mike Simmons
Master Rancher
Posts: 3322
31
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see - I misread your question.  Well, it's printed once because the method makeCollection() is called once, and that method has a print statement.

Perhaps you wonder why it's only printed once?  That's because this:



is equivalent to



Although it's in a for statement, the makeCollection() call is only executed once, to get the value of the thing we're looping over.  We don't need to keep calling that method every time we get a new value of i.  We've already got the whole list, in the first call.  Afterwards we're just looping through the different values in the one list, that was created once at the beginning of the loop.
 
Saloon Keeper
Posts: 3462
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be aware that in a classical for-loop the continuation condition is re-evaluated with every new iteration, including variables that may appear in such a continuation condition. For instance, can you explain the outcome of the following:


Edit: oops, forgot to say that you need to change the return type of makeCollection to List<Integer>, otherwise this code snippet will not compile! (why?)
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!