ProgrammingHardTCS Digital · Infosys SP
What will be the output of the following pseudocode? (All variables are integers, so / is integer division)
What will be the output of the following pseudocode? (All variables are integers, so / is integer division)
Integer c = 0, i = 1, j
while (i < 1000)
j = i
while (j > 0)
c = c + 1
j = j / 2
end while
i = i * 3
end while
Print c- A. 35
- B. 37
- C. 28
- D. 42
Correct answer: B. 37
i runs through 1, 3, 9, 27, 81, 243, 729. The inner loop runs once per binary digit of i: 1, 2, 4, 5, 7, 8, 10 times. Total c = 37.
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? (stop ends the program; code(c) gives a letter's character code) String s =…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[8] = {1, 1, 2, 2, 2, 3, 5, 5} Integer k = 1 for…
- What will be the output of the following pseudocode? (strings are indexed from 0) String x = "AGGTAB", y = "GXTXAYB" Integer L[0..6][0..7]…
- What will be the output of the following pseudocode? Integer wt[3] = {2, 3, 4}, val[3] = {3, 5, 6} Integer best[0..6] = all 0 for (each i…
- What will be the output of the following pseudocode? Integer lcm = 18, steps = 0 Integer a = 12, b = 18 while (lcm mod a != 0 OR lcm mod b…
- What will be the output of the following pseudocode? Integer total = 0 Procedure p(Integer n) if (n <= 0) return end if total = total + n…