ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains
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 of loop iterations printed?
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 of loop iterations printed?
count = 0
for i = 1 to 20 step 3
count = count + 1
end for
Print count- A. 5
- B. 10
- C. 20
- D. 7
Correct answer: D. 7
The values 1, 4, 7, 10, 13, 16, 19 are 7 iterations. Stepping by 4 gives 5 iterations, a step of 2 gives 10, and counting to 20 itself gives 20.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- 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)…
- Array a = [4, 9, 1, 7, 3]. The procedure below finds the sum of all elements that are greater than the mean of the array. What is printed?…
- Array a = [2, 5, 7, 10, 12] is searched for the number of elements less than or equal to 7 by a binary search that finds the boundary…
- The procedure below counts the pairs (i, j) with i < j and a[i] + a[j] = 8, for the array a = [1, 5, 3, 3, 7, 4, 6, 2]. What is printed?…
- Array a = [4, 4, 6, 1, 1] has a value appearing more than once. The procedure below counts the values that appear exactly twice. What is…