ProgrammingHardAccenture · Cognizant pattern
What will be the output? (A do-while loop runs its body once before checking the condition; / is integer division.)
What will be the output? (A do-while loop runs its body once before checking the condition; / is integer division.)
Integer x = 0, c = 0, y = 10, k = 0
do
c = c + 1
x = x + 5
while (x < 0)
do
k = k + 1
y = y / 2
while (y > 0)
Print c, x, k- A. 0 0 4
- B. 0 0 3
- C. 1 5 3
- D. 1 5 4
Correct answer: D. 1 5 4
The first loop's condition is false from the start, but a do-while still runs once: c = 1, x = 5. The second loop takes y through 10 → 5 → 2 → 1 → 0, which is 4 passes. Treating them as while loops gives 0 0 for the first loop.
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? Integer p = 1 for (each i from 1 to 10) p = (p 3) mod 7 end for Print p
- The array holds {1, 3, 5, 7, 9, 11, 13, 15, 17} (indexed from 0). What is printed? (/ is integer division.) Integer low = 0, high = 8…
- How many swaps does this pseudocode perform? (Arrays are indexed from 0.) Integer arr[5] = {5, 1, 4, 2, 8} Integer swaps = 0 for (each i…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Function f(Integer n) if (n…
- What will be the output of the following pseudocode? Integer a = 3, b b = a a a = b - a b = b + a Print a mod b, b mod a
- How many times is count incremented? Integer n = 4, count = 0 for (each i from 1 to n) for (each j from 1 to i i) if (j mod i == 0) for…