wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Python Set

Total questions: 40

Worksheet time: 20mins

Name
Class
Date
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

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}

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

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

5.

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

a)

{ }

b)

set()

c)

[ ]

d)

( )

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

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

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

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

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

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}

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

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

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}

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

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

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

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}

19.

Set members must not be hashable.

a)

True

b)

False

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

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}

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

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

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

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

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

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}

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

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)

False

c)

Error, no method called issubset() exists

d)

Syntax error for issubset() method

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

31.

What will be the output of the following Python code?

s=set()

type(s)

a)

<’set’>

b)

<class ‘set’>

c)

set

d)

class set

32.

The following Python code results in an error.

s={2, 3, 4, [5, 6]}

a)

True

b)

False

33.

Set makes use of __________

Dictionary makes use of ____________

a)

keys, keys

b)

key values, keys

c)

keys, key values

d)

key values, key values

34.

Which of the following lines of code will result in an error?

a)

s={abs}

b)

s={4, ‘abc’, (1,2)}

c)

s={2, 2.2, 3, ‘xyz’}

d)

s={san}

35.

What will be the output of the following Python code?

s={2, 5, 6, 6, 7}

s

a)

{2, 5, 7}

b)

{2, 5, 6, 7}

c)

{2, 5, 6, 6, 7}

d)

Error

36.

Input order is preserved in sets.

a)

True

b)

False

37.

Write a list comprehension for number and its cube for:

l=[1, 2, 3, 4, 5, 6, 7, 8, 9]

a)

[x**3 for x in l]

b)

[x^3 for x in l]

c)

[x**3 in l]

d)

[x^3 in l]

38.

What will be the output of the following Python code?

s={1, 2, 3}

s.update(4)

s

a)

{1, 2, 3, 4}

b)

{1, 2, 4, 3}

c)

{4, 1, 2, 3}

d)

Error

39.

Which of the following functions cannot be used on heterogeneous sets?

a)

pop

b)

remove

c)

update

d)

sum

40.

What will be the output of the following Python code?

s={4>3, 0, 3-3}

all(s)

any(s)

a)

True

False

b)

False

True

c)

True

True

d)

False

False