ProgrammingHardTCS NQT · Accenture pattern
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.)
Function countDigits(Integer n)
if (n == 0)
return 1
end if
Integer c = 0
while (n > 0)
n = n / 10
c = c + 1
end while
return c
End Function
Print countDigits(94857)- A. 4
- B. 5
- C. 6
- D. 9
Correct answer: B. 5
Each pass strips one digit by integer-dividing by 10: 94857->9485->948->94->9->0, which is 5 divisions, matching the 5 digits of 94857.
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 (Pascal's identity for combinations)? Function C(Integer n, Integer r) if (r == 0 or r…
- What will be the output of the following pseudocode (Armstrong number check)? Function isArmstrong(Integer n) String s = toString(n)…
- What will be the output of the following pseudocode? Integer c = 0 for (each i from 1 to 3) for (each j from 1 to 3) if (j == 2) break end…
- What will be the output of the following pseudocode (Kadane's maximum subarray sum)? (Arrays are indexed from 0.) Integer arr[9] = {-2, 1…
- What is the time complexity of binary search on a sorted array of n elements? Function binarySearch(Array arr, Integer target) Integer lo…
- What is the worst-case time complexity of the following pseudocode (bubble sort) on an array of n elements? for (each i from 0 to n - 2)…