ProgrammingHardCapgemini · Infosys pattern
What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR, ^ is bitwise XOR, << and >> are left and right shifts.)
What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR, ^ is bitwise XOR, << and >> are left and right shifts.)
Integer n = 6, s = 0
for (each i from 1 to n)
s = s + (i & 1) * i - (1 - (i & 1)) * i
end for
Print s- A. 21
- B. 3
- C. 9
- D. -3
Correct answer: D. -3
i & 1 is 1 for odd i and 0 for even i, so odd terms are added and even terms subtracted: 1 - 2 + 3 - 4 + 5 - 6 = -3.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- 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 <…
- 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…