nerdexam
Python_Institute

PCEP-30-02 · Question #38

What is the expected output of the following code? ``python data = set([1, 2, 2, 3, 3, 3, 4, 4, 4, 4]) print(len(data)) ``

The correct answer is A. 4. set() removes duplicate values, so set([1, 2, 2, 3, 3, 3, 4, 4, 4, 4]) produces {1, 2, 3, 4} - four unique elements - making len(data) equal to 4 (A). Option C (10) is wrong because that's the length of the original list before deduplication; options B, E, and F (2, 1, 3) have…

Question

What is the expected output of the following code?
data = set([1, 2, 2, 3, 3, 3, 4, 4, 4, 4])
print(len(data))

Options

  • A4
  • B2
  • C10
  • D0
  • E1
  • F3

How the community answered

(25 responses)
  • A
    80% (20)
  • B
    4% (1)
  • E
    12% (3)
  • F
    4% (1)

Explanation

set() removes duplicate values, so set([1, 2, 2, 3, 3, 3, 4, 4, 4, 4]) produces {1, 2, 3, 4} - four unique elements - making len(data) equal to 4 (A). Option C (10) is wrong because that's the length of the original list before deduplication; options B, E, and F (2, 1, 3) have no basis in either the list or the set; option D (0) would only be correct for an empty set. Memory tip: Think of a set like a bag of unique coins - no matter how many duplicate pennies you drop in, you still only have one penny in the bag, so always count the distinct values, not the total insertions.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice