Python Set MCQ : Set 2

Python Set MCQ

1). Which method is used to return the minimum element from a set?

a) min()
b) minimum()
c) smallest()
d) get_min()

Correct answer is: a) min()
Explanation: The min() function is used to return the minimum element from a set.

2). Which method is used to calculate the sum of elements in a set?

a) sum()
b) total()
c) calculate_sum()
d) get_sum()

Correct answer is: a) sum()
Explanation: The sum() function is used to calculate the sum of elements in a set.

3). Which method is used to find the union of multiple sets?

a) union()
b) combine()
c) merge()
d) unite()

Correct answer is: a) union()
Explanation: The union() method is used to find the union of multiple sets.

4). Which method is used to find the intersection of multiple sets?

a) intersection()
b) common()
c) intersect()
d) same()

Correct answer is: a) intersection()
Explanation: The intersection() method is used to find the intersection of multiple sets.

5). Which method is used to find the difference between multiple sets?

a) difference()
b) subtract()
c) diff()
d) minus()

Correct answer is: a) difference()
Explanation: The difference() method is used to find the difference between multiple sets.

6). Which method is used to find the symmetric difference between multiple sets?

a) symmetric_difference()
b) symmetric_diff()
c) symmetric()
d) diff_symmetric()

Correct answer is: a) symmetric_difference()
Explanation: The symmetric_difference() method is used to find the symmetric difference between multiple sets.

7). Which method is used to update a set with the union of itself and another set?

a) update()
b) add()
c) append()
d) merge()

Correct answer is: a) update()
Explanation: The update() method is used to update a set with the union of itself and another set.

8). Which method is used to remove an element from a set?

a) remove()
b) delete()
c) discard()
d) pop()

Correct answer is: a) remove()
Explanation: The remove() method is used to remove an element from a set.

9). Which method is used to remove an element from a set without raising an error if the element is not present?

a) remove()
b) delete()
c) discard()
d) pop()

Correct answer is: c) discard()
Explanation: The discard() method removes an element from a set, but if the element is not present, no error is raised.

10). Which method is used to check if two sets are disjoint (have no common elements)?

a) is_disjoint()
b) has_common()
c) check_disjoint()
d) is_empty()

Correct answer is: a) is_disjoint()
Explanation: The is_disjoint() method is used to check if two sets have no common elements.

11). Which method is used to check if a set is a superset of another set?

a) is_superset()
b) is_super()
c) superset()
d) issubset()

Correct answer is: a) is_superset()
Explanation: The is_superset() method is used to check if a set is a superset of another set.

12). Which method is used to check if two sets are equal?

a) is_equal()
b) equal()
c) equals()
d) == operator

Correct answer is: d) == operator
Explanation: The == operator is used to check if two sets are equal.

13). Which method is used to remove and return an arbitrary element from a set?

a) remove()
b) delete()
c) pop()
d) extract()

Correct answer is: c) pop()
Explanation: The pop() method removes and returns an arbitrary element from a set.

14). What is the output of the following code?

a = {1, 2, 3, 5, 6}
print(“Original set: “, a)
a.add(7)
print(“New set: “, a)

a) Original set: {1, 2, 3, 5, 6}, New set: {1, 2, 3, 5, 6}
b) Original set: {1, 2, 3, 5, 6}, New set: {1, 2, 3, 5, 6, 7}
c) Original set: {1, 2, 3, 5, 6}, New set: {7}
d) Original set: {1, 2, 3, 5, 6}, New set: {1, 2, 3, 5, 6, 7, 7}

Correct answer is: b) Original set: {1, 2, 3, 5, 6}, New set: {1, 2, 3, 5, 6, 7}
Explanation: The original set `a` contains the elements {1, 2, 3, 5, 6}. The `add()` method is used to add the element 7 to the set `a`. After adding 7, the new set becomes {1, 2, 3, 5, 6, 7}. Therefore, the output is “Original set: {1, 2, 3, 5, 6}, New set: {1, 2, 3, 5, 6, 7}”.

15). What is the output of the following code?

a = {1, 2, 3, 5, 6}
print(“Original set: “, a)
a.remove(5)
print(“New set: “, a)

a) Original set: {1, 2, 3, 5, 6}
New set: {1, 2, 3, 6}
b) Original set: {1, 2, 3, 5, 6}
New set: {1, 2, 3, 5, 6}
c) Original set: {1, 2, 3, 5, 6}
New set: {1, 2, 3}
d) Original set: {1, 2, 3, 5, 6}
New set: {1, 2, 3, 4, 6}

Correct answer is: a) Original set: {1, 2, 3, 5, 6}
New set: {1, 2, 3, 6}
Explanation: The original set `a` contains the elements 1, 2, 3, 5, and 6. The `remove(5)` method is used to remove the element 5 from the set. After removal, the new set `a` contains the elements 1, 2, and 3. Therefore, the output is “Original set: {1, 2, 3, 5, 6}” and “New set: {1, 2, 3, 6}”.

16). What is the output of the following code?

a = {1, 2, 3, 5, 6}
print(“Original set: “, a)
print(“Length of given set: “, len(a))

a) Original set: {1, 2, 3, 5, 6}; Length of given set: 5
b) Original set: {1, 2, 3, 5, 6}; Length of given set: 6
c) Original set: {1, 2, 3, 5, 6}; Length of given set: 4
d) Original set: {1, 2, 3, 5, 6}; Length of given set: 3

Correct answer is: a) Original set: {1, 2, 3, 5, 6}; Length of given set: 5
Explanation: The set `a` contains the elements 1, 2, 3, 5, and 6. When printing the original set, it will display as `{1, 2, 3, 5, 6}`. The `len(a)` function returns the length of the set, which is 5 in this case.

