ProgrammingHardTCS NQT · Accenture pattern
What will be the output of the following pseudocode (checking whether an array is sorted in non-decreasing order)? (Arrays are indexed from 0.)
What will be the output of the following pseudocode (checking whether an array is sorted in non-decreasing order)? (Arrays are indexed from 0.)
Function isSorted(Array arr)
for (each i from 1 to length(arr) - 1)
if (arr[i] < arr[i - 1])
return false
end if
end for
return true
End Function
Print isSorted({1, 2, 2, 3, 4}), isSorted({1, 3, 2})- A. true false
- B. false true
- C. true true
- D. false false
Correct answer: A. true false
{1,2,2,3,4} never decreases (equal adjacent values are fine for non-decreasing), so it returns true. {1,3,2} drops from 3 to 2, violating the check, so it returns false.
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 (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 =…
- 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…