ProgrammingHardAmazon OA · Infosys SP
What will be the output of the following pseudocode? (^ is XOR, & is AND, >> is right shift)
What will be the output of the following pseudocode? (^ is XOR, & is AND, >> is right shift)
Integer x = 21, z = 22 Integer y = x ^ (x >> 1) Integer w = z ^ (z >> 1) Print y, (y & (y + 1)) == 0, (w & (w + 1)) == 0
- A. 31 false true
- B. 31 true false
- C. 15 true true
- D. 29 true true
Correct answer: B. 31 true false
If the bits of x alternate (21 = 10101), x ^ (x >> 1) is all 1's (11111 = 31), and then y & (y + 1) = 0. For 22 = 10110 the bits do not alternate: w = 10110 ^ 01011 = 11101 = 29, and 29 & 30 ≠ 0. Output 31 true false.
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? (code(c) is the ASCII code: 'A' = 65, 'a' = 97) Function cmp(String a, String b)…
- What will be the output of the following pseudocode? Integer a[8] = {3, 1, 4, 1, 5, 9, 2, 6} Integer best = a[0], c = 1 for (each i from 1…
- What will be the output of the following pseudocode? Integer a[6] = {100, 4, 200, 1, 3, 2} Set s = all values of a Integer best = 0, len…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer m[4][4] = {{1, 4, 7, 11}, {2, 5, 8, 12}, {3, 6…
- What will be the output of the following pseudocode? Integer a[7] = {4, 5, 2, 10, 8, 6, 9} Stack st = empty for (each i from 0 to 6) while…
- What will be the output of the following pseudocode? (+ joins strings) Queue q = {"1"} Integer k = 0 String s while (k < 6) s = dequeue(q)…