ProgrammingHardCapgemini · Accenture

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 n = 0, digitsA = 0, digitsB = 0
Integer x = n
while (x > 0)
    digitsA = digitsA + 1
    x = x / 10
end while
x = n
do
    digitsB = digitsB + 1
    x = x / 10
while (x > 0)
Print digitsA, digitsB
  1. A. 1 1
  2. B. 0 0
  3. C. 0 1
  4. D. 1 0

Correct answer: C. 0 1

For n = 0 the while loop never runs, so digitsA = 0, but the do-while body runs once before testing, giving digitsB = 1. Output 0 1: the do-while version is the correct digit count for 0.

More Pseudocode and Code Tracing questions

Exams that ask this topic