Python Questions and Answers – Sets – 2

Python Multiple Choice Questions & Answers (MCQs) focuses on “Sets – 2”.

Q 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

Show Answer Answer:-A. Mutable data type
Explanation A frozenset is an immutable data type.


Q 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

Show Answer Answer:-B. frozenset({5,6,7})
Explanation The above piece of code is the correct syntax for creating a frozenset.


Q 3. Is the following Python code valid?

Subscribe Now: Python Newsletter | Important Subjects Newsletters
>>> 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}

Show Answer Answer:-B. No, frozen set is immutable
Explanation Since a frozen set is immutable, add method doesn’t exist for frozen method.


Q 4. Set members must not be hashable.
A. True
B. False

Show Answer Answer:-B. False
Explanation Set members must always be hashable.


Q 5. What will be the output of the following Python code?

>>> a={3,4,5}
>>> a.update([1,2,3])
>>> a
A. {1, 2, 3, 4, 5}
B. Error, no method called update for set data type
C. Error, list can’t be added to set
D. Error, duplicate item present in list

Show Answer Answer:-A. {1, 2, 3, 4, 5}
Explanation The method update adds elements to a set.


Q 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. {1,4,5}
C. Error, duplicate item present in list
D. Error, no method called intersection_update for set data type

Show Answer Answer:-A. {2,3}
Explanation The method intersection_update returns a set which is an intersection of both the sets.


Q 7. What will be the output of the following Python code?

>>> a={1,2,3}
>>> b=a
>>> b.remove(3)
>>> a
A. {1,2}
B. {1,2,3}
C. Error, copying of sets isn’t allowed
D. Error, invalid syntax for remove

Show Answer Answer:-A. {1,2}
Explanation Any change made in b is reflected in a because b is an alias of a.


Q 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. {1,2,3,4}
C. Error, invalid syntax for add
D. Error, copying of sets isn’t allowed

Show Answer Answer:-A. {1,2,3}
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.


Q 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

Show Answer Answer:-D. Nothing is printed
Explanation The method add returns nothing, hence nothing is printed.


Q 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. frozenset({1,2})
C. Error as unsupported operand type for set data type
D. Error as difference between a set and frozenset can’t be found out

Show Answer Answer:-A. {1,2}
Explanation operator gives the set of elements in set a but not in set b.


Q 11. What will be the output of the following Python code?

>>> a={5,6,7}
>>> sum(a,5)
A. 5
B. 13
C. 23
D. Invalid syntax for sum method, too many arguments

Show Answer Answer:-C. 23
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.


Q 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. {8,10}
C. {8, 2, 10, 4, 6}
D. Error, set comprehensions aren’t allowed

Show Answer Answer:-C. {8, 2, 10, 4, 6}
Explanation Set comprehensions are allowed.


Q 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. 10
C. Error, unsupported operand ‘+’ for sets
D. Nothing is displayed

Show Answer Answer:-C. Error, unsupported operand ‘+’ for sets
Explanation Duplicate elements in a+b is eliminated and the length of a+b is computed.


Q 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

Show Answer Answer:-A. True
Explanation The method issubset() returns True if b is a proper subset of a.


Q 15. Is the following Python code valid?

a={1,2,3}
b={1,2,3,4}
c=a.issuperset(b)
print(c)
A. True
B. False
C. Syntax error for issuperset() method
D. Error, no method called issuperset() exists

Show Answer Answer:-B. False
Explanation The method issubset() returns True if b is a proper subset of a.


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page