ProgrammingHardTCS NQT · Accenture pattern
How many of the following pseudocode statements print 1? (/ is integer division; & is bitwise AND; ^ is bitwise XOR.) (i) Print (7 / 2) * 2 == 7 (ii) Print (6 & 3) == 7 (iii) Print (5 ^ 2) == 25 (iv) Print (-8 >> 1) == -4, assuming an arithmetic right shift
- A. Only three
- B. All four
- C. Only one
- D. Only two
Correct answer: C. Only one
Only (iv) prints 1: an arithmetic shift keeps the sign, so -8 >> 1 = -4. (i) prints 0 because 7 / 2 = 3 and 3 × 2 = 6. (ii) prints 0 because 110 & 011 = 010 = 2 (7 would be OR). (iii) prints 0 because ^ is XOR: 101 ^ 010 = 111 = 7, not 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? 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
- What will be the output of the following pseudocode? Integer n = 1234, r = 0 while (n > 0) r = r 10 + (n mod 10) n = n / 10 // integer…
- What will be the output of the following pseudocode? Integer x = 5, y = 9 x = x ^ y // ^ is bitwise XOR y = x ^ y x = x ^ y Print x, y, (5…
- What will be the output of the following pseudocode? Integer Function f(Integer n) if (n <= 0) return 0 return (n mod 10) + f(n / 10) //…
- What will be the output of the following pseudocode? Integer c = 0 for (i = 1 to 100) if ((i mod 3 == 0) OR (i mod 5 == 0)) if (NOT (i mod…