Sets Questions and Answers

Practice Mode
Showing 10 of 25 questions
Q1
Which of the following creates an empty set in Python?
  • A {}
  • B set()
  • C []
  • D ()
Answer: Option B
Explanation: Curly braces {} without values create an empty dictionary, not a set. To create an empty set, you must use the set() constructor.
Q2
What is the output of: set([1, 2, 3, 2, 1])?
  • A [1, 2, 3]
  • B {1, 2, 3}
  • C {1, 2, 3, 2, 1}
  • D Error
Answer: Option B
Explanation: Sets automatically remove duplicate elements, so [1, 2, 3, 2, 1] becomes {1, 2, 3}.
Q3
Which method adds an element to a set?
  • A append()
  • B insert()
  • C add()
  • D push()
Answer: Option C
Explanation: add() method adds a single element to a set, while update() adds multiple elements from an iterable.
Q4
What does the union() method do for sets?
  • A Combines two sets and returns common elements
  • B Combines two sets and returns all unique elements
  • C Removes common elements from both sets
  • D Updates the first set with elements from second set
Answer: Option B
Explanation: Union returns a new set containing all elements from both sets without duplicates.
Q5
Which operator is used for set difference in Python?
  • A -
  • B &
  • C |
  • D ^
Answer: Option A
Explanation: The minus (-) operator returns elements present in the first set but not in the second set.
Q6
What is the result of: {1, 2, 3} & {2, 3, 4}?
  • A {1, 2, 3, 4}
  • B {2, 3}
  • C {1, 4}
  • D {1, 2}
Answer: Option B
Explanation: The & operator performs intersection and returns common elements between sets.
Q7
Which method removes and returns an arbitrary element from a set?
  • A remove()
  • B discard()
  • C pop()
  • D delete()
Answer: Option C
Explanation: pop() removes and returns an arbitrary element from the set. Raises KeyError if set is empty.
Q8
What is the main characteristic of sets in Python?
  • A Ordered and indexed
  • B Allow duplicate elements
  • C Unordered and unique elements
  • D Immutable once created
Answer: Option C
Explanation: Sets are unordered collections of unique elements. They don't allow duplicates and are mutable.
Q9
Which of these is a frozen set?
  • A set([1, 2, 3])
  • B {1, 2, 3}
  • C frozenset([1, 2, 3])
  • D All of the above
Answer: Option C
Explanation: frozenset() creates an immutable set that cannot be modified after creation.
Q10
What does the symmetric_difference() method return?
  • A Common elements only
  • B Elements in first set but not in second
  • C Elements in either set but not both
  • D All elements from both sets
Answer: Option C
Explanation: Symmetric difference returns elements that are in either set but not in their intersection.
Questions and Answers for Competitive Exams Various Entrance Test