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

Doubt about Chapter 2 Question 9 on page 96 (Java OCA 8 Programmer I Study Guide, Sybex)

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In for loop initialization is done only once right? but how for every iteration 'i' will again assigned to zero?
and after the 4th line ''i" value got incremented from 5th line right?

How many times will the following code print "Hello World"?



a) 9
b) 10
c) 11
d) The code will not compile because of line 3.
e) The code will not compile because of line 5.
f) The code contains an infinite loop and does not terminate.

Boyarsky, Jeanne; Selikoff, Scott (2014-12-11). OCA: Oracle Certified Associate Java SE 8 Programmer I Study Guide: Exam 1Z0-808 (p. 96). Wiley. Kindle Edition.
 
Greenhorn
Posts: 21
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See what the output is when you add the following above line 4 inside the loop:

System.out.println("before increment: "+i);

and the following after line 4 and before line 5:

System.out.println("after increment: "+i);
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sangeetha Revanna wrote:In for loop initialization is done only once right? but how for every iteration 'i' will again assigned to zero?
and after the 4th line ''i" value got incremented from 5th line right?


In this topic you'll find an explanation for exactly the same question. So it's definitely worth reading! And in this topic you'll find an extensive discussion with lots of code examples. Definitely worth reading too!

Hope it helps!
Kind regards,
Roel
 
Sangeetha Revanna
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya now I got the best explanation.Thank you so much Roel.. It was very useful...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic