ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains
Array a = [3, 1, 4, 1, 5, 9, 2, 6] is scanned from left to right. The procedure below counts the positions i (from 2 to n) where a[i] is smaller than a[i-1]. What is printed?
Array a = [3, 1, 4, 1, 5, 9, 2, 6] is scanned from left to right. The procedure below counts the positions i (from 2 to n) where a[i] is smaller than a[i-1]. What is printed?
c = 0
for i = 2 to n
if a[i] < a[i-1] then c = c + 1
end for
Print c- A. 4
- B. 0
- C. 2
- D. 3
Correct answer: D. 3
The falls are 3 to 1, 4 to 1, 9 to 2, so there are 3 falls. Counting the rises gives 4, counting equal neighbours gives 0, and skipping the comparison at index 1 leaves only 2 falls.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Array a = [5, 3, 8, 1] (length 4). The procedure below counts how many elements are leaders, where an element is a leader if it is…
- For the array a = [1, 2, 3, 5, 4] the next permutation (in lexicographic order) is computed by the standard procedure: find the last i…
- Array a = [3, 6, -2, -5, 7, 3]. The procedure below finds the largest product of two adjacent elements. What is printed? best = a[0] a[1]…
- The loop below prints the running count for i = 1, 4, 7, 10, ... up to and including 20 (i increases by 3 each time). What is the number…
- Array a = [1, 2, 1, 2, 3, 2, 1, 1] is processed left to right. The procedure below finds the length of the longest run of equal…
- Array a = [1, 1, 2, 2, 2, 3, 4, 4] is sorted. The two-pointer procedure below removes duplicates in place (keeping one copy of each value)…