• 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

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
  • 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
  • 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
  • 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...
 
Not so fast naughty spawn! I want you to know about
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic