ProgrammingHardTCS NQT · 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 = 10, steps = 0
while (n != 1)
if (n mod 3 == 0)
n = n / 3
else
n = n + 1
end if
steps = steps + 1
end while
Print steps- A. 3
- B. 8
- C. 7
- D. 9
Correct answer: B. 8
Trace: 10 → 11 → 12 → 4 → 5 → 6 → 2 → 3 → 1, eight updates. Reading the else branch as n − 1 gives 10 → 9 → 3 → 1 (3 steps), stopping before the last step 3 → 1 gives 7, and counting the 9 values visited instead of the moves gives 9.
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? (All variables are integers, so / is integer division; and / are evaluated left to…
- What will be the output of the following pseudocode? Integer h[6] = {2, 1, 5, 6, 2, 3}, best = 0 for (each i from 0 to 5) Integer mn =…
- What will be the output of the following pseudocode? String stream = "aabcbc" Integer cnt[26] = all 0 Queue q = empty for (each ch in…
- What will be the output of the following pseudocode? Procedure f(Integer n) if (n == 0) return end if Print n f(n - 1) Print n End…
- What will be the output of the following pseudocode? // a trie (prefix tree) starts with only an empty root for each word in {"car"…
- What will be the output of the following pseudocode? // LRU cache of capacity 2: get(k) returns the value or -1; put evicts the least…