Examveda
Examveda

What will be the value of y after execution of switch statement?
public class Test{
        public static void main(String[] args){
                int x = 3, y = 4;
                switch(x + 3){
                        case 6: y = 0;
                        case 7: y = 1;
                        default: y += 1;
                }
        }
}

A. 1

B. 2

C. 3

D. 4

E. 0

Answer: Option B

Solution(By Examveda Team)

Initially 'x' = 3 and y =4
when switch statement execute switch(x + 3) case 6 will be executed and initialize y = 0 and after that case 7 will be executed (because there is no break statement so it will execute all the case) and initialize y = 1 and after that default case execute which add 1 in y and y become 2.

This Question Belongs to Java Program >> Flow Control

Join The Discussion

Comments ( 1 )

  1. Ashwin Prasad
    Ashwin Prasad :
    6 years ago

    Can somebody please give an explanation for this?

Related Questions on Flow Control