Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
Filter by Categories
nmims post
Objective Type Set
Online MCQ Assignment
Question Solution
Solved Question
Uncategorized

Python MCQ Set 1

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

View Answer

Answer: a [Reason:] A frozenset is an immutable data type.

2. What is the syntax of the following piece of 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

View Answer

Answer: b [Reason:] The above piece of code is the correct syntax for creating a frozenset.

3. Is the following piece of 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}

View Answer

Answer: b [Reason:] Since a frozen set is immutable, add method doesn’t exist for frozen method.

4. Set members must not be hashable. True or False?
a) True
b) False

View Answer

Answer: b [Reason:] Set members must always be hashable.

5. What is the output of the following piece of 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

View Answer

Answer: b [Reason:] The method update adds elements to a set.

6. What is the output of the following piece of code when executed in the python shell?

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

View Answer

Answer: a [Reason:] The method intersection_update returns a set which is an intersection of both the sets.

7. What is the output for the following piece of 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

View Answer

Answer: c [Reason:] Any change made in b is reflected in a because b is an alias of a.

8. What is the output of the following piece of 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

View Answer

Answer: a [Reason:] 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.

9. What is the output of the following 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

View Answer

Answer: d [Reason:] The method add returns nothing, hence nothing is printed.

10. What is the output of the following 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})

View Answer

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

11. What is the output of the following piece of code?

>>> a={5,6,7}
>>> sum(a,5)

a) 5
b) 23
c) 18
d) Invalid syntax for sum method, too many arguments

View Answer

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

12. What is the output of the following 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}

View Answer

Answer: c [Reason:] Set comprehensions are allowed.

13. What is the output of the following piece of 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

View Answer

Answer: b [Reason:] Duplicate elements in a+b is eliminated and the length of a+b is computed.

14. What is the output of the following piece of 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

View Answer

Answer: a [Reason:] The method issubset() returns True if b is a proper subset of a.

15. Is the following piece of 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

View Answer

Answer: a [Reason:] The method issubset() returns True if b is a proper subset of a.

Python MCQ Set 2

1. What is the output of the code shown?

s=set()
type(s)

a) <’set’>
b) <class ‘set’>
c) set
d) class set

View Answer

Answer: b [Reason:] When we find the type of a set, the output returned is: .

2. The line of code shown below results in an error. State whether this statement is true or false.

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

a) True
b) False

View Answer

Answer: a [Reason:] 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

View Answer

Answer: c [Reason:] 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}

View Answer

Answer: d [Reason:] 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.

5. What is the output of the code shown below?

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

a) {2, 5, 7}
b) {2, 5, 6, 7}
c) {2, 5, 6, 6, 7}
d) Error

View Answer

Answer: b [Reason:] Duplicate values are not allowed in sets. Hence, the output of the code shown above will be a set containing the duplicate value only once. Therefore the output is: {2, 5, 6, 7}

6. Input order is preserved in sets. State whether this statement is true or false.
a) True
b) False

View Answer

Answer: b [Reason:] The input order in sets is not maintained. This is demonstrated by the code shown below: >>> s={2, 6, 8, 1, 5} >>> s {8, 1, 2, 5, 6}

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]

View Answer

Answer: a [Reason:] 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 is the output of the code shown below?

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

View Answer

Answer: d [Reason:] 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

View Answer

Answer: d [Reason:] 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 is the output of the code shown below?

s={4>3, 0, 3-3}
all(s)
any(s)

a) True
False
b) False
True
c) True
True
d) False
False

View Answer

Answer: b [Reason:] 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.

Python MCQ Set 3

1. Which of the following functions will return the symmetric difference between two sets, x and y?
a) x | y
b) x ^ y
c) x & y
d) x – y

View Answer

Answer: c [Reason:] The function x ^ y returns the symmetric difference between the two sets x and y. This is basically an XOR operation being performed on the two sets.

2. What is the output of the snippet of code shown below?

z=set('abc$de')
'a' in z

