ProgrammingHardAccenture · Cognizant pattern
How many times is count incremented?
How many times is count incremented?
Integer n = 4, count = 0
for (each i from 1 to n)
for (each j from 1 to i * i)
if (j mod i == 0)
for (each k from 1 to j)
count = count + 1
end for
end if
end for
end for
Print count- A. 30
- B. 65
- C. 40
- D. 100
Correct answer: B. 65
For each i the innermost loop runs only when j = i, 2i, ..., i·i, adding j each time: i × (1 + 2 + ... + i) = i²(i + 1)/2. That gives 1 + 6 + 18 + 40 = 65.
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? (&& does not evaluate its right side when the left side is false.) Integer c = 0, s = 0 Function inc() c = c + 1…
- What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR, ^ is bitwise XOR, << and >> are left and right…
- Two sorted arrays a = {1, 4, 7} and b = {2, 3, 8, 9} are merged. How many element comparisons does the pseudocode make? Integer i = 0, j =…
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Integer c = 0, i = 16 while…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Function f(Integer n) if (n <…