Python MCQs on SET: SET 1

Python MCQs on SET

In this section, we are going to introduce you to the most frequently asked Python MCQs on SET. These Python MCQs on SET will be going to help you in Interviews and Exams.

 

1. Which of these about a set is not true?

a) Mutable data type

b) Allows duplicate values

c) Data type with unordered values

d) Immutable data type

Check Answer

Answer: d
A set is a mutable data type with non-duplicate, unordered values, providing the usual mathematical set operations.

2. Which of the following is not the correct syntax for creating a set?

a) set([[1,2],[3,4]])

b) set([1,2,2,3,4])

c) set((1,2,3,4))

d) {1,2,3,4}

Check Answer

Answer: a
The argument given for the set must be an iterable.

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

nums = set([1,1,2,3,3,3,4,4])

print(len(nums))

a) 7

b) Error, invalid syntax for formation of set

c) 4

d) 8

Check Answer

Answer: c
A set doesn’t have duplicate items.

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

a = [5,5,6,7,7,7]

b = set(a)

def test(lst):

if lst in b:

return 1

else:

return 0

for i in filter(test, a):

print(i,end=” “)

a) 5 5 6

b) 5 6 7

c) 5 5 6 7 7 7

d) 5 6 7 7 7

Check Answer

Answer: c
The filter function will return all the values from list a which are true when passed to function test. Since all the members of the set are non-duplicate members of the list, all of the values will return true. Hence all the values in the list are printed.

5. Which of the following statements is used to create an empty set?

a) { }

b) set()

c) [ ]

d) ( )

Check Answer

Answer: b
{ } creates a dictionary not a set. Only set() creates an empty set.

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

>>> a={5,4}

>>> b={1,2,4,5}

>>> a<b

a) {1,2}

b) True

c) False

d) Invalid operation

Check Answer

Answer: b
a<b returns True if a is a proper subset of b.

7. If a={5,6,7,8}, which of the following statements is false?

a) print(len(a))

b) print(min(a))

c) a.remove(5)

d) a[2]=45

Check Answer

Answer: d
The members of a set can be accessed by their index values since the elements of the set are unordered.

8. If a={5,6,7}, what happens when a.add(5) is executed?

a) a={5,5,6,7}

b) a={5,6,7}

c) Error as there is no add function for set data type

d) Error as 5 already exists in the set

Check Answer

Answer: b
There exists add method for set data type. However 5 isn’t added again as set consists of only non-duplicate elements and 5 already exists in the set. Execute in python shell to verify.

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

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

>>> b={2,8,6}

>>> a+b

a) {4,5,6,2,8}

b) {4,5,6,2,8,6}

c) Error as unsupported operand type for sets

d) Error as the duplicate item 6 is present in both sets

Check Answer

Answer: c
Execute in python shell to verify.

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

>>> a={4,5,6}
>>> b={2,8,6}
>>> a-b

a) {4,5}

b) {6}

c) Error as unsupported operand type for set data type

d) Error as the duplicate item 6 is present in both sets

Check Answer

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

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