Thursday, January 7, 2021

Python Set Interview questions - Multiple choice

Set Interview questions :

========================================================================

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

a) Mutable data type

b) Not Allows duplicate values

c) Data type with unordered values

d) Immutable data type


Answer: d

Explanation: 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}


Answer: a

Explanation: The argument given for the set must be 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


Answer: c

Explanation: A set doesn’t have duplicate items.


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

a) { }

b) set()

c) [ ]

d) ( )


Answer: b

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


5. 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


Answer: b

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


6. 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


Answer: d

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


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

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

>>> a.update([1, 2, 3])

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


Answer: b

Explanation: The method update adds elements to a set.


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


Answer: b

Explanation: 5 isn’t added again to the set and the set consists of only non-duplicate elements and 5 already exist 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


Answer: c

Explanation: 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


Answer: a

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


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


Answer: a

Explanation: The multiplication operator isn’t valid for the set data type.


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


Answer: a

Explanation: It is possible to compare two sets and the order of elements in both 

the sets don’t matter if the values of the elements are the same.


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

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

>>> b={5, 6, 7}

>>> a|b  # union

a) Invalid operation

b) {3, 4, 5, 6, 7}

c) {5}

d) {3,4,6,7}


Answer: d

Explanation: The operation in the above piece of code is a union operation. 

This operation produces a set of elements in both set a and set b.


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


Answer: c

Explanation: In python, elements of a set must not be mutable and sets are mutable. Thus, subsets can’t exist


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


Answer: a

Explanation: A frozenset is an 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


Answer: b

Explanation: The above piece of code is the correct syntax for creating a frozenset.


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}


Answer: b

Explanation: Since a frozen set is immutable, add method doesn’t exist for frozen method.


19. Set members must not be hashable.

a) True

b) False


Answer: b

Explanation: Set members must always be hashable.


20. 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}


Answer: a

Explanation: The method intersection_update returns a set that is an intersection of both sets.


21. 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


Answer: c

Explanation: Any change made in b is reflected in a because b is an alias of a.


22. 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


Answer: a

Explanation: In the above piece of code, b is barely a copy and not an alias of a. Hence any change made in b isn’t reflected in a.


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

>>> a={1,2,3}

>>> b=a.add(4)

>>> b


a) None

b) {1,2,3,4}

c) {1,2,3}

d) Nothing is printed


Answer: d

Explanation: The method add returns nothing, hence nothing is printed.


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


Answer: a

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


25. 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


Answer: b

Explanation: The second parameter is the start value for the sum of elements in set a. Thus, sum(a,5) = 5+(5+6+7)=23


26. 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}


Answer: c

Explanation: Set comprehensions are allowed.


13. 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


Answer: b

Explanation: Duplicate elements in a+b is eliminated and the length of a+b is computed.


14. 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) Error, no method called issubset() exists

c) Syntax error for issubset() method

d) False


Answer: a

Explanation: The method issubset() returns True if b is a proper subset of a.


15. 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

c) Syntax error for issuperset() method

d) Error, no method called issuperset() exists


Answer: a

Explanation: The method issubset() returns True if b is a proper subset of a.


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


s=set()

type(s)


a) <’set’>

b) <class ‘set’>

c) set

d) class set


Answer: b

Explanation: When we find the type of a set, the output returned is: .


2. The following Python code results in an error.


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


a) True

b) False


Answer: a

Explanation: The set data type makes use of a principle known as hashing. 

This means that each item in the set should be hashable. Hashable in this context means immutable. List is mutable and hence 

the line of code shown above will result in an error


3. Set makes use of __________

Dictionary makes use of ____________


a) keys, keys

b) key values, keys

c) keys, key values

d) key values, key values


Answer: c

Explanation: Set makes use of keys.

Dictionary makes use of key values.


4. 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}


Answer: d

Explanation: The line: s={san} will result in an error because ‘san’ is not defined. 

The line s={abs} does not result in an error because abs is a built-in function. 

The other sets shown do not result in an error because all the items are hashable.


7. 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]


Answer: a

Explanation: The list comprehension to print a list of cube of the numbers for the given list is: [x**3 for x in l].


8. 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


Answer: d

Explanation: The code shown above will result in an error because the argument given to 

the function update should necessarily be an iterable. Hence if we write this function as: s.update([4]), there will be no error.


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


a) pop

b) remove

c) update

d) sum


Answer: d

Explanation: The functions sum, min and max cannot be used on mixed type (heterogeneous) sets. 

The functions pop, remove, update etc can be used on homogenous as well as heterogeneous sets. 

An example of heterogeneous sets is: {‘abc’, 4, (1, 2)}


10. 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


Answer: b

Explanation: The function all returns true only if all the conditions given are true. 

But in the example shown above, we have 0 as a value. Hence false is returned. 

Similarly, any returns true if any one condition is true. Since the condition 4>3 is true, true is returned


11. What will be the output of the following Python code snippet?


z=set('abc$de')

'a' in z

a) True

b) False

c) No output

d) Error


Answer: a

Explanation: The code shown above is used to check whether a particular item is a part of a given set or not. 

