ProgrammingHardGoldman Sachs OA · Infosys SP
What will be the output of the following pseudocode? (^ is XOR, >> is right shift)
What will be the output of the following pseudocode? (^ is XOR, >> is right shift)
Integer s = 0, g
for (each i from 0 to 7)
g = i ^ (i >> 1)
s = s + g
if (i == 5)
Print g
end if
end for
Print s- A. 5 28
- B. 6 28
- C. 7 36
- D. 7 28
Correct answer: D. 7 28
i ^ (i >> 1) is the Gray code of i: 0, 1, 3, 2, 6, 7, 5, 4. For i = 5 it is 101 ^ 010 = 111 = 7. The 8 codes are a rearrangement of 0 to 7, so s = 28: 7 28.
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? Function possible(Integer out[5]) Stack st = empty Integer next = 1 for (each i from…
- What will be the output of the following pseudocode? Integer coins[4] = {9, 6, 5, 1} // sorted largest first Integer amt = 11, used = 0…
- What will be the output of the following pseudocode? Integer a[8] = {10, 9, 2, 5, 3, 7, 101, 18} Integer L[8], best = 0 for (each i from 0…
- What will be the output of the following pseudocode? (strings are indexed from 0) String x = "SUNDAY", y = "SATURDAY" Integer…
- What will be the output of the following pseudocode? Integer a[6] = {5, 5, 10, 100, 10, 5} Integer incl = 0, excl = 0, t for (each i from…
- An array of n elements is ALREADY sorted in increasing order. What is the time complexity of this pseudocode on it? for (each i from 1 to…