ProgrammingHardTCS NQT · Accenture
Array a = [0, 2, 2, 3, 5]. The procedure below counts the positions i (0-based) where a[i] equals i. What is printed?
Array a = [0, 2, 2, 3, 5]. The procedure below counts the positions i (0-based) where a[i] equals i. What is printed?
count = 0
for i = 0 to n - 1
if a[i] == i then count = count + 1
end for
Print count- A. 3
- B. 2
- C. 0
- D. 5
Correct answer: A. 3
Positions 0, 2 and 3 hold their own index, so the count is 3. Counting the mismatches gives 2, counting positions that hold index minus one gives 0, and the array length is 5.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- For the array a = [1, 2, 3, 4, 5, 6], the procedure below counts the contiguous subarrays of length 3 whose sum is divisible by 3. What is…
- Two numbers are formed from the arrays a = [1, 3, 5] and b = [2, 3, 6] by merging them into one sorted list. The procedure below prints…
- For the array a = [-1, 4, -2, 6, -3, 5], the procedure below finds the maximum sum of a contiguous window of exactly 3 elements (windows…
- Array a = [4, 9, 2, 8, 2, 7]. The procedure below counts the prefix records, that is, the positions i where a[i] is strictly greater than…
- A rotated sorted array a = [4, 5, 6, 7, 0, 1, 2] has its smallest element at some index. The procedure below finds that index by a linear…
- The loops below print the pairs (i, j) with 1 <= i <= j <= 3 in order, and the procedure prints the total count of pairs. What is printed?…