ProgrammingHardAmazon OA · TCS Digital
What will be the output of the following pseudocode? (^ is XOR, & is AND)
What will be the output of the following pseudocode? (^ is XOR, & is AND)
Integer a = 10, b = 20, x, flips = 0
x = a ^ b
while (x > 0)
x = x & (x - 1)
flips = flips + 1
end while
Print a ^ b, flips- A. 30 2
- B. 10 4
- C. 30 4
- D. 30 5
Correct answer: C. 30 4
10 = 01010 and 20 = 10100 differ in four places: a ^ b = 11110 = 30, and clearing its 1-bits one by one takes 4 steps. 4 bit flips turn 10 into 20: 30 4.
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? (All variables are integers, so / is integer division) Function f(Integer n) if (n <=…
- What will be the output of the following pseudocode? Integer calls = 0 Procedure f(Integer n) calls = calls + 1 for (each i from 1 to n…
- What will be the output of the following pseudocode? Integer t[0..10] t[0] = 0 t[1] = 1 t[2] = 1 for (each i from 3 to 10) t[i] = t[i - 1]…
- What will be the output of the following pseudocode? (strings are indexed from 0) String s = "{[()]}([)]" Stack st = empty Integer bad =…
- What will be the output of the following pseudocode? Queue q = {1, 2, 3, 4, 5} Integer k = 3 Stack st = empty for (each i from 1 to k)…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) // numbers stored as digit…