ProgrammingHardTCS NQT · Accenture pattern
What will be the output of the following pseudocode (GCD of an entire list, folding pairwise)?
What will be the output of the following pseudocode (GCD of an entire list, folding pairwise)?
Function gcdList(Array arr)
Integer r = arr[0]
for (each x in arr, skipping the first)
r = gcd(r, x)
end for
return r
End Function
Print gcdList({24, 36, 48})- A. 6
- B. 4
- C. 12
- D. 24
Correct answer: C. 12
gcd(24,36)=12, then gcd(12,48)=12. Folding the gcd pairwise across the whole list correctly gives the overall GCD, 12.
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 (counting permutations of a string via recursion)? Function countPerms(String s) if…
- What will be the output of the following pseudocode (basic run-length string compression, kept only if actually shorter)? Function…
- What will be the output of the following pseudocode (counting distinct substrings of length 2)? String s = "abab" Set subs = empty set for…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Integer n = 20, steps = 0…
- What will be the output of the following pseudocode (counting all subsets of a set of size n)? Function countSubsets(Integer n) if (n ==…
- What will be the output of the following pseudocode (counting set bits using Brian Kernighan's trick, for each value 0 to 7)? (& is…