ExamVeda
Login
Home
This question belongs to Java Program Flow Control
Flow Control
?

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;
                }
        }
}

Answer & Solution
Correct Answer: Option B
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.
Examveda
Question posted by Examveda
Community

Join the Discussion

1 Comment
Ashwin Prasad
Ashwin Prasad 8 years ago
Can somebody please give an explanation for this?