Question discussion

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…

288870Medium 1 reply 1,756 views
29
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; } }}
  • A1
  • B2
  • C3
  • D4
  • E0
Correct answer: B

Discussion 1

0
CCommunity
2

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.