QUESTION IMAGE
Question
what is y after the following switch statement is executed?
int x = 3; int y = 4;
switch (x + 3) {
case 6: y = 0;
case 7: y = 1;
default: y += 1;
}
a 2
b 1
c 4
d 3
e 0
Step1: Calculate the value of \(x + 3\)
Given \(x = 3\), then \(x+3=3 + 3=6\).
Step2: Execute the switch - case statement
When the switch expression \(x + 3 = 6\), it will execute the code for case 6: y = 0;. But in a switch statement without break statements, after executing case 6, it will fall through to the next case (if there is no break). So it will also execute case 7: y = 1; (because there is no break after case 6). Then it will execute the default case. Since \(y = 1\) (after case 7), and then \(y+=1\) (equivalent to \(y=y + 1\)). So \(y=1+1=2\).
Snap & solve any problem in the app
Get step-by-step solutions on Sovi AI
Photo-based solutions with guided steps
Explore more problems and detailed explanations
A. 2