ProgrammingVery hardGoldman Sachs OA · Amazon OA
What will be the output of the following pseudocode? (All variables are integers, so / is integer division; & is AND, ^ is XOR, | is OR, >> is right shift; −x is two's complement)
What will be the output of the following pseudocode? (All variables are integers, so / is integer division; & is AND, ^ is XOR, | is OR, >> is right shift; −x is two's complement)
Integer x = 156 Integer small = x & (-x) Integer ripple = x + small Integer ones = x ^ ripple ones = (ones >> 2) / small Print ripple | ones
- A. 157
- B. 184
- C. 172
- D. 163
Correct answer: D. 163
This is the classic 'next larger number with the same number of 1-bits'. 156 = 10011100: small = 4, ripple = 160 = 10100000, and the leftover 1's (ones = 3) are put back at the right: 10100011 = 163.
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 n = 3524, steps = 0 while (n != 6174) Integer big = digits of n sorted in…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer total = 0, n for (each…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division; + joins strings) Function…
- What will be the output of the following pseudocode? Integer m[3][3] = {{1, 2, 3}, {2, 5, 9}, {4, 6, 0}} Integer bad = 0 for (each i from…
- What will be the output of the following pseudocode? (operands are evaluated strictly left to right) Integer a = 7, b = 3 a = a + b - (b =…
- What will be the output of the following pseudocode? Integer calls = 0 Function C(Integer n, Integer k) calls = calls + 1 if (k == 0 OR k…