Sets in python: 18 MCQs with answers

All 18 multiple-choice questions from this kit, free and with no sign-up.

Warm-up · 8 questions

Q1. What is the correct way to create an empty set in Python?

Beginner
A {} B () C [] D set()
Show answer & explanation

set()

Using `set()` creates an empty set, while `{}` creates an empty dictionary.

Q2. Which of the following is a property of a Python set?

Beginner
A Mutable B Ordered C Allows duplicates D Indexable
Show answer & explanation

Mutable

Sets are mutable (can add/remove elements) but unordered and do not allow duplicates.

Q3. Which method adds an element to a set?

Beginner
A insert() B append() C extend() D add()
Show answer & explanation

add()

`add()` is the correct method for sets; `append()` and `extend()` are for lists.

Q4. What is the output of `len({1, 2, 3, 2, 1})`?

Beginner
A 2 B Error C 3 D 5
Show answer & explanation

3

Sets automatically remove duplicates, so the set becomes {1, 2, 3} with length 3.

Q5. A set can contain duplicate elements.

Beginner
A True B False
Show answer & explanation

False

Sets are collections of unique elements; duplicates are automatically removed.

Q6. You can access elements of a set using an index like `my_set[0]`.

Beginner
A True B False
Show answer & explanation

False

Sets are unordered, so they do not support indexing or slicing.

Q7. The `clear()` method removes all elements from a set.

Beginner
A True B False
Show answer & explanation

True

`clear()` empties the set, leaving it with no elements.

Q8. A set can contain both integers and strings.

Beginner
A True B False
Show answer & explanation

True

Sets can hold heterogeneous hashable types, including integers and strings.

Building up · 9 questions

Q9. What will be the output of `{1, 2, 3} | {2, 3, 4}`?

Intermediate
A {1, 2, 3, 4} B Error C {2, 3} D {1, 4}
Show answer & explanation

{1, 2, 3, 4}

The `|` operator performs union, combining all unique elements from both sets.

Q10. What does `{1, 2, 3} - {2, 3, 4}` return?

Intermediate
A {1} B {1, 4} C {2, 3} D {4}
Show answer & explanation

{1}

The `-` operator performs difference, returning elements in the first set not in the second.

Q11. Which of the following cannot be an element of a set?

Intermediate
A Tuple B Integer C List D String
Show answer & explanation

List

Sets require hashable elements; lists are mutable and unhashable, so they cannot be added.

Q12. Which operator checks if a set is a subset of another?

Intermediate
A < B <= C >= D ==
Show answer & explanation

<=

`

Q13. What does `{1, 2, 3} & {2, 3, 4}` return?

Intermediate
A {1, 4} B {1, 2, 3, 4} C {2, 3} D Error
Show answer & explanation

{2, 3}

The `&` operator performs intersection, returning elements common to both sets.

Q14. Which method removes an element from a set and raises an error if not found?

Intermediate
A delete() B discard() C remove() D pop()
Show answer & explanation

remove()

`remove()` removes the element and raises KeyError if missing; `discard()` does not raise an error.

Q15. What is the output of `{x**2 for x in range(3)}`?

Intermediate
A {0, 1, 2} B {0, 1, 4} C Error D {0, 1, 4, 9}
Show answer & explanation

{0, 1, 4}

This is a set comprehension; range(3) gives 0,1,2 and squares are 0,1,4.

Q16. What does `{1, 2}.issuperset({1})` return?

Intermediate
A None B True C False D Error
Show answer & explanation

True

`issuperset()` returns True if the set contains all elements of the argument set.

Q17. The `^` operator performs symmetric difference on sets.

Intermediate
A True B False
Show answer & explanation

True

Symmetric difference returns elements in either set but not both.

Topper level · 1 questions

Q18. Which of the following is true about frozenset?

Advanced
A It is mutable B It allows duplicates C It is ordered D It can be used as a dictionary key
Show answer & explanation

It can be used as a dictionary key

A frozenset is immutable and hashable, so it can be used as a dictionary key.

Make your own

Your notes can be a kit like these

Every kit here was made from real study material. Point Veda at a chapter, a PDF, a photo or a link and get notes, flashcards, practice MCQs, games and a scored test — free, in 42 languages.

  • Chapter notes
  • Flashcards
  • Practice MCQs
  • Scored tests
  • Exam prep

Get Veda free See what students get

Free to start · No ads · Works in 42 languages