ProgrammingVery hardProduct company OA
What will be the output of the following pseudocode? (/ is integer division; the division in the loop is always exact.)
What will be the output of the following pseudocode? (/ is integer division; the division in the loop is always exact.)
Integer n = 10, c = 1, odd = 0
for (k = 0 to n)
if (c mod 2 == 1)
odd = odd + 1
end if
c = c * (n - k) / (k + 1)
end for
Print odd- A. 6
- B. 11
- C. 2
- D. 4
Correct answer: D. 4
c runs through row 10 of Pascal's triangle: 1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1. The odd entries are 1, 45, 45, 1 → 4. Lucas' theorem predicts this: 10 = 1010₂ has two 1-bits, so the row has 2² = 4 odd entries. '11' counts every entry.
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? ('ref' parameters are passed by reference; ^ is bitwise XOR; arrays are indexed from…
- What will be the output of the following pseudocode? (Arrays are passed by reference and indexed from 0.) Procedure p(Integer arr[]…
- What will be the output of the following pseudocode? (/ is integer division.) Integer c = 0, i = 1, j while (i < 1000) j = i while (j > 0)…
- What will be the output of the following pseudocode? (/ is integer division.) Integer c = 0 for (n = 1 to 200) Integer s = 0, t = n while…
- What will be the output of the following pseudocode? (isComposite[] starts all false; ops counts every assignment inside the inner loop…
- What will be the output of the following pseudocode? (&& stops as soon as the left side is false) Integer c = 0, k = 0 Function f(Integer…