ProgrammingVery hardGoldman Sachs OA · TCS Digital
What will be the output of the following pseudocode? (All variables are integers, so / is integer division)
What will be the output of the following pseudocode? (All variables are integers, so / is integer division)
Integer n = 7, c = 1
for (each k from 0 to n - 1)
Print c
c = c / (k + 1) * (n - k)
end for
Print c- A. 1 7 21 35 35 21 7 1
- B. 1 7 18 30 28 15 4 0
- C. 1 7 18 30 28 15 4 1
- D. 1 7 21 35 35 21 7 0
Correct answer: B. 1 7 18 30 28 15 4 0
Dividing before multiplying truncates: 7 / 2 = 3 → 3 × 6 = 18 instead of 21, and the error snowballs: 18 / 3 × 5 = 30, 30 / 4 × 4 = 28, 28 / 5 × 3 = 15, 15 / 6 × 2 = 4, 4 / 7 × 1 = 0. The correct row 1 7 21 35 35 21 7 1 needs c × (n − k) / (k + 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 count = 0 String last = "" Procedure gen(String s, Integer n) if (length(s)…
- What will be the output of the following pseudocode? Integer p[4] = {2, 0, 3, 1} // coefficients of 2x³ + 0x² + 3x + 1 Integer x = 3, m =…
- What is the time complexity of the following pseudocode, as a function of n? Procedure f(Integer n) if (n <= 1) return end if for (each i…
- What is the time complexity of the following pseudocode, as a function of n? for (i = 1; i i <= n; i = i + 1) for (j = 1; j <= n; j = j 2)…
- What is the time complexity of the following pseudocode, as a function of n? Function f(Integer n) if (n <= 1) return 1 end if return f(n…
- What will be the output of the following pseudocode? (OR stops evaluating once the left side is true; ++c adds 1 to c and then gives the…