a) True
b) False
c) No output
d) Error

View Answer

Answer: a [Reason:] 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.

3. What is the output of the code shown below?

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

View Answer

Answer: d [Reason:] 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 is the output of the code shown below?

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

View Answer

Answer: c [Reason:] 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 f unsupported data type, that is list and set.

5. What is the output of the code shown below?

for x in set('pqr'):
	print(x*2)

a) pp
qq
rr
b) pqr
pqr
c) ppqqrr
d) pqrpqr

View Answer

Answer: a [Reason:] The code shown above prints each element of the set twice separately. Hence the output of this code is: pp qq rr

6. What is the output of the following code?

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

View Answer

Answer: d [Reason:] 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 is the output of each of the functions shown below?

{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

View Answer

Answer: c [Reason:] 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 line of code shown below is: class<’set’>. State whether this statement is true or false.

type({})

a) True
b) False

View Answer

Answer: b [Reason:] 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. The output of the code shown below is:

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

View Answer

Answer: c [Reason:] 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 is the output of the code shown below?

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

View Answer

Answer: b [Reason:] 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.

Python MCQ Set 4

1. What is the output of the code shown below?

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

View Answer

Answer: c [Reason:] 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

View Answer

Answer: b [Reason:] 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

View Answer

Answer: d [Reason:] 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 is the output of the code shown below?

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}

View Answer

Answer: a [Reason:] The function s1.diference(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 is the output of the following 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

View Answer

Answer: c [Reason:] 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)

View Answer

Answer: b [Reason:] 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 is the output of this 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}

View Answer

Answer: d [Reason:] 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 is the output of the code shown below?

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

View Answer

Answer: c [Reason:] 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 is the output of the line of code shown below, if s1= {1, 2, 3}?

s1.issubset(s1)

a) True
b) Error
c) No output
d) False

View Answer

Answer: a [Reason:] Every set is a subset of itself and hence the output of this line of code is true.

10. What is the output of the snippet of code shown below?

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

View Answer

Answer: b [Reason:] 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’}.

Python MCQ Set 5

1. Which type of copy is shown in this code?

l1=[[10, 20], [30, 40], [50, 60]]
ls=list(l1)
ls
[[10, 20], [30, 40], [50, 60]]

a) Shallow copy
b) Deep copy
c) memberwise
d) All of the mentioned

View Answer

Answer: a [Reason:] The code shown above depicts shallow copy. For deep copy, the command given is: l2 = l1.copyy().

2. What is the output of the code shown below?

l=[2, 3, [4, 5]]
l2=l.copy()
l2[0]=88
l
l2

a) [88, 2, 3, [4, 5]]
[88, 2, 3, [4, 5]]
b) [2, 3, [4, 5]]
[88, 2, 3, [4, 5]]
c) [88, 2, 3, [4, 5]]
[2, 3, [4, 5]]
d) [2, 3, [4, 5]]
[2, 3, [4, 5]]

View Answer

Answer: b [Reason:] The code shown above depicts deep copy. In deep copy, the base address of the objects is not copied. Hence the modification done on one list does not affect the other list.

3. In _______________ copy, the base address of the objects are copied.
In _______________ copy, the base address of the objects are not copied.
a) deep. shallow
b) memberwise, shallow
c) shallow, deep
d) deep, memberwise

View Answer

Answer: c [Reason:] In shallow copy, the base address of the objects are copied. In deep copy, the base address of the objects are not copied. Note that memberwise copy is another name for shallow copy.

4. The nested list undergoes shallow copy even when the list as a whole undergoes deep copy. State whether this statement is true or false.
a) True
b) False

View Answer

Answer: a [Reason:] A nested list undergoes shallow copy even when the list as a whole undergoes deep copy. Hence, this statement is true.

5. The output of the code shown below and state the type of copy that is depicted:

l1=[2, 4, 6, 8]
l2=[1, 2, 3]
l1=l2
l2

