Multiple choice

int x = 5; int y = 2; char op = ''; switch (op) { default : x += 1; case '+' : x += y; /*It will go to all the cases/ case '-' : x -= y; } After the sample code above is executed, what will be the value of variable x?

  1. 4

  2. 5

  3. 6

  4. 7

  5. 8

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

x starts at 5. Since op='*' doesn't match any case, switch goes to default: x becomes 6. Without break, execution falls through to case '+': x += 2 makes x=8, then case '-': x -= 2 makes x=6. Fallthrough continues through all cases after the matching one.