ProgrammingHardTCS NQT · Accenture pattern
What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR, ^ is bitwise XOR, << and >> are left and right shifts.) ~ is bitwise NOT in two's complement.
What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR, ^ is bitwise XOR, << and >> are left and right shifts.) ~ is bitwise NOT in two's complement.
Integer a = 40 Integer b = a & (~a + 1) Integer c = a ^ (a - 1) Print b, c
- A. 40 7
- B. 32 15
- C. 8 15
- D. 8 1
Correct answer: C. 8 15
In two's complement, ~a + 1 = -a, and a & -a keeps only the lowest set bit: 40 = 101000, so b = 8. a ^ (a - 1) = 101000 ^ 100111 = 001111 = 15, which is that bit and all the bits below it.
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? (Arrays are indexed from 0.) Integer arr[4] = {2, 3, 1, 0} for (each i from 0 to 3)…
- What will be the output of the following pseudocode? Function f(Integer n) if (n > 100) return n - 10 end if return f(f(n + 11)) End…
- What will be the output of the following pseudocode? Function A(Integer m, Integer n) if (m == 0) return n + 1 end if if (n == 0) return…
- What will be the output of the following pseudocode? Integer s = 0 for (each i from 1 to 4) for (each j from 1 to 4) if ((i j) mod 3 == 0)…
- What will be the output? (A do-while loop runs its body once before checking the condition; / is integer division.) Integer x = 0, c = 0…
- What will be the output of the following pseudocode? Integer p = 1 for (each i from 1 to 10) p = (p 3) mod 7 end for Print p