ProgrammingHardGoldman Sachs OA · Amazon OA
What will be the output of the following pseudocode? (& is bitwise AND on 32-bit integers)
What will be the output of the following pseudocode? (& is bitwise AND on 32-bit integers)
Function pow4(Integer n)
return n > 0 AND (n & (n - 1)) == 0 AND (n & 1431655765) != 0
End Function
Print pow4(16), pow4(32), pow4(1)- A. true true false
- B. false false true
- C. true false true
- D. true false false
Correct answer: C. true false true
n & (n − 1) == 0 means n is a power of 2, and 1431655765 = 0101…0101 in binary keeps only the even bit positions, which is where powers of 4 have their 1-bit. 16 = 4², 32 = 2⁵ (odd position) fails, 1 = 4⁰ passes: true false true.
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 AND, ^ is XOR, << is left shift) Integer x = 7, m = 1 while ((x & m) != 0) x =…
- What will be the output of the following pseudocode? Integer printed = 0 Procedure perm(String s, Integer l) // s is passed by value if (l…
- What will be the output of the following pseudocode? Integer t[4][4] = {{2}, {3, 4}, {6, 5, 7}, {4, 1, 8, 3}} // row r has r + 1 numbers…
- What will be the output of the following pseudocode? Integer a = 20, b = 6, c = 0 while (a > b) a = a - 3 b = b + 2 c = c + 1 end while…
- What will be the output of the following pseudocode? Integer r[3][4] = {{1, 2, 3, 4}, {2, 4, 6, 8}, {4, 2, 5, 1}} Integer c = 0 for (each…
- What will be the output of the following pseudocode? // list: 10 -> 20 -> 30 -> 40 -> 50 -> NULL Node fast = head, slow = head Integer k =…