ProgrammingHardCAT · MBA placement

Two positions in the word "hello" are chosen (the letters at the two positions form a pair, and positions are distinct). The procedure below counts the pairs whose two letters are different. What is printed?

Two positions in the word "hello" are chosen (the letters at the two positions form a pair, and positions are distinct). The procedure below counts the pairs whose two letters are different. What is printed?
count = 0
for i = 0 to n - 2
    for j = i + 1 to n - 1
        if s[i] != s[j] then count = count + 1
    end for
end for
Print count
  1. A. 10
  2. B. 1
  3. C. 6
  4. D. 9

Correct answer: D. 9

The only equal pair is the two l letters, so 10 - 1 = 9 pairs have different letters. Counting all pairs gives 10, counting equal pairs gives 1, and choosing two of the four distinct letters gives 6.

More Pseudocode and Code Tracing questions

Exams that ask this topic