Python MCQs on SET: SET 2

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

>>> a={5,6,7,8}

>>> b={7,8,10,11}

>>> a^b

a) {5,6,7,8,10,11}

b) {7,8}

c) Error as unsupported operand type of set data type

d) {5,6,10,11}

Check Answer

Answer: d
^ operator returns a set of elements in set A or set B, but not in both (symmetric difference).

12. What will be the output of the following Python code?

>>> s={5,6}

>>> s*3

a) Error as unsupported operand type for set data type

b) {5,6,5,6,5,6}

c) {5,6}

d) Error as multiplication creates duplicate elements which isn’t allowed

Check Answer

Answer: a
The multiplication operator isn’t valid for the set data type.

13. What will be the output of the following Python code?

>>> a={5,6,7,8}

>>> b={7,5,6,8}

>>> a==b

a) True

b) False

Check Answer

Answer: a
It is possible to compare two sets and the order of elements in both the sets doesn’t matter if the values of the elements are the same.

14. What will be the output of the following Python code?

>>> a={3,4,5}

>>> b={5,6,7}

>>> a|b

a) Invalid operation

b) {3, 4, 5, 6, 7}

c) {5}

d) {3,4,6,7}

Check Answer

Answer: b
The operation in the above piece of code is union operation. This operation produces a set of elements in both set a and set b.

15. Is the following Python code valid?

a={3,4,{7,5}}

print(a[2][0])

a) Yes, 7 is printed

b) Error, elements of a set can’t be printed

c) Error, subsets aren’t allowed

d) Yes, {7,5} is printed

Check Answer

Answer: c
In python, elements of a set must not be mutable and sets are mutable. Thus, subsets can’t exist.

16. 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

Check Answer

Answer: a
A frozenset is an immutable data type.

17. 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

Check Answer

Answer: b
The above piece of code is the correct syntax for creating a frozenset.

18. 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}

Check Answer

Answer: b
Since a frozen set is immutable, add method doesn’t exist for frozen method.

19. Set members must not be hashable.

a) True

b) False

Check Answer

Answer: b
Set members must always be hashable.

20. 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

Check Answer

Answer: b
The method update adds elements to a set.

Page  1  2  3  4  5  6

Python Interview Questions (MCQs)

We have divided these Python Questions and Answers into various parts based on the topics. Open the Topic of your choice and Practice these MCQs.

Python MCQs on Variables and Operators Python MCQs on Precedence and Associativity
Python MCQs on Data Type Python MCQs on Boolean
Python MCQs on Bitwise Operators Python MCQs on Formatting and Advance Formatting
Python MCQs on Decorators Python MCQs on While and For Loops
Python MCQs on List Python MCQs on List Comprehension
Python MCQs on String Python MCQs on Tuple
Python MCQs on SET Python MCQs on Dictionary
Python MCQs on Functions Python MCQs on Argument Parsing
Python MCQs on Global and Local Variables Python MCQs on Recursion
Python MCQs on Mapping Functions Python MCQs on Modules
Python MCQs on Regular Expressions Python MCQs on Files
Python MCQs on Overloading Python MCQs on Classes and Objects
Python MCQs on Inheritance Python MCQs on Polymorphism and Encapsulation
Python MCQs on Exception Handling

Leave a Comment