Java quiz 15: Loop within loop

<< Java Quizzes   Green = Easy,  Blue = Normal,  Red = Hard
 Q01   Q02   Q03   Q04   Q05   Q06   Q07   Q08   Q09   Q10   Q11   Q12   Q13   Q14   Q15   Q16   Q17   Q18   Q19   Q20   Q21   Q22   Q23   Q24 
 By title 
>>

For many of the projects on which I worked, I needed to use loops within loops. In the beginning seems very hard, but if you understand the process you become gradually familiar with it.

Java quiz 15: Loop within loop

Level: Hard

What is the output of this code?

public class MyLoop
{
  public static void main(String[] args)
  {
    int y = 0;
    while(y < 14)
    {
      y++;
      for(int i = 1; i < 4; i++)
      {
        y += i;
      }
    }
    System.out.println(y);
  }
}
Author: Sar Maroof

Select the correct answer.


Answer explanation

  1. If y is smaller than 14 the body of the while loop is executed. The initial value of y is 0.
  2. When the body of the while loop is excuted the statement y++; increments the value of y by 1. y = 0 + 1 = 1.
  3. The second loop is a for loop. The initial value of i is 1 and the body of the loop is executed as long as y is smaller than 4. This loop is executed for the values of i 1, 2, 3. The statement y += i increments the value of y by i. So, y = 1 + 1 + 2 + 3 = 7.
  4. The last value of y is 7 and that is smaller than 14. The body of the while loop is executed for the second time and y ++; increments the value of y by 1, y = 7 + 1 = 8.
  5. The body of the for loop is executed and the statement y += i increments the value of y by i and those are 1, 2, 3. So, y = 8 + 1 + 2 + 3 = 14. The while loop is terminated because y is no longer smaller than 14.

The correct answer is: b.

If you found this difficult, you might consider reading my book Java quizmaster for beginners. It is easy to understand, organized to learn Java in 17 days and it guides you to master Java code by solving 105 quizzes and 117 assignments. This book explains each chapter properly before starting with exercises and assignments. It is available on Amazon. See inside the book here!

Please, leave your questions, feedback and suggestions in the comments below!
SarMaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.




Sar Maroof is graduated from HBO Amsterdam "higher professional education". He is a professional computer programming teacher, gives master classes and publishes technical articles. His experience with developing software started from 2001 and he has worked as a software developer for several big as well as small companies and later as a freelancer. He is the author of Java quizmaster and Build a Java application in 7 days.

Books by Sar Maroof

           

 Subscribe to Sar Maroof's RSS feeds!
 Mail this page to a friend!
 Bookmark this page!

Leave a Reply

Your email address will not be published. Required fields are marked *

Sarmaroof © 2017