ProgrammingVery hardProduct company OA · code-tracing level
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 <= 100; i = i * 2)
for (j = i; j <= 100; j = j + 1)
c = c + 1
end for
end for
Print c- A. 707
- B. 127
- C. 580
- D. 600
Correct answer: C. 580
i takes the values 1, 2, 4, 8, 16, 32, 64 (128 > 100), seven values. For each, the inner loop runs 101 − i times. Total = 7 × 101 − (1 + 2 + … + 64) = 707 − 127 = 580. 707 forgets to subtract the starting values; 127 sums the i values themselves.
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? // 32-bit signed integers; overflow wraps around (two's complement); / truncates…
- What is the time complexity of the following pseudocode, as a function of n? for (i = n; i > 0; i = i / 2) for (j = 0; j < i; j = j + 1)…
- What is the time complexity of the following pseudocode, as a function of n? for (i = 1; i <= n; i = i + 1) j = 1 while (j < i) j = j 2…
- What is the time complexity of the following pseudocode, as a function of n? i = 2 while (i <= n) i = i i end while
- 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 will be the output of the following pseudocode? (Arrays are indexed from 0; / is integer division; 'from … to …' includes both ends.)…