Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sangeetha Revanna
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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.
 
John Freeman
Greenhorn
Posts: 19
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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);
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Ya now I got the best explanation.Thank you so much Roel.. It was very useful...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic