# 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

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}

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

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

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) ( )

{ } 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

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=45

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

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

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