ProgrammingHardInfosys SP · Amazon OA
What will be the output of the following pseudocode? (| is bitwise OR)
What will be the output of the following pseudocode? (| is bitwise OR)
Integer x = 0, c = 0
while (x < 1000)
x = x | (x + 1)
c = c + 1
end while
Print c, x- A. 9 511
- B. 10 1024
- C. 10 1023
- D. 11 2047
Correct answer: C. 10 1023
x | (x + 1) sets the lowest 0 bit, so x runs 1, 3, 7, 15, …, 2^c − 1. The first such value ≥ 1000 is 1023 = 2^10 − 1, reached after 10 steps: 10 1023.
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[7] = {1, 2, 3, 4, 5, 6, 7} reverse(arr, 0, 2)…
- What will be the output of the following pseudocode? (the static s keeps its value between calls; in f(n - 1) + s the call is evaluated…
- What will be the output of the following pseudocode? Integer c = 0 for (each i from 1 to 6) for (each j from 1 to 6) if ((i j) mod 4 == 0)…
- What will be the output of the following pseudocode? (Strings are indexed from 0; + joins strings) String s = "AAABCCDAA", r = "" Integer…
- 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? (ref means the argument is passed by reference; other parameters are copies)…