ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains
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?
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?
count = 0
for i = 1 to 3
for j = i to 3
count = count + 1
end for
end for
Print count- A. 3
- B. 9
- C. 6
- D. 10
Correct answer: C. 6
The pairs are (1,1), (1,2), (1,3), (2,2), (2,3) and (3,3), which is 6. Requiring i < j gives 3, using all j values gives 9, and adding i per pass gives 14.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Array a = [3, 1, 2]. The procedure below performs a stable count of the number of ways the array can be read in increasing order using…
- Array a = [1, 3, 2, 3, 4, 1] is searched for the longest strictly increasing contiguous run. The procedure below returns its length. What…
- Array a = [5, 1, 4, 2, 3, 7]. The procedure below counts the interior local maxima, that is, positions i from 1 to n - 2 whose value is…
- The KMP prefix function pi[i] is the length of the longest proper prefix of s[0..i] that is also a suffix of it. For s = "abacabab" the…
- Pattern "aba" is searched in the text "abababa" with overlapping matches allowed. The procedure below counts the starting positions where…
- For the string s = "forgeeksskeegfor", the procedure below expands around each centre to find the length of the longest palindromic…