ProgrammingHardCapgemini · TCS NQT

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 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 printed?
for each distinct value v in a
    if the number of occurrences of v is exactly 2 then count = count + 1
Print count
  1. A. 3
  2. B. 2
  3. C. 4
  4. D. 0

Correct answer: B. 2

The values 4 and 1 each occur exactly twice, so the answer is 2. Counting all distinct values gives 3, summing the repeated occurrences gives 4, and comparing the values with 2 finds none.

More Pseudocode and Code Tracing questions

Exams that ask this topic