a) [2, 4, 6, 8], shallow copy
b) [2, 4, 6, 8], deep copy
c) [1, 2, 3], shallow copy
d) [1, 2, 3], deep copy

View Answer

Answer: c [Reason:] The code shown above depicts shallow copy and the output of the code is: [1, 2, 3].

6. What is the output of the codes shown below?

l1=[10, 20, 30]
l2=l1
id(l1)==id(l2)
 
l2=l1.copy()
id(l1)==id(l2)

a) False, False
b) False, True
c) True, True
d) True, False

View Answer

Answer: d [Reason:] The first code shown above represents shallow copy. Hence the output of the expression id(l1)==id(l2) is True. The second code depicts deep copy. Hence the output of the expression id(l1)==id(l2) in the second case is False.

7. What is the output of the code shown below?

l1=[1, 2, 3, [4]]
l2=list(l1)
id(l1)==id(l2)

a) True
b) False
c) Error
d) Address of l1

View Answer

Answer: b [Reason:] The code shown above shows a nested list. A nested list will undergo shallow copy when the list as a whole undergoes deep copy. Hence the output of this code is False.

8. What is the output of the code shown below?

l1=[10, 20, 30, [40]]
l2=copy.deepcopy(l1)
l1[3][0]=90
l1
l2

a) [10, 20, 30, [40]]
[10, 20, 30, 90]
b) Error
c) [10, 20, 30 [90]]
[10, 20, 30, [40]]
d) [10, 20, 30, [40]]
[10, 20, 30, [90]]

View Answer

Answer: c [Reason:] The code shown above depicts deep copy. Hence at the end of the code, l1=[10, 20, 30, [90]] and l2=[10, 20, 30, [40]].

9. Fill in the blanks:
In ____________________ copy, the modification done on one list affects the other list.
In ____________________ copy, the modification done on one list does not affect the other list.
a) shallow, deep
b) memberwise, shallow
c) deep, shallow
d) deep, memberwise

View Answer

Answer: a [Reason:] In shallow copy, the modification done on one list affects the other list. In deep copy, the modification done on one list does not affect the other list.

10. What is the output of the code shown below?

l1=[1, 2, 3, (4)]
l2=l1.copy()
l2
l1

a) [1, 2, 3, (4)]
[1, 2, 3, 4]
b) [1, 2, 3, 4]
[1, 2, 3, (4)]
c) [1, 2, 3, 4]
[1, 2, 3, 4]
d) [1, 2, 3, (4)]
[1, 2, 3, (4)]

View Answer

Answer: c [Reason:] In the code shown above, the list l1 is enclosed in a tuple. When we print this list, it is printed as [1, 2, 3, 4]. Note the absence of the tuple. The code shown depicts deep copy. Hence the output of this program is: l1=[1, 2, 3, 4] and l2=[1, 2, 3, 4].

11. What is the output of the piece of code given below?

def check(n):
    if n < 2:
        return n % 2 == 0
    return check(n - 2)
print(check(11))

a) False
b) True
c) 1
d) An exception is thrown

View Answer

Answer: a [Reason:] The above piece of code checks recursively whether a number is even or odd.

12. What is the base case in the MergeSort algorithm when it is solved recursively?
a) n=0
b) n=1
c) A list of length one
d) An empty list

View Answer

Answer: c [Reason:] MergeSort algorithm implements the recursive algorithm and when the recursive function receives a list of length 1 which is the base case, the list is returned.

13. What is the output of the following piece of code?

a = [1, 2, 3, 4, 5]
b = lambda x: (b (x[1:]) + x[:1] if x else []) 
print(b (a))

a) 1 2 3 4 5.
b) [5,4,3,2,1].
c) [].
d) Error, lambda functions can’t be called recursively.

View Answer

Answer: c [Reason:] The above piece of code appends the first element of the list to a reversed sublist and reverses the list using recursion.

Synopsis and Project Report

You can buy synopsis and project from distpub.com. Just visit https://distpub.com/product-category/projects/ and buy your university/institute project from distpub.com