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)
  1. A. 6
  2. B. 4
  3. C. 5
  4. 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.

More Pseudocode and Code Tracing questions

Exams that ask this topic