ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains
Array a = [4, 1, 7, 3, 2, 8] (three queries are not needed here). The procedure below counts the elements that are strictly greater than the element before them (a rise), for positions 2 to n. What is printed?
Array a = [4, 1, 7, 3, 2, 8] (three queries are not needed here). The procedure below counts the elements that are strictly greater than the element before them (a rise), for positions 2 to n. 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. 3
- B. 0
- C. 5
- D. 2
Correct answer: D. 2
The rises are 1 to 7 and 2 to 8, so the count is 2. Counting the falls gives 3, counting equal neighbours gives 0 and the number of adjacent pairs is 5.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Array a = [1, 2, 3, 4, 5, 6, 7] is rotated to the right by k = 3 positions: each element moves k places to the right with wrap-around. The…
- Lomuto partition is applied to a = [3, 8, 2, 5, 1, 4] with the pivot equal to the last element. For j = 0 to n - 2: if a[j] <= pivot, then…
- For the array a = [2, 9, 4, 7, 5, 6, 3] (indices 0 to 6), the procedure below computes the sum of the maximum values of all contiguous…
- 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…
- 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…