ProgrammingHardTCS NQT · Accenture pattern
What will be the output of the following pseudocode (binary string to decimal)?
What will be the output of the following pseudocode (binary string to decimal)?
String bits = "110101"
Integer val = 0
for (each b in bits)
val = val * 2 + digitValue(b)
end for
Print val- A. 45
- B. 53
- C. 54
- D. 42
Correct answer: B. 53
Processing left to right, each bit doubles the accumulated value and adds the new bit: 1,3,6,13,26,53 -- matching 110101 binary = 53 decimal.
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 (finding the 1-based position of the rightmost set bit)? (All variables are integers…
- What will be the output of the following pseudocode (checking if s3 is an interleaving of s1 and s2, via dynamic programming)? Function…
- What will be the output of the following pseudocode (counting pairs (i,j) with i<=j<=n whose product is divisible by n)? Integer n = 12, c…
- What will be the output of the following pseudocode (checking whether all characters in a string are unique)? Function…
- What will be the output of the following pseudocode (Hamming distance: number of differing bits between two integers)? (^ is bitwise XOR…
- What will be the output of the following pseudocode (finding the minimum in a rotated sorted array via binary search)? (Arrays are indexed…