ProgrammingVery hardTCS NQT · Infosys · Capgemini
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Function J(n, k)
If n = 1 Then Return 0
Return (J(n - 1, k) + k) mod n
End Function
Print J(13, 4) + 1- A. 4
- B. 13
- C. 9
- D. 5
Correct answer: D. 5
J(n,k) is the 0-based position of the survivor when every k-th person is removed from a circle of n. Unwinding J(1) = 0, J(2) = 0 + 4 mod 2, and so on up to n = 13 gives J(13,4) = 4, so the survivor is person 5 in 1-based numbering (the printed value adds 1).
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 n = 1, s = 0 Repeat s = s + n n = n 2 + 1 Until s >= 100 Print n, s
- What will be the output of the following pseudocode? Integer s = 0 for (each i from 1 to 5) s = s + i i end for Print s
- What will be the output of the following pseudocode? Integer i = 10 while (i > 2) i = i - 3 end while Print i
- What will be the output of the following pseudocode? Integer n = 4096, c = 0 while (n > 0) n = n / 10 c = c + 1 end while Print c
- What will be the output of the following pseudocode? Integer p = 29, q = 6 Print (p mod q) + (p / q)
- What will be the output of the following pseudocode? Integer n = 3140, r = 0 while (n > 0) r = r 10 + n mod 10 n = n / 10 end while Print r