ProgrammingVery hardTCS NQT · Infosys · Capgemini
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer a[51] = all zeros (indices 0 to 50)
For i = 1 to 50
For j = i to 50 step i
a[j] = 1 - a[j]
End For
End For
Integer c = 0
For k = 1 to 50
c = c + a[k]
End For
Print c- A. 7
- B. 10
- C. 43
- D. 25
Correct answer: A. 7
Cell k is toggled once for every divisor of k, so it ends at 1 exactly when k has an odd number of divisors, i.e. when k is a perfect square. Squares up to 50: 1, 4, 9, 16, 25, 36, 49, which makes 7.
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? (ord('A') = 65; the letters are uppercase and the mod keeps every shift inside the…
- What will be the output of the following pseudocode? (Arrays are 0-indexed.) Integer a[7] = {5, 1, 4, 2, 8, 0, 3} Integer swaps = 0…
- What will be the output of the following pseudocode? (Static scoping: a name used inside a procedure refers to the variable visible where…
- What will be the output of the following pseudocode? (Arrays are 0-indexed.) Integer a[7] = {1, 2, 4, 5, 7, 9, 10} Integer i = 0, j = 6, c…
- What will be the output of the following pseudocode? (Division is integer division; arrays are 0-indexed.) Integer a[9] = {2, 3, 3, 3, 5…
- 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…