ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains
Array a = [3, 8, 7, 2, 7]. A linear scan returns the 0-based index of the first occurrence of 7. What is printed?
Array a = [3, 8, 7, 2, 7]. A linear scan returns the 0-based index of the first occurrence of 7. What is printed?
for i = 0 to n - 1
if a[i] == 7 then print i; stop
end for- A. 4
- B. 1
- C. 3
- D. 2
Correct answer: D. 2
The first 7 sits at position 2 counting from 0, which is the third element. Scanning from the end finds index 4, finding the first value above 7 gives index 1, and reporting a 1-based position gives 3.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Binary search for the value 13 in the sorted array [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] uses lo = 0, hi = n - 1 and mid = (lo + hi) div 2…
- Array a = [8, 4, 2, 1, 7, 5]. The number of inversions (pairs i < j with a[i] > a[j]) is the number of out-of-order pairs, and merge sort…
- Prefix sums are computed for a = [4, 1, 7, 3, 2, 8] with P[0] = 0 and P[k] = a[1] + ... + a[k]. A range sum for 1-based positions l to r…
- 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…
- 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…