Python Set Question 1 What is a set in python? A collection of ordered elements. A collection of key-value pairs. A collection of unique elements. A collection of elements with duplicates. Question 2 Which of the following is a valid way to create an empty set? s=set() s={} s=() All of the above. Question 3 What is the output of following code snippet? s=set()print(len(s)) 0 1 Error None Question 4 How do you add an element to a set s? s.append(x) s.add(x) s.insert(x) s.update(x) Question 5 Which of the following is true about sets in python? Sets are ordered collections. Sets allow duplicate elements. Sets are mutable. Sets are iterable. Question 6 What happens if you try to add a duplicate element to a set? The set becomes empty. The duplicate element is ignored. An error is raised. The original element is replaced. Question 7 How do you remove an element x from a set s? s.remove(x) s.delete(x) s.discard(x) s.pop(x) Question 8 Which method is used to clear all elements from a set s? s.clear( ) s.empty( ) s.remove_all( ) s.delete_all( ) Question 9 What is the result of following code snippet? set1 & set2 Union of set1 and set2. Intersection of set1 and set2. Difference of set1 and set2. Symmetric difference of set1 and set2. Question 10 What is the purpose of union() in sets? To find the common elements between two sets. To combine two sets without duplicates. To remove elements from a set. To find the difference between two sets. Question 11 How do you check if a set set1 is a subset of another set set2? set1.issuperset(set2) set1.issubset(set2) set1.subset(set2) set1.includes(set2) Question 12 What is the output of following code snippet? length = len(set([1,2,3,4,5] ))print(length) 5 1 Error None Question 13 Which of the following is a set comprehension in python? {x for x in range(10)} [x for x in range(10)] (x for x in range(10)) None of the above. Question 14 How can you find the difference between two sets set1 and set2? set1-set2 set1|set2 set1&set2 set1^set2 Question 15 What is the output of following code snippet? s=set('hello')print(s) {h, e, l, l, o,} {hello} {h, e, l, o} {h, e, l, l, l, o} Question 16 Which of the following methods can be used to remove and return an arbitrary element from a set? pop() remove() discard() clear() Question 17 What is the output of following code snippet? s=set('hello')|set('world')print(s) {hello, world} {'e', 'r', 'h', 'l', 'o', 'w', 'd'} {h, e, l, o} d) {w, r, o, l, d} Question 18 Which operator is used for symmetric difference between two sets? & - I ^ Question 19 How do you check if a specific element x is in set s? x in s s.contains(x) s.has(x) s.find(x) Question 20 Which of the following statements is true about frozensets in python? Frozensets are mutable. Frozensets are hashable. Frozensets allow duplicate elements. Frozensets have a add() method. Question 21 How do you create a frozenset in python? f=frozenset() f=set() f={} f=set([]) Question 22 What is the output of following code snippet? s=frozenset(hello)print(s) {h, e, l, l, o} {hello} {h, e, l, o} {h, e, l, l, l, o} Question 23 Can you add elements to a frozenset after its created? Yes No Question 24 Which of the following methods can be used to perform a union operation on two sets? union() intersection() difference() symmetric_difference() Question 25 What is the output of following code snippet? s=set(apple)-set(pineapple)print(s) set() {pine} {p, l, e} {apple} Question 26 How do you find the maximum element in a set s? max(s) s.maximum() s.max() s.largest() Question 27 What is the output of set following code snippet? s=set(abc) & set(bcd)print(s) {abc} {b, c} {bc} {a, b, c, d} Question 28 How do you remove and return an arbitrary element from a set s while raising an error if the set is empty? s.discard() s.pop() s.remove() s.get() Question 29 Which of the following is a valid set in python? {1, 2, [3, 4]} {1, 2, (3, 4)} {{1, 2}, {3, 4}} {{1, 2}, [3, 4]} Question 30 What is the output of following code snippet? s=set([1, 2, 3])print(s) {1, 2, 3} {[1, 2, 3]} Error None Question 31 Which of the following methods can be used to check if two sets have no elements in common? isdisjoint() issubset() issuperset() intersection() Question 32 How do you find the sum of all elements in a set s? sum(s) s.sum() s.reduce() s.total() Question 33 Which operator is used to check if one set is a superset of another set? > < >= <= Question 34 What is the output of following code snippet? s=set(hello)^set(world)print(s) {hello, world} {w, d, r, e, h} {h, e, l, o} {w, r, o, l, d} Question 35 How do you find the difference between two sets set1 and set2 using the difference() method? set1.difference(set2) set1-set2 set1|set2 set1&set2 Question 36 Which method is used to remove a specific element x from a set s without raising an error if the element is not present? s.remove(x) s.delete(x) s.discard(x) s.pop(x) Question 37 What is the output of following code snippet? s=set(abc)-set(bcd)print(s) {a} {b,c} {bc} {a, b, c, d} Question 38 How do you find the maximum element in a set s? max(s) s.maximum() s.max() s.largest() Question 39 Which of the following is a valid frozenset in python? frozenset{1, 2, [3, 4]} frozenset{1, 2, (3, 4)} frozenset{{1, 2}, {3, 4}} frozenset{{1, 2}, [3, 4]} Question 40 What is the output of following code snippet? s=frozenset(1, 2, 3)print(s) {1, 2, 3} {[1, 2, 3]} Error None Question 41 How do you check if a specific element x is in frozenset f? x in f f.contains(x) f.has(x) f.find(x) Question 42 Which of the following methods can be used to check if two frozensets have no elements in common? isdisjoint() issubset() issuperset() intersection() Question 43 How do you find the sum of all elements in a frozenset f? sum(f) f.sum() f.reduce() f.total() Question 44 Which operator is used to check if one frozenset is a superset of another frozenset? > < >= <= Question 45 What is the output of following code snippet? f =frozenset(hello)^frozenset(world)print(s) {hello, world} frozenset({'w', 'd', 'r', 'e', 'h'}) {h, e, l, o} {w, r, o, l, d} Question 46 How do you create a set having 1, 2 and 3 elements? s=set([1,2,3]) s={1,2,3} s=set((1,2,3)) All of the above. Question 47 What is the output of len(set()) when s is an empty set? 0 1 Error None Question 48 How do you add an element to a set s? s.append(x) s.add(x) s.insert(x) s.update(x) Question 49 What is the result of following code snippet? s={1,2,3}^{2,3,4}print(s) {1,2,3} {1,4} {2,3,4} {1} Question 50 What is the result of following code snippet? nums=set([1,1,2,3,3,3,4,4]) print(len(nums)) 7 8 Error, invalid syntax for formation of set. 4 Name Email Phone