Since ‘a’ is a part of the set z, the output is true. Note that this code would result in an error in the absence of the quotes.


12. What will be the output of the following Python code snippet?


z=set('abc')

z.add('san')

z.update(set(['p', 'q']))

z


a) {‘abc’, ‘p’, ‘q’, ‘san’}

b) {‘a’, ‘b’, ‘c’, [‘p’, ‘q’], ‘san}

c) {‘a’, ‘c’, ‘c’, ‘p’, ‘q’, ‘s’, ‘a’, ‘n’}

d) {‘a’, ‘b’, ‘c’, ‘p’, ‘q’, ‘san’}


Answer: d

Explanation: The code shown first adds the element ‘san’ to the set z. 

The set z is then updated and two more elements, namely, ‘p’ and ‘q’ are added to it. 

Hence the output is: {‘a’, ‘b’, ‘c’, ‘p’, ‘q’, ‘san’}


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


s=set([1, 2, 3])

s.union([4, 5])

s|([4, 5])


a)

   {1, 2, 3, 4, 5}

   {1, 2, 3, 4, 5}


b)

   Error

   {1, 2, 3, 4, 5}


c)

   {1, 2, 3, 4, 5}

   Error


d)

   Error

   Error

   

Answer: c

Explanation: The first function in the code shown above returns the set {1, 2, 3, 4, 5}. 

This is because the method of the function union allows any iterable. 

However the second function results in an error because of unsupported data type, that is list and set.


5. What will be the output of the following Python code snippet?


for x in set('pqr'):

print(x*2)


a)

pp

qq

rr

b)

pqr

pqr

c) ppqqrr

d) pqrpqr


Answer: a

Explanation: The code shown above prints each element of the set twice separately. Hence the output of this code is:

pp

qq

rr


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


{a**2 for a in range(4)}


a) {1, 4, 9, 16}

b) {0, 1, 4, 9, 16}

c) Error

d) {0, 1, 4, 9}


Answer: d

Explanation: The code shown above returns a set containing the square of values in the range 0-3, 

that is 0, 1, 2 and 3. Hence the output of this line of code is: {0, 1, 4, 9}.


7. What will be the output of the following Python function?


{x for x in 'abc'}

{x*3 for x in 'abc'}


a)

   {abc}

aaa

bbb

ccc


b)

abc

   abc abc abc


c)

{‘a’, ‘b’, ‘c’}

   {‘aaa’, ‘bbb’, ‘ccc’}


d)

{‘a’, ‘b’, ‘c’}

abc

abc

abc


Answer: c

Explanation: The first function prints each element of the set separately, hence the output is: {‘a’, ‘b’, ‘c’}. 

The second function prints each element of the set thrice, contained in a new set. 

Hence the output of the second function is: {‘aaa’, ‘bbb’, ‘ccc’}. (Note that the order may not be the same)


8. The output of the following code is: class<’set’>.


type({})


a) True

b) False


Answer: b

Explanation: The output of the line of code shown above is: class<’dict’>. This is because {} represents an empty dictionary, 

whereas set() initializes an empty set. Hence the statement is false.


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


a=[1, 4, 3, 5, 2]

b=[3, 1, 5, 2, 4]

a==b

set(a)==set(b)


a)

   True

   False


b)

   False

   False


c)

   False

   True


d)

True

True


Answer: c

Explanation: In the code shown above, when we check the equality of the two lists, a and b, 

we get the output false. This is because of the difference in the order of elements of the two lists. 

However, when these lists are converted to sets and checked for equality, the output is true. 

This is known as order-neutral equality. Two sets are said to be equal if and only if they contain exactly 

the same elements, regardless of order.


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


l=[1, 2, 4, 5, 2, 'xy', 4]

set(l)

l


a)

{1, 2, 4, 5, 2, ‘xy’, 4}

[1, 2, 4, 5, 2, ‘xy’, 4]


b)


{1, 2, 4, 5, ‘xy’}

[1, 2, 4, 5, 2, ‘xy’, 4]


c)


{1, 5, ‘xy’}

[1, 5, ‘xy’]


d)


{1, 2, 4, 5, ‘xy’}

[1, 2, 4, 5, ‘xy’]


Answer: b

Explanation: In the code shown above, the function set(l) converts the given list into a set. 

When this happens, all the duplicates are automatically removed. Hence the output is: {1, 2, 4, 5, ‘xy’}. 

On the other hand, the list l remains unchanged. Therefore the output is: [1, 2, 4, 5, 2, ‘xy’, 4].

Note that the order of the elements may not be the same.


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


Answer: c

Explanation: 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)}.


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


a) remove

b) pop

c) discard

d) dispose


Answer: b

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


3. 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


Answer: d

Explanation: 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).


4. 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}


Answer: a

Explanation: 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}.


5. 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


Answer: c

Explanation: 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.


6. 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)


Answer: b

Explanation: 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).


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


Answer: d

Explanation: 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}


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


Answer: c

Explanation: 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().


9. 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


Answer: a

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


10. 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’}


Answer: b

Explanation: 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’}.


Request: If you find this information useful, please provide your valuable comments.


No comments:

Post a Comment