Python MCQs on SET: SET 6

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

s1={3, 4}

s2={1, 2}

s3=set()

i=0

j=0

for i in s1:

for j in s2:

s3.add((i,j))

i+=1

j+=1

print(s3)

a) {(3, 4), (1, 2)}

b) Error

c) {(4, 2), (3, 1), (4, 1), (5, 2)}

d) {(3, 1), (4, 2)}

Check Answer

Answer: c
The code shown above finds the Cartesian product of the two sets, s1 and s2. The Cartesian product of these two sets is stored in a third set, that is, s3. Hence the output of this code is: {(4, 2), (3, 1), (4, 1), (5, 2)}.

52. The ____________ function removes the first element of a set and the last element of a list.

a) remove

b) pop

c) discard

d) dispose

Check Answer

Answer: b
The function pop removes the first element when used on a set and the last element when used to a list.

53. The difference between the functions discard and remove is that:

a) Discard removes the last element of the set whereas remove removes the first element of the set

b) Discard throws an error if the specified element is not present in the set whereas remove does not throw an error in case of absence of the specified element

c) Remove removes the last element of the set whereas discard removes the first element of the set

d) Remove throws an error if the specified element is not present in the set whereas discard does not throw an error in case of absence of the specified element.

 

Check Answer

Answer: d
The function remove removes the element if it is present in the set. If the element is not present, it throws an error. The function discard removes the element if it is present in the set. If the element is not present, no action is performed (Error is not thrown).

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

s1={1, 2, 3}

s2={3, 4, 5, 6}

s1.difference(s2)

s2.difference(s1)

a)

{1, 2}
{4, 5, 6}

b)

{1, 2}
{1, 2}

c)

{4, 5, 6}
{1, 2}

d)

{4, 5, 6}
{4, 5, 6}

Check Answer

Answer: a
The function s1.difference(s2) returns a set containing the elements which are present in the set s1 but not in the set s2. Similarly, the function s2.difference(s1) returns a set containing elements which are present in the set s2 but not in the set s1. Hence the output of the code shown above will be:
{1, 2}
{4, 5, 6}.

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

s1={1, 2, 3}

s2={4, 5, 6}

s1.isdisjoint(s2)

s2.isdisjoint(s1)

a)

True
False

b)

False
True

c)

True
True

d)

False
False

Check Answer

Answer: c
The function isdisjoint returns true the two sets in question are disjoint, that is if they do not have even a single element in common. The two sets s1 and s2 do not have any elements in common, hence true is returned in both the cases.

56. If we have two sets, s1 and s2, and we want to check if all the elements of s1 are present in s2 or not, we can use the function:

a) s2.issubset(s1)

b) s2.issuperset(s1)

c) s1.issuperset(s2)

d) s1.isset(s2)

Check Answer

Answer: b
Since we are checking whether all the elements present in the set s1 are present in the set s2. This means that s1 is the subset and s1 is the superset. Hence the function to be used is: s2.issuperset(s1). This operation can also be performed by the function: s1.issubset(s2).

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

s1={1, 2, 3, 8}

s2={3, 4, 5, 6}

s1|s2

s1.union(s2)

a)

{3}
{1, 2, 3, 4, 5, 6, 8}

b)

{1, 2, 4, 5, 6, 8}
{1, 2, 4, 5, 6, 8}

c)

{3}
{3}

d)

{1, 2, 3, 4, 5, 6, 8}
{1, 2, 3, 4, 5, 6, 8}

Check Answer

Answer: d
The function s1|s2 as well as the function s1.union(s2) returns a union of the two sets s1 and s2. Hence the output of both of these functions is: {1, 2, 3, 4, 5, 6, 8}.

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

a=set(‘abc’)

b=set(‘def’)

b.intersection_update(a)

a

b

a)

set()
(‘e’, ‘d’, ‘f’}

b)

{}
{}

c)

{‘b’, ‘c’, ‘a’}
set()

d)

set()
set()

Check Answer

Answer: c
The function b.intersection_update(a) puts those elements in the set b which are common to both the sets a and b. The set a remains as it is. Since there are no common elements between the sets a and b, the output is:
‘b’, ‘c’, ‘a’}
set().

59. What will be the output of the following Python code, if s1= {1, 2, 3}?

s1.issubset(s1)

a) True

b) Error

c) No output

d) False

Check Answer

Answer: a
Every set is a subset of itself and hence the output of this line of code is true.

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

x=set(‘abcde’)

y=set(‘xyzbd’)

x.difference_update(y)

x

y

a)

{‘a’, ‘b’, ‘c’, ‘d’, ‘e’}
{‘x’, ‘y’, ‘z’}

b)

{‘a’, ‘c’, ‘e’}
{‘x’, ‘y’, ‘z’, ‘b’, ‘d’}

c)

{‘b’, ‘d’}
{‘b’, ‘d’}

d)

{‘a’, ‘c’, ‘e’}
{‘x’, ‘y’, ‘z’}

Check Answer

Answer: b
The function x.difference_update(y) removes all the elements of the set y from the set x. Hence the output of the code is:
{‘a’, ‘c’, ‘e’}
{‘x’, ‘y’, ‘z’, ‘b’, ‘d’}.

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