Q: Which of these about a set is not true?
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?
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))
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=" ")
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?
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
Solution: a
Q: If a={5,6,7,8}, which of the following statements is false?
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?
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
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
Solution: operator gives the set of elements in set a but not in set b.
You Have Score    | /10 |