ProgrammingHardAccenture · Cognizant pattern
What will be the output of the following pseudocode (counting distinct ordered character pairs (s[i], s[j]) with i<j)? (Strings are indexed from 0.)
What will be the output of the following pseudocode (counting distinct ordered character pairs (s[i], s[j]) with i<j)? (Strings are indexed from 0.)
String s = "aabc"
Set pairs = empty set
for (each i from 0 to length(s) - 1)
for (each j from i + 1 to length(s) - 1)
add(pairs, (s[i], s[j]))
end for
end for
Print size(pairs)- A. 6
- B. 4
- C. 5
- D. 3
Correct answer: B. 4
The ordered pairs generated are (a,a),(a,b),(a,c),(a,b),(a,c),(b,c); as a set, the distinct ones are (a,a),(a,b),(a,c),(b,c) -- 4 distinct pairs.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode (capitalizing the first letter of each word, manually)? String s = "hello world foo"…
- What will be the output of the following pseudocode (maximum sum of any 4 consecutive elements, sliding window)? (Arrays are indexed from…
- What will be the output of the following pseudocode (reversing an array in groups of size k)? (Arrays are indexed from 0.) Integer arr[8]…
- What will be the output of the following pseudocode? (A square matrix's main diagonal is mat[i][i]; its anti-diagonal is mat[i][n-1-i]. If…
- This function uses a counter that PERSISTS across calls (a static/global variable), starting at 3. What does each of the 7 calls print, in…
- What will be the output of the following pseudocode? Function isEven(Integer n) if (n == 0) return true end if return isOdd(n - 1) end…