Test Series - python

Test Number 50/108

Q: 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
Solution: A set is a mutable data type with non-duplicate, unordered values, providing the usual mathematical set operations.
Q: 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}
Solution: The argument given for the set must be an iterable.
Q: 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
Solution: A set doesn’t have duplicate items.
Q: 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
Solution: 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.
Q: Which of the following statements is used to create an empty set?
A. { }
B. set()
C. [ ]
D. ( )
Solution: { } creates a dictionary not a set. Only set() creates an empty set.
Q: What will be the output of the following Python code?

>>> a={5,4}
>>> b={1,2,4,5}
>>> a
A. {1,2}
B. True
C. False
D. Invalid operation
Solution: a
Q: 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
Solution: The members of a set can be accessed by their index values since the elements of the set are unordered.
Q: 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
Solution: 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.
Q: 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
Solution: Execute in python shell to verify.
Q: 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
Solution: operator gives the set of elements in set a but not in set b.

You Have Score    /10