ProgrammingHardAccenture · Cognizant GenC Next
What will be the output of the following pseudocode? (Strings are indexed from 0)
What will be the output of the following pseudocode? (Strings are indexed from 0)
String s = "110100"
Integer v = 0, p = 1
for (each i from 0 to length(s) - 1)
if (s[i] == '1')
v = v + p
end if
p = p * 2
end for
Print v- A. 52
- B. 13
- C. 11
- D. 44
Correct answer: C. 11
The weight p doubles while moving left to right, so the string is read with its first character as the units bit: 1 + 2 + 8 = 11. Reading it as ordinary binary gives 52.
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? Boolean mark[2..30] = all false Integer ops = 0, j for (each p from 2 to 30) if (p p…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division; arrays are indexed from 0)…
- What will be the output of the following pseudocode? Stack s1 = empty, s2 = empty Procedure enqueue(Integer x) push(s1, x) End Procedure…
- What will be the output of the following pseudocode? (^ is bitwise XOR) Integer s = 0 for (each i from 0 to 15) s = s + (i ^ 5) end for…
- What will be the output of the following pseudocode? (parameters are passed by value; inside g, x is the global x; operands are evaluated…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division; + joins strings) Function…