Output of Python Programs | Set 24 (Sets)

Prerequisite: Python-Sets

1. What is the output of the code shown below?

Options:

  1. {1, 2, 3}
  2. {1, 2, 3, 4}
  3. {1, 2, 3, 4, 4}
  4. Error
Output:
2. {1, 2, 3, 4}

Explanation : Duplicate values are not allowed in sets. Hence, the output of the code shown above will be a set containing the duplicate value only once. Hence output will be {1, 2, 3, 4}.

2. What is the output of the code shown below?

Options:

  1. {1, 2, 3, 4, 5}
  2. {3, 4, 5, 1, 2, 3}
  3. {1, 2, 3, 3, 4, 5}
  4. Error
Output:
1. {1, 2, 3, 4, 5}

Explanation: The method update adds elements to a set.

3. What is the output of the code shown below?

Options:

  1. {1, 2, 3, 4}
  2. {1, 2, 3}
  3. Invalid Syntax
  4. Error
Output:
2. {1, 2, 3}

Explanation: In the above piece of code, set2 is barely a copy and not an alias of set1. Hence any change made in set2 isn’t reflected in set1.

4. What is the output of the code shown below?

Options:

  1. {1, 2, 3, 4}
  2. {1, 2, 3}
  3. Invalid Syntax
  4. None
Output:
4. None

Explanation: add method doesn’t return anything. Hence there will be no output.

5. What is the output of the code shown below?

Options:

  1. 3
  2. 6
  3. Unexpected
  4. Error
Output:
4. Error

Explanation: unsupported operand type(s) for +: ‘set’ and ‘set’.



My Personal Notes arrow_drop_up

Recommended Posts:


    Article Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.