ProgrammingVery hardTCS NQT · Infosys · Capgemini
What will be the output of the following pseudocode? (& is bitwise AND; arrays are 0-indexed.)
What will be the output of the following pseudocode? (& is bitwise AND; arrays are 0-indexed.)
Integer m[4][4]
For i = 0 to 3
For j = 0 to 3
m[i][j] = (i * 3 + j * 2) mod 5
End For
End For
Integer s = 0
For i = 0 to 3
For j = 0 to 3
If (i & j) = 0 Then s = s + m[i][j]
End For
End For
Print s- A. 0
- B. 10
- C. 13
- D. 20
Correct answer: D. 20
The cells with i AND j = 0 are all of row 0, plus (1,0), (1,2), (2,0), (2,1) and (3,0): nine cells whose values 0, 2, 4, 1, 3, 2, 1, 3, 4 add up to 20.
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? Function J(n, k) If n = 1 Then Return 0 Return (J(n - 1, k) + k) mod n End Function…
- What will be the output of the following pseudocode? Integer n = 1, s = 0 Repeat s = s + n n = n 2 + 1 Until s >= 100 Print n, s
- What will be the output of the following pseudocode? Integer s = 0 for (each i from 1 to 5) s = s + i i end for Print s
- What will be the output of the following pseudocode? Integer i = 10 while (i > 2) i = i - 3 end while Print i
- What will be the output of the following pseudocode? Integer n = 4096, c = 0 while (n > 0) n = n / 10 c = c + 1 end while Print c
- What will be the output of the following pseudocode? Integer p = 29, q = 6 Print (p mod q) + (p / q)