ProgrammingModerateCapgemini · Cognizant GenC Next

What will be the output of the following pseudocode? (C-style switch: a case without break falls through to the next one)

What will be the output of the following pseudocode? (C-style switch: a case without break falls through to the next one)
Integer x = 2, s = 0
switch (x)
    case 1: s = s + 1
    case 2: s = s + 2
    case 3: s = s + 3
            break
    case 4: s = s + 4
    default: s = s + 10
end switch
Print s
  1. A. 2
  2. B. 5
  3. C. 15
  4. D. 19

Correct answer: B. 5

Without a break, case 2 falls through into case 3: s = 2 + 3 = 5, and the break after case 3 stops it before case 4 and default.

More Pseudocode and Code Tracing questions

Exams that ask this topic