ProgrammingHardTCS Digital · Infosys 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 to 5)
for (j = i to 5)
if ((i + j) mod 3 == 0)
c = c + 1
end if
end for
end for
Print c- A. 8
- B. 4
- C. 9
- D. 5
Correct answer: D. 5
Pairs with i ≤ j and i + j divisible by 3: (1,2), (1,5), (2,4), (3,3), (4,5) → 5. Letting j start at 1 counts ordered pairs: 8 (the 4 unequal pairs twice, plus (3,3)); missing (3,3) gives 4.
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 integer division; calls counts every call of f.) Integer calls = 0 Function…
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer a[5] = {1, 2, 3, 4, 5}, t for (i = 0 to 4) t =…
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer a[5] = {5, 1, 8, 2, 3}, c = 0 for (i = 0 to 3)…
- What will be the output of the following pseudocode? (A static variable is initialised once and keeps its value between calls; operands…
- What will be the output of the following pseudocode? (^ is bitwise XOR, & is AND, >> is a right shift.) Integer x = 29, y = 30 Integer gx…
- What will be the output of the following pseudocode? (Arrays are indexed from 0; the matrix has 3 rows and 4 columns.) Integer m[3][4] =…