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- A. 2
- B. 5
- C. 15
- 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.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode? Integer i = 1, c = 0 while (i <= 100) i = i 2 + c c = c + 1 end while Print c, i
- What will be the output of the following pseudocode? // list: 1 -> 2 -> 3 -> 4 -> 5 -> NULL, head points to node 1 Node prev = NULL, cur =…
- What will be the output of the following pseudocode? (a loop whose start is greater than its end runs zero times) Integer count = 0…
- What will be the output of the following pseudocode? Integer stars = 0 Procedure p(Integer n) stars = stars + 1 for (each i from 1 to n)…
- What will be the output of the following pseudocode? (& is bitwise AND on two's-complement integers) Integer n = 360 while (n > 0) Print n…
- What will be the output of the following pseudocode? (substring(s, i, k) is the k characters starting at index i; indices start at 0)…