This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Sets – 2”.
1. Which of these about a frozenset is not true?
a) Mutable data type
b) Allows duplicate values
c) Data type with unordered values
d) Immutable data type
View Answer
Explanation: A frozenset is an immutable data type.
2. What is the syntax of the following Python code?
>>> a=frozenset(set([5,6,7])) >>> a
a) {5,6,7}
b) frozenset({5,6,7})
c) Error, not possible to convert set into frozenset
d) Syntax error
View Answer
Explanation: The above piece of code is the correct syntax for creating a frozenset.
3. Is the following Python code valid?
>>> a=frozenset([5,6,7]) >>> a >>> a.add(5)
a) Yes, now a is {5,5,6,7}
b) No, frozen set is immutable
c) No, invalid syntax for add method
d) Yes, now a is {5,6,7}
View Answer
Explanation: Since a frozen set is immutable, add method doesn’t exist for frozen method.
4. Set members must not be hashable.
a) True
b) False
View Answer
Explanation: Set members must always be hashable.
5. What will be the output of the following Python code?
>>> a={3,4,5} >>> a.update([1,2,3]) >>> a
a) Error, no method called update for set data type
b) {1, 2, 3, 4, 5}
c) Error, list can’t be added to set
d) Error, duplicate item present in list
View Answer
Explanation: The method update adds elements to a set.
6. What will be the output of the following Python code?
>>> a={1,2,3} >>> a.intersection_update({2,3,4,5}) >>> a
a) {2,3}
b) Error, duplicate item present in list
c) Error, no method called intersection_update for set data type
d) {1,4,5}
View Answer
Explanation: The method intersection_update returns a set which is an intersection of both the sets.
7. What will be the output of the following Python code?
>>> a={1,2,3} >>> b=a >>> b.remove(3) >>> a
a) {1,2,3}
b) Error, copying of sets isn’t allowed
c) {1,2}
d) Error, invalid syntax for remove
View Answer
Explanation: Any change made in b is reflected in a because b is an alias of a.
8. What will be the output of the following Python code?
>>> a={1,2,3} >>> b=a.copy() >>> b.add(4) >>> a
a) {1,2,3}
b) Error, invalid syntax for add
c) {1,2,3,4}
d) Error, copying of sets isn’t allowed
View Answer
Explanation: In the above piece of code, b is barely a copy and not an alias of a. Hence any change made in b isn’t reflected in a.
9. What will be the output of the following Python code?
>>> a={1,2,3} >>> b=a.add(4) >>> b
a) 0
b) {1,2,3,4}
c) {1,2,3}
d) Nothing is printed
View Answer
Explanation: The method add returns nothing, hence nothing is printed.
10. What will be the output of the following Python code?
>>> a={1,2,3} >>> b=frozenset([3,4,5]) >>> a-b
a) {1,2}
b) Error as difference between a set and frozenset can’t be found out
c) Error as unsupported operand type for set data type
d) frozenset({1,2})
View Answer
Explanation: – operator gives the set of elements in set a but not in set b.
11. What will be the output of the following Python code?
>>> a={5,6,7} >>> sum(a,5)
a) 5
b) 23
c) 18
d) Invalid syntax for sum method, too many arguments
View Answer
Explanation: The second parameter is the start value for the sum of elements in set a. Thus, sum(a,5) = 5+(5+6+7)=23.
12. What will be the output of the following Python code?
>>> a={1,2,3} >>> {x*2 for x in a|{4,5}}
a) {2,4,6}
b) Error, set comprehensions aren’t allowed
c) {8, 2, 10, 4, 6}
d) {8,10}
View Answer
Explanation: Set comprehensions are allowed.
13. What will be the output of the following Python code?
>>> a={5,6,7,8} >>> b={7,8,9,10} >>> len(a+b)
a) 8
b) Error, unsupported operand ‘+’ for sets
c) 6
d) Nothing is displayed
View Answer
Explanation: Duplicate elements in a+b is eliminated and the length of a+b is computed.
14. What will be the output of the following Python code?
a={1,2,3} b={1,2,3} c=a.issubset(b) print(c)
a) True
b) Error, no method called issubset() exists
c) Syntax error for issubset() method
d) False
View Answer
Explanation: The method issubset() returns True if b is a proper subset of a.
15. Is the following Python code valid?
a={1,2,3} b={1,2,3,4} c=a.issuperset(b) print(c)
a) False
b) True
c) Syntax error for issuperset() method
d) Error, no method called issuperset() exists
View Answer
Explanation: The method issubset() returns True if b is a proper subset of a.
Sanfoundry Global Education & Learning Series – Python.
To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Apply for Programming Internship
- Check Python Books
- Check Information Technology Books
- Practice Programming MCQs
- Apply for Python Internship