Java puzzle 04: Switch statement

<< Java Puzzles   Green = Easy,  Blue = Normal,  Red = Hard
 P01   P02   P03   P04   P05   P06   P07   P08   P09   P10   P11   P12   P13   P14   P15   P16   P17   P18   P19   P20   P21   P22   P23   P24 
 By title 
>>

If you are or want to become a good programmer in the future and you don’t understand this puzzle series, you got to do something about it.
Every advanced programmer can see most of the results of this series. The third one was very hard, I understand if you ignored it. Programming is writing code not memorizing theories. No employer is intersted in theories, but they ask you to build new features, build a new application or web application. So please, spend some time to understand this puzzle!

This small program is a combination of a switch and a loop.
If the program is compiled and run, it writes “eabcdebcdecdede” to the standard output.
add only one statement to the line 21, as a result of that the program writes “ede” to
the standard output. What is that statement?

Java puzzle 4: A simple switch statement

Level: Normal

public class MyClass
{
  public static void main(String[] args)
  {
    int n = 0;
    while (n < 5)
    {
      n++;
      switch (n)
      {
        case 2:
          System.out.print("a");
        case 3:
          System.out.print("b");
        case 4:
          System.out.print("c");
        case 5:
          System.out.print("d");
        default:
          System.out.print("e");
          // add your statement here
      }
    }
  }
}
                                                                    Author: Sar Maroof

Answer explanation

  1. The condition while n < 5 return true as long as n is smaller than 5.
  2. The initial value of n is 0. Therefore the above mentioned condition returns true.
  3. The statement n++; increments the value of n by one. So, the value of n = 0+1 = 1;
  4. There is no case 1, that is why the statement under the default statement System.out.print(“e”); is executed  and writes e to the standard output.
  5. The statement n+=3; increments the value of n by 3. That means that n = 1 + 3 = 4.
  6. The condition of the loop returns true, because n is still smaller than 5.
  7. The body of the loop is executed and n++; increments the value of n by on. n = 4 + 1 = 5
  8. The statement of the case 5 System.out.print(“d”); writes d to the standard output.
  9. There is no break; statement under case 5. Therefore the statements under the default
    are also executed. The statement System.out.print(“e”); writes e to the standard output.
    The statement n+=3; increments the value of n by 3. So, n = 5 + 3 = 8.
  10. The condition of the loop returns false because n is now greater than 5.
    The loop is terminated.The program writes: “ede” to the standard output.

The answer is: n += 3;

 

public class MyClass
{
  public static void main(String[] args)
  {
    int n = 0;
    while (n &lt; 5)
    {
      n++;
      switch (n)
      {
        case 2:
          System.out.print("a");
        case 3:
          System.out.print("b");
        case 4:
          System.out.print("c");
        case 5:
          System.out.print("d");
        default:
          System.out.print("e");
          n += 3;
      }
    }
  }
}

 

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