ProgrammingHardAccenture · Cognizant pattern
What will be the output of the following pseudocode (a do-while-style loop that always runs at least once)?
What will be the output of the following pseudocode (a do-while-style loop that always runs at least once)?
Integer x = 10, c = 0
repeat
x = x - 3
c = c + 1
until (x <= 0)
Print x, c- A. 1 3
- B. -2 4
- C. 0 4
- D. -2 3
Correct answer: B. -2 4
x goes 10->7->4->1->-2 over 4 iterations; the until condition (x<=0) is only checked AFTER each iteration, so it keeps going through x=1 (still >0) and stops only once x becomes -2.
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 (searching a 2D array, stopping both loops once found)? (Arrays are indexed from 0.)…
- What will be the output of the following pseudocode (finding the missing number from 1..n)? (Arrays are indexed from 0.) Integer arr[5] =…
- What will be the output of the following pseudocode (checking whether an array is sorted in non-decreasing order)? (Arrays are indexed…
- What will be the output of the following pseudocode (summing a range with a step)? Function sumRange(Integer a, Integer b, Integer step)…
- What will be the output of the following pseudocode (tail-recursive factorial with an accumulator)? Function factAcc(Integer n, Integer…
- What will be the output of the following pseudocode (GCD of an entire list, folding pairwise)? Function gcdList(Array arr) Integer r =…