Python MCQs on SET: SET 3

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

Check Answer

Answer: a
The method intersection_update returns a set which is an intersection of both the sets.

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

Check Answer

Answer: c
Any change made in b is reflected in a because b is an alias of a.

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

Check Answer

Answer: a
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.

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

Check Answer

Answer: d
The method add returns nothing, hence nothing is printed.

25. 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})

Check Answer

Answer: a
– operator gives the set of elements in set a but not in set b.

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

Check Answer

Answer: b
The second parameter is the start value for the sum of elements in set a. Thus, sum(a,5) = 5+(5+6+7)=23.

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

Check Answer

Answer: c
Set comprehensions are allowed.

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

Check Answer

Answer: b
Duplicate elements in a+b is eliminated and the length of a+b is computed.

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

Check Answer

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

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

Check Answer

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

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