Topics Index

Print quarters depending upon the month number

If you need explanation Read this topic

If you need Answer Take test on this topic

Print quarters depending upon the month number
What will be the output of the following program.
class Quarters
    public static void main(String s[])
        int month = 5;

            case 1:
            case 2:
            case 3:
                System.out.println("First Quarter");
            case 4:
            case 5:
            case 6:
                System.out.println("Second Quarter");
            case 7:
            case 8:
            case 9:
                System.out.println("Third Quarter");
            case 10:
            case 11:
            case 12:
                System.out.println("Fourth Quarter");

A. No Output
B. Compilation Error - since some case statements does not have any code.
C. Compilation Error - since some case statements does not have break
D. Second Quarter
E. Second Quarter
Third Quarter
Fourth Quarter
Topic: switch Statement In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Answer is option D.Because after case 5 it goes to case 6 since there is no break for case 5.

Posted by Sudha Snigdha    2014-11-20 12:16:41


Posted by Phan Sovan Narith    2014-11-20 12:21:19

Ans is D.. Here we have the concept of switch statement. The program execution starts from main function, The variable month is initialised to 5 and case 5 is considered and corresponding text will be printed if available, break statement isn't encountered so control goes to next case till next break encounters. so "Second Quarter" will be printed. Then break statement encounters the control come out of switch function and terminate the program.

Posted by Sai Ram    2014-11-20 12:27:41

Bcoz mont is 5 and case 5 will print second qater
Since no break is after 5 fall through prints second qtr

Posted by Rohit Hrthaker    2014-11-20 13:55:10

ans d.The switch statement is Java’s multiway branch statement. It provides an easy way to dispatch
execution to different parts of your code based on the value of an expression. As such, it often
provides a better alternative than a large series of if-else-if statements.The expression must be of type byte, short, int, or char; each of the values specified in the
case statements must be of a type compatible with the expression.The value of the expression is compared with each
of the literal values in the case statements. If a match is found, the code sequence following
that case statement is executed. If none of the constants matches the value of the expression,
then the default statement is executed. However, the default statement is optional. If no case
matches and no default is present, then no further action is taken.The break statement is used inside the switch to terminate a statement sequence. When
a break statement is encountered, execution branches to the first line of code that follows the
entire switch statement. This has the effect of “jumping out” of the switch.The break statement is optional. If you omit the break, execution will continue on into the
next case. It is sometimes desirable to have multiple cases without break statements between
them.                                                                                                                            coming to this program expression variable is assigned with int switch case finds correct match case,but that case dont have any statements,and also dont have break condition,soexecution falls through each case until a break statement,so here case 6 get executed sop prints Second Quarter thn swithcase control goes outside .

Posted by Maheshwari Natarajan    2014-11-20 17:34:10

This dose is now closed and the winners are Sai Ram,  for 'First Correct Comment', Maheshwari Natarajan,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Varun Kumar. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2014-11-21 02:16:37

© meritcampus 2019

All Rights Reserved.

Open In App