NEW
Font size
WorksheetsPython Set
Total questions: 40
Worksheet time: 20mins
Which of these about a set is not true?
Mutable data type
Allows duplicate values
Data type with unordered values
Immutable data type
Which of the following is not the correct syntax for creating a set?
set([[1,2],[3,4]])
set([1,2,2,3,4])
set((1,2,3,4))
{1,2,3,4}
What will be the output of the following Python code?
nums = set([1,1,2,3,3,3,4,4])
print(len(nums))
7
Error, invalid syntax for formation of set
4
8
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=" ")
5 5 6
5 6 7
5 5 6 7 7 7
5 6 7 7 7
Which of the following statements is used to create an empty set?
{ }
set()
[ ]
( )
What will be the output of the following Python code?
>>> a={5,4}
>>> b={1,2,4,5}
>>> a<b
{1,2}
True
False
Invalid operation
If a={5,6,7,8}, which of the following statements is false?
print(len(a))
print(min(a))
a.remove(5)
a[2]=45
If a={5,6,7}, what happens when a.add(5) is executed?
a={5,5,6,7}
a={5,6,7}
Error as there is no add function for set data type
Error as 5 already exists in the set
What will be the output of the following Python code?
>>> a={4,5,6}
>>> b={2,8,6}
>>> a+b
{4,5,6,2,8}
{4,5,6,2,8,6}
Error as unsupported operand type for sets
Error as the duplicate item 6 is present in both sets
What will be the output of the following Python code?
>>> a={4,5,6}
>>> b={2,8,6}
>>> a-b
{4,5}
{6}
Error as unsupported operand type for set data type
Error as the duplicate item 6 is present in both sets
What will be the output of the following Python code?
>>> a={5,6,7,8}
>>> b={7,8,10,11}
>>> a^b
{5,6,7,8,10,11}
{7,8}
Error as unsupported operand type of set data type
{5,6,10,11}
What will be the output of the following Python code?
>>> s={5,6}
>>> s*3
Error as unsupported operand type for set data type
{5,6,5,6,5,6}
{5,6}
Error as multiplication creates duplicate elements which isn’t allowed
What will be the output of the following Python code?
>>> a={5,6,7,8}
>>> b={7,5,6,8}
>>> a==b
True
False
What will be the output of the following Python code?
>>> a={3,4,5}
>>> b={5,6,7}
>>> a|b
Invalid operation
{3, 4, 5, 6, 7}
{5}
{3,4,6,7}
Is the following Python code valid?
a={3,4,{7,5}}
print(a[2][0])
Yes, 7 is printed
Error, elements of a set can’t be printed
Error, subsets aren’t allowed
Yes, {7,5} is printed
Which of these about a frozenset is not true?
Mutable data type
Allows duplicate values
Data type with unordered values
Immutable data type
What is the syntax of the following Python code?
>>> a=frozenset(set([5,6,7]))
>>> a
{5,6,7}
frozenset({5,6,7})
Error, not possible to convert set into frozenset
Syntax error
Is the following Python code valid?
>>> a=frozenset([5,6,7])
>>> a
>>> a.add(5)
Yes, now a is {5,5,6,7}
No, frozen set is immutable
No, invalid syntax for add method
Yes, now a is {5,6,7}
Set members must not be hashable.
True
False
What will be the output of the following Python code?
>>> a={3,4,5}
>>> a.update([1,2,3])
>>> a
Error, no method called update for set data type
{1, 2, 3, 4, 5}
Error, list can’t be added to set
Error, duplicate item present in list
What will be the output of the following Python code?
>>> a={1,2,3}
>>> a.intersection_update({2,3,4,5})
>>> a
{2,3}
Error, duplicate item present in list
Error, no method called intersection_update for set data type
{1,4,5}
What will be the output of the following Python code?
>>> a={1,2,3}
>>> b=a
>>> b.remove(3)
>>> a
{1,2,3}
Error, copying of sets isn’t allowed
{1,2}
Error, invalid syntax for remove
What will be the output of the following Python code?
>>> a={1,2,3}
>>> b=a.copy()
>>> b.add(4)
>>> a
{1,2,3}
Error, invalid syntax for add
{1,2,3,4}
Error, copying of sets isn’t allowed
What will be the output of the following Python code?
>>> a={1,2,3}
>>> b=a.add(4)
>>> b
0
{1,2,3,4}
{1,2,3}
Nothing is printed
What will be the output of the following Python code?
>>> a={1,2,3}
>>> b=frozenset([3,4,5])
>>> a-b
{1,2}
Error as difference between a set and frozenset can’t be found out
Error as unsupported operand type for set data type
frozenset({1,2})
What will be the output of the following Python code?
>>> a={5,6,7}
>>> sum(a,5)
5
23
18
Invalid syntax for sum method, too many arguments
What will be the output of the following Python code?
>>> a={1,2,3}
>>> {x*2 for x in a|{4,5}}
{2,4,6}
Error, set comprehensions aren’t allowed
{8, 2, 10, 4, 6}
{8,10}
What will be the output of the following Python code?
>>> a={5,6,7,8}
>>> b={7,8,9,10}
>>> len(a+b)
8
Error, unsupported operand ‘+’ for sets
6
Nothing is displayed
What will be the output of the following Python code?
a={1,2,3}
b={1,2,3}
c=a.issubset(b)
print(c)
True
False
Error, no method called issubset() exists
Syntax error for issubset() method
Is the following Python code valid?
a={1,2,3}
b={1,2,3,4}
c=a.issuperset(b)
print(c)
False
True
What will be the output of the following Python code?
s=set()
type(s)
<’set’>
<class ‘set’>
set
class set
The following Python code results in an error.
s={2, 3, 4, [5, 6]}
True
False
Set makes use of __________
Dictionary makes use of ____________
keys, keys
key values, keys
keys, key values
key values, key values
Which of the following lines of code will result in an error?
s={abs}
s={4, ‘abc’, (1,2)}
s={2, 2.2, 3, ‘xyz’}
s={san}
What will be the output of the following Python code?
s={2, 5, 6, 6, 7}
s
{2, 5, 7}
{2, 5, 6, 7}
{2, 5, 6, 6, 7}
Error
Input order is preserved in sets.
True
False
Write a list comprehension for number and its cube for:
l=[1, 2, 3, 4, 5, 6, 7, 8, 9]
[x**3 for x in l]
[x^3 for x in l]
[x**3 in l]
[x^3 in l]
What will be the output of the following Python code?
s={1, 2, 3}
s.update(4)
s
{1, 2, 3, 4}
{1, 2, 4, 3}
{4, 1, 2, 3}
Error
Which of the following functions cannot be used on heterogeneous sets?
pop
remove
update
sum
What will be the output of the following Python code?
s={4>3, 0, 3-3}
all(s)
any(s)
True
False
False
True
True
True
False
False
