This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Sets – 3”.
1. What will be the output of the following Python code?
s=set() print(type(s))
a) <’set’>
b) <class ‘set’>
c) set
d) class set
View Answer
Explanation: When you create an empty set using s = set() and then print type(s), Python outputs the type of the object. Since s is a set, the output is <class ‘set’>, which indicates the data type of the variable.
2. The following Python code results in an error.
s={2, 3, 4, [5, 6]} print(s)
a) True
b) False
View Answer
Explanation: In Python, sets can only contain hashable (immutable) elements. Lists like [5, 6] are mutable and unhashable, so trying to include a list inside a set (s = {2, 3, 4, [5, 6]}) will raise a TypeError. Therefore, the statement is True — the code results in an error.
3. Set makes use of __________
Dictionary makes use of ____________
a) keys, keys
b) key values, keys
c) keys, key values
d) key values, key values
View Answer
Explanation: A set in Python is implemented using a hash table and only stores keys (i.e., unique values without associated data). A dictionary also uses a hash table but stores both keys and their associated values (key-value pairs).
4. Which of the following lines of code will result in an error?
a) s={abs}
b) s={4, ‘abc’, (1,2)}
c) s={2, 2.2, 3, ‘xyz’}
d) s={san}
View Answer
Explanation: Here,
- abs is a built-in function and is hashable, so s = {abs} is valid.
- The set {4, ‘abc’, (1, 2)} contains immutable and hashable elements, so it’s valid.
- The set {2, 2.2, 3, ‘xyz’} contains hashable types, so it’s valid.
- san is undefined (not quoted or declared), so s = {san} raises a NameError because san is not defined.
5. What will be the output of the following Python code?
s={2, 5, 6, 6, 7} print(s)
a) {2, 5, 7}
b) {2, 5, 6, 7}
c) {2, 5, 6, 6, 7}
d) Error
View Answer
Explanation: Sets automatically remove duplicate elements. In the set s = {2, 5, 6, 6, 7}, the duplicate 6 will be removed, so printing s outputs {2, 5, 6, 7}.
6. Input order is preserved in sets.
a) True
b) False
View Answer
Explanation: Sets in Python are unordered collections, meaning they do not preserve the order of elements. When you create or print a set, the order of elements can appear arbitrary and may vary. Only from Python 3.7+, dictionaries preserve insertion order, but sets do not.
7. Write a list comprehension for number and its cube for:
l=[1, 2, 3, 4, 5, 6, 7, 8, 9]
a) [x**3 for x in l]
b) [x^3 for x in l]
c) [x**3 in l]
d) [x^3 in l]
View Answer
Explanation: In Python, ** is the exponentiation operator used to calculate the cube of a number (x**3). The list comprehension [x**3 for x in l] generates a new list with each element being the cube of the corresponding element in l. The ^ operator, used in options b and d, is a bitwise XOR, not exponentiation, so it’s incorrect here.
8. What will be the output of the following Python code?
s={1, 2, 3} s.update(4) print(s)
a) {1, 2, 3, 4}
b) {1, 2, 4, 3}
c) {4, 1, 2, 3}
d) Error
View Answer
Explanation: The update() method expects an iterable as an argument (like a list, set, or tuple) to add multiple elements to the set. Passing an integer 4 directly is not iterable, so s.update(4) will raise a TypeError. To add a single element using update(), you need to pass it as an iterable, for example: s.update([4]).
9. Which of the following functions cannot be used on heterogeneous sets?
a) pop
b) remove
c) update
d) sum
View Answer
Explanation: Functions like pop(), remove(), and update() can be used on sets regardless of whether the elements are heterogeneous (mixed data types) or homogeneous. However, sum() requires all elements to be numeric because it adds them up. If the set contains mixed types (like strings and numbers), sum() will raise a TypeError. Therefore, sum() cannot be used on heterogeneous sets.
10. What will be the output of the following Python code?
s={4>3, 0, 3-3} print(all(s)) print(any(s))
a)
True False
b)
False True
c)
True True
d)
False FalseView Answer
Explanation: The set contains the values True, 0, and 0. The all() function returns False because not all elements are true—there are zeros which are considered false. The any() function returns True since at least one element (True) is True.
Sanfoundry Global Education & Learning Series – Python.
To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Practice Programming MCQs
- Apply for Python Internship
- Check Information Technology Books
- Apply for Programming Internship
- Check Python Books