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- A. 1 1
- B. 0 0
- C. 0 1
- 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.
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? Function odd(Integer n) if (n == 0) return 0 end if return odd(n - 1) + 2 n - 1 End…
- What will be the output of the following pseudocode? ((mask >> i) & 1 is evaluated before ==; ^ is XOR) Integer a[3] = {5, 1, 6}, total =…
- What will be the output of the following pseudocode? (+ joins strings) String s = "PAYPALISHIRING" Integer rows = 3, r = 0, step = 1…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[5] = {1, 3, 5, 4, 2}, n = 5 Integer i = n - 2…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[9] = {3, 2, 3, 1, 2, 4, 5, 5, 6}, k = 4 sort a…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Function canSplit(Integer a[]…