ProgrammingModerateGoldman Sachs OA · Infosys
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer a = 29, b = 15, x, c = 0
x = a ^ b
while (x > 0)
c = c + (x & 1)
x = x >> 1
end while
Print c- A. 3
- B. 2
- C. 4
- D. 1
Correct answer: B. 2
c counts the bits where a and b differ. 29 = 11101 and 15 = 01111, so a ^ b = 10010, which has two 1's: c = 2.
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? (integers are two's complement; ~ is bitwise NOT; >> is an arithmetic shift that…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer n = 19, steps = 0, s…
- What will be the output of the following pseudocode? Integer u[1..10], i2 = 1, i3 = 1, i5 = 1, nx u[1] = 1 for (each k from 2 to 10) nx =…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer n = 50, lo = 0, hi =…
- What will be the output of the following pseudocode? (operands are evaluated left to right; arrays are indexed from 0) Integer q[5], front…
- What will be the output of the following pseudocode? String in = "A+BC-D/E", out = "" Stack st = empty // prec: and / are 2, + and - are 1…