ProgrammingHardAccenture · Cognizant pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer c = 0
for (i = 1; i < 64; i = i * 2)
for (j = i; j < 64; j = j + i)
c = c + 1
end for
end for
Print c- A. 120
- B. 127
- C. 63
- D. 126
Correct answer: A. 120
i takes the values 1, 2, 4, 8, 16, 32. For each, j runs over the multiples of i below 64, which is (64/i) - 1 values: 63 + 31 + 15 + 7 + 3 + 1 = 120. Allowing j up to 64 gives 64/i values and the trap 126.
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? (& is bitwise AND, | is bitwise OR, ^ is bitwise XOR, << and >> are left and right…
- What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR, ^ is bitwise XOR, << and >> are left and right…
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer arr[4] = {2, 3, 1, 0} for (each i from 0 to 3)…
- What will be the output of the following pseudocode? Function f(Integer n) if (n > 100) return n - 10 end if return f(f(n + 11)) End…
- What will be the output of the following pseudocode? Function A(Integer m, Integer n) if (m == 0) return n + 1 end if if (n == 0) return…
- What will be the output of the following pseudocode? Integer s = 0 for (each i from 1 to 4) for (each j from 1 to 4) if ((i j) mod 3 == 0)…