17). What is the output of the following code?

a = {1,2,3,5,6}
print(“Original set: “,a)
if 5 in a:
    print(“True”)
else:
    print(“False”)

a) Original set: {1, 2, 3, 5, 6}\nTrue
b) Original set: {1, 2, 3, 5, 6}\nFalse
c) Original set: {1, 2, 3, 5, 6}
d) True

Correct answer is: a) Original set: {1, 2, 3, 5, 6}\nTrue
Explanation: The code defines a set “a” with the elements {1, 2, 3, 5, 6}. The print statement displays the original set as “Original set: {1, 2, 3, 5, 6}”. The if statement checks if the number 5 is present in the set. Since 5 is indeed an element of the set, the code block under the if statement executes, which prints “True”. Hence, the output will be “Original set: {1, 2, 3, 5, 6}\nTrue” (where “\n” represents a new line).

18). What is the output of the following code?

a = {1, 2, 4, 5}
b = {7, 8, 9, 1}
print(“Original set1: “, a)
print(“Original set2: “, b)
print(“Union of a and b: “, a.union(b))

a) Original set1: {1, 2, 4, 5} Original set2: {7, 8, 9, 1} Union of a and b: {1, 2, 4, 5, 7, 8, 9}
b) Original set1: {1, 2, 4, 5} Original set2: {7, 8, 9, 1} Union of a and b: {1, 2, 4, 5}
c) Original set1: {1, 2, 4, 5} Original set2: {7, 8, 9, 1} Union of a and b: {7, 8, 9}
d) Original set1: {1, 2, 4, 5} Original set2: {7, 8, 9, 1} Union of a and b: {1, 7, 2, 4, 5, 8, 9}

Correct answer is: a) Original set1: {1, 2, 4, 5} Original set2: {7, 8, 9, 1} Union of a and b: {1, 2, 4, 5, 7, 8, 9}
Explanation: The code creates two sets, `a` and `b`, with specific elements. The `union()` method is then called on set `a` with `b` as an argument. The `union()` method returns a new set that contains all unique elements from both sets. In this case, the union of `a` and `b` will be {1, 2, 4, 5, 7, 8, 9}, which will be printed as the output.

20). What is the output of the code?

a = {1,2,4,5}
b = {7,8,9,1}
print(“Original set1: “,a)
print(“Original set2: “,b)
print(“Intersection of a and b: “,a.intersection(b))

a) Original set1: {1, 2, 4, 5} Original set2: {7, 8, 9, 1} Intersection of a and b: {1}
b) Original set1: {1, 2, 4, 5} Original set2: {7, 8, 9, 1} Intersection of a and b: {1, 7, 8, 9}
c) Original set1: {1, 2, 4, 5} Original set2: {7, 8, 9, 1} Intersection of a and b: {2, 4, 5}
d) Original set1: {1, 2, 4, 5} Original set2: {7, 8, 9, 1} Intersection of a and b: {7, 8, 9}

Correct answer is: a) Original set1: {1, 2, 4, 5} Original set2: {7, 8, 9, 1} Intersection of a and b: {1}
Explanation: The intersection of sets a and b is the set of elements that are common to both sets. In this case, the intersection is {1}, as 1 is present in both sets a and b. The intersection() method is used to find the intersection of sets. The code correctly prints the original sets and the intersection set.

21). What is the output of the above code?

a = {1,2,4,5}
b = {7,8,9,1}
print(“Original set1: “,a)
print(“Original set2: “,b)
print(“Difference of a and b: “,a.difference(b))

a) Original set1: {1, 2, 4, 5}
Original set2: {7, 8, 9, 1}
Difference of a and b: {2, 4, 5}
b) Original set1: {1, 2, 4, 5}
Original set2: {7, 8, 9, 1}
Difference of a and b: {1, 2, 4, 5}
c) Original set1: {1, 2, 4, 5}
Original set2: {7, 8, 9, 1}
Difference of a and b: {2, 4, 5}
d) Original set1: {1, 2, 4, 5}
Original set2: {7, 8, 9, 1}
Difference of a and b: {}

Correct answer is: c) Original set1: {1, 2, 4, 5}
Original set2: {7, 8, 9, 1}
Difference of a and b: {2, 4, 5}
Explanation: The difference() method returns a new set containing the elements that are present in set ‘a’ but not in set ‘b’.

22). What is the output of the above code?

a = {1,2,4,5}
b = {7,8,9,1}
print(“Original set1: “,a)
print(“Original set2: “,b)
print(“Symmetric difference of a and b: “,a.symmetric_difference(b))

a) Original set1: {1, 2, 4, 5} Original set2: {7, 8, 9, 1} Symmetric difference of a and b: {2, 4, 5, 7, 8, 9}
b) Original set1: {1, 2, 4, 5} Original set2: {7, 8, 9, 1} Symmetric difference of a and b: {2, 4, 5}
c) Original set1: {1, 2, 4, 5} Original set2: {7, 8, 9, 1} Symmetric difference of a and b: {1, 7, 8, 9}
d) Original set1: {1, 2, 4, 5} Original set2: {7, 8, 9, 1} Symmetric difference of a and b: {1}

Correct answer is: a) Original set1: {1, 2, 4, 5} Original set2: {7, 8, 9, 1} Symmetric difference of a and b: {2, 4, 5, 7, 8, 9}
Explanation: The symmetric_difference() method returns a new set that contains elements present in either set a or set b, but not in both.

Leave a Comment