129k views
1 vote
In the given switch statement, what will be displayed if the value of var is 3? switch(var) { case 1: System.out.println("Apple"); break; case 2: System.out.println("Banana"); break; case 3: System.out.println("Peach"); case 4: System.out.println("Pear"); case 5: System.out.println("Pineapple"); break; default : System.out.println("Cantaloupe"); break; } A. Peach Peach B. Pear Peach C. Pear Pineapple D. None of the above.

User Maohieng
by
5.4k points

1 Answer

7 votes

Answer:

The output of the given program is

Peach Pear Pineapple.

Explanation:

since the value of var is so control will move to the case 3 directly and executed the statement inside the case 3 so it will print "Peach" in window after that their is no break statement in the case 3 the control moves the case 4 and executed the statement inside the case 4 so it will print "Pear" in window again there is no break statement in the case 4 the control moves the case 5 and executed the statement inside the case 5 so it will print "Pineapple".Finally, the break statement is reached the execution of the program is stopped.

User Mahouk
by
5.8k points