Python Set MCQ : Set 1

Python Set MCQ

1). What is a set in Python?

a) A sequence of elements
b) A collection of unordered and unique elements
c) A data structure used for sorting elements
d) A variable that can store multiple values

Correct answer is: b) A collection of unordered and unique elements
Explanation: In Python, a set is an unordered collection of unique elements.

2). Which symbol is used to define a set in Python?

a) []
b) {}
c) ()
d) <>

Correct answer is: b) {}
Explanation: Curly braces {} are used to define a set in Python.

3). What is the result of executing the following code: set = {1, 2, 3}?

a) [1, 2, 3, 4]
b) {1, 2, 3,}
c) {1, 2, 3}
d) {2, 2, 3, 1}

Correct answer is: c) {1, 2, 3}
Explanation: Sets only contain unique elements, so duplicate values are removed.

4). In Python which method is used to add an element to a set?

a) add()
b) insert()
c) append()
d) update()

Correct answer is: a) add()
Explanation: The add() method is used to add a single element to a set.

5). Which method is used to remove an element from a set?

a) remove()
b) delete()
c) discard()
d) pop()

Correct answer is: c) discard()
Explanation: The discard() method removes a specified element from a set.

6). What happens if you use the remove() method to remove an element that doesn’t exist in the set?

a) It raises a KeyError
b) It raises an IndexError
c) It removes the last element in the set
d) It does nothing and doesn’t raise an error

Correct answer is: a) It raises a KeyError
Explanation: The remove() method raises a KeyError if the element doesn’t exist in the set.

7). Which method is used to remove an arbitrary element from a set?

a) remove()
b) delete()
c) discard()
d) pop()

Correct answer is: d) pop()
Explanation: The pop() method removes and returns an arbitrary element from a set.

8). Which method is used to clear all elements from a set?

a) clear()
b) remove()
c) discard()
d) delete()

Correct answer is: a) clear()
Explanation: The clear() method removes all elements from a set, leaving an empty set.

9). Which operator is used to perform the union of two sets?

a) &
b) |
c) –
d) ^

Correct answer is: b) |
Explanation: The | operator is used for the union operation, which combines elements from two sets.

10). Which operator is used to perform the intersection of two sets?

a) &
b) |
c) –
d) ^

Correct answer is: a) &
Explanation: The & operator is used for the intersection operation, which returns common elements from two sets.

11). Which operator is used to perform the difference between two sets?

a) &
b) |
c) –
d) ^

Correct answer is: c) –
Explanation: The – operator is used for the difference operation, which returns elements present in the first set but not in the second set.

12). Which operator is used to perform the symmetric difference between two sets?

a) &
b) |
c) –
d) ^

Correct answer is: d) ^
Explanation: The ^ operator is used for the symmetric difference operation, which returns elements present in either set, but not in both.

13). What is the result of executing the following code: set1 = {1, 2, 3}; set2 = {3, 4, 5}; symmetric_difference_set = set1 ^ set2; print(symmetric_difference_set)?
a) {1, 2, 3, 4, 5}
b) []
c) {1, 2, 4, 5}
d) {1, 2, 3}

Correct answer is: c) {1, 2, 4, 5}
Explanation: The ^ operator returns elements present in either set but not in both, which are 1, 2, 4, and 5.

14). Which method is used to check if a set is a subset of another set?

a) issubset()
b) issuperset()
c) subset()
d) superset()

Correct answer is: a) issubset()
Explanation: The issubset() method checks if a set is a subset of another set.

15). Which method is used to check if a set is a superset of another set?

a) issubset()
b) issuperset()
c) subset()
d) superset()

Correct answer is: b) issuperset()
Explanation: The issuperset() method checks if a set is a superset of another set.

16). Which method is used to check if two sets have no common elements?

a) isdisjoint()
b) iscommon()
c) nocommon()
d) nodisjoint()

Correct answer is: a) isdisjoint()
Explanation: The isdisjoint() method checks if two sets have no common elements.

17). Which method is used to return a shallow copy of a set?

a) copy()
b) clone()
c) duplicate()
d) replicate()

Correct answer is: a) copy()
Explanation: The copy() method in Python returns a shallow copy of a set.

18). Which method is used to return the length of a set?

a) size()
b) length()
c) count()
d) len()

Correct answer is: d) len()
Explanation: The len() function in Python returns the number of elements in a set.

19). Which method is used to convert a list into a set?

a) to_set()
b) set()
c) convert_set()
d) list_to_set()

Correct answer is: b) set()
Explanation: The set() function is used to convert a list into a set.

20). Which method is used to convert a set into a list?

a) to_list()
b) list()
c) convert_list()
d) set_to_list()

Correct answer is: b) list()
Explanation: The list() function is used to convert a set into a list.

21). Which method is used to iterate over the elements of a set?

a) iterate()
b) for loop
c) loop()
d) iterate_elements()

Correct answer is: b) for loop
Explanation: A for loop is used to iterate over the elements of a set.

22). Which method is used to return a frozen set from a set?

a) freeze()
b) frozen_set()
c) freeze_set()
d) frozenset()

Correct answer is: d) frozenset()
Explanation: The frozenset() function is used to return a frozen set from a set.

Leave a Comment