ProgrammingHardTCS NQT · Accenture pattern
What will be the output of the following pseudocode? (Arrays are indexed from 0.)
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}}
Integer s = 0
for (each i from 0 to 2)
for (each j from 0 to 2)
if (i + j == 2 || i == j)
s = s + m[i][j]
end if
end for
end for
Print s- A. 15
- B. 25
- C. 20
- D. 30
Correct answer: B. 25
Main diagonal 1 + 5 + 9 = 15 and anti-diagonal 3 + 5 + 7 = 15. The centre 5 meets both conditions but is added only once, because it is a single if with ||. 30 - 5 = 25.
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? (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 <…
- How many lines does this pseudocode print for hanoi(5, 'A', 'B', 'C')? Function hanoi(Integer n, Char from, Char via, Char to) if (n == 0)…
- How many of the following pseudocode statements print 1? (/ is integer division; & is bitwise AND; ^ is bitwise XOR.) (i) Print (7 / 2) 2…
- What will be the output of the following pseudocode? Integer a = 12, b = 18, t while (b != 0) t = b b = a mod b a = t end while Print a
- What will be the output of the following pseudocode? Integer s = 0 for (i = 1 to 5) for (j = i to 5) s = s + 1 end for end for Print s