Vidyalelo
Java Programming · Q37

Flow Control

Programming · Java Programming · question 37

Q37

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
Answer
C.
3
D.
4
E.
0

Answer: Option B

Solution

Answer: Option B
Solution:
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.