Java puzzle 04: Switch statement

  Quizzes     Assignments     Puzzles     Easy exercises     Required knowledge  
< Java Puzzles   Green = Easy,  Blue = Normal,  Red = Hard
Select    01   02   03   04   05   06   07   08   09   10   11   12   13   14   15 
 16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   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;
      }
    }
  }
}

 





Sar Maroof is a professional software development teacher, gives master classes and publishes technical articles. He is also an expert software developer and worked for several big as well as small companies and later as a freelancer.

 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 *