Python Set MCQ : Set 3

Python Set MCQ

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

List = [1, 2, 3, 4, 5]
list_set = set(List)
print(“Original list: “, List)
print(“List to set: “, list_set)

a) Original list: [1, 2, 3, 4, 5] List to set: {1, 2, 3, 4, 5}
b) Original list: {1, 2, 3, 4, 5} List to set: [1, 2, 3, 4, 5]
c) Original list: [1, 2, 3, 4, 5] List to set: [1, 2, 3, 4, 5]
d) Original list: {1, 2, 3, 4, 5} List to set: {1, 2, 3, 4, 5}

Correct answer is: a) Original list: [1, 2, 3, 4, 5] List to set: {1, 2, 3, 4, 5}
Explanation: The variable `List` is initialized with the values [1, 2, 3, 4, 5]. The set() function is used to convert the list into a set. The resulting set will contain unique elements from the list. The print statements display the original list and the set obtained from the list.

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

Set = {1, 2, 3, 4, 5}
set_list = list(Set)
print(“Original set: “, Set)
print(“set to list: “, set_list)

a) Original set: {1, 2, 3, 4, 5} set to list: [1, 2, 3, 4, 5]
b) Original set: {1, 2, 3, 4, 5} set to list: {1, 2, 3, 4, 5}
c) Original set: {1, 2, 3, 4, 5} set to list: (1, 2, 3, 4, 5)
d) Error: NameError: name ‘List’ is not defined

Correct answer is: a) Original set: {1, 2, 3, 4, 5} set to list: [1, 2, 3, 4, 5]
Explanation: The code initializes a set called “Set” with values 1, 2, 3, 4, and 5. It then converts the set into a list using the `list()` function and assigns it to the variable “set_list.” The first `print()` statement outputs the original set, which is {1, 2, 3, 4, 5}. The second `print()` statement outputs the converted list, which is [1, 2, 3, 4, 5].

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

Set = {10, 23, 45, 66, 96, 83}
print(“Original set1: “, Set)
maximum = 0
for ele in Set:
    if ele > maximum:
        maximum = ele
print(“Maximum value: “, maximum)

a) Original set1: {10, 23, 45, 66, 96, 83} Maximum value: 96
b) Original set1: {10, 23, 45, 66, 96, 83} Maximum value: 83
c) Original set1: {10, 23, 45, 66, 96, 83} Maximum value: 66
d) Original set1: {10, 23, 45, 66, 96, 83} Maximum value: 45

Correct answer is: a) Original set1: {10, 23, 45, 66, 96, 83} Maximum value: 96
Explanation: The code initializes a variable “maximum” to 0 and iterates through the elements of the set “Set”. It compares each element with the current maximum value and updates “maximum” if a larger element is found. The final value of “maximum” is the maximum value in the set, which is 96.

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

Set = {10, 23, 45, 66, 96, 83}
print(“Original set1: “, Set)
print(“Minimum value: “, min(Set))

a) Original set1: {10, 23, 45, 66, 96, 83}
Minimum value: 10
b) Original set1: {10, 23, 45, 66, 96, 83}
Minimum value: 96
c) Original set1: {10, 23, 45, 66, 96, 83}
Minimum value: 83
d) Error: min() function cannot be used on sets

Correct answer is: a) Original set1: {10, 23, 45, 66, 96, 83}
Minimum value: 10
Explanation: The code creates a set called “Set” with elements 10, 23, 45, 66, 96, and 83. It then prints the original set using the “print” statement. Finally, it uses the “min” function to find the minimum value in the set, which is 10.

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

Set = {1, 2, 3, 4, 5}
total = 0
print(“Original set1: “, Set)
for ele in Set:
    total += ele
print(“Total of elements in the set: “, total)

a) Original set1: {1, 2, 3, 4, 5}
Total of elements in the set: 15
b) Original set1: {1, 2, 3, 4, 5}
Total of elements in the set: 10
c) Original set1: {1, 2, 3, 4, 5}
Total of elements in the set: 14
d) Original set1: {1, 2, 3, 4, 5}
Total of elements in the set: 5

Correct answer is: a) Original set1: {1, 2, 3, 4, 5}
Total of elements in the set: 15
Explanation: The code initializes a set named “Set” with elements 1, 2, 3, 4, and 5. It also initializes a variable “total” as 0. The code then enters a loop where it iterates over each element “ele” in the set. Inside the loop, the value of “ele” is added to the “total” using the “+=” operator. After the loop, the code prints the original set and the total of elements in the set. In this case, the sum of elements in the set is 1 + 2 + 3 + 4 + 5 = 15. Therefore, the output is “Original set1: {1, 2, 3, 4, 5}” and “Total of elements in the set: 15.”

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

Set = {1, 2, 3, 4, 5}
print(“Original set1: “, Set)
total = 0
for ele in Set:
    total += ele
print(“Average of the set: “, total / len(Set))

a) Original set1: {1, 2, 3, 4, 5}
Average of the set: 3.0
b) Original set1: {1, 2, 3, 4, 5}
Average of the set: 3
c) Original set1: {1, 2, 3, 4, 5}
Average of the set: 15/5
d) Original set1: {1, 2, 3, 4, 5}
Average of the set: 2.5

Correct answer is: a) Original set1: {1, 2, 3, 4, 5}
Average of the set: 3.0
Explanation: The code calculates the sum of all elements in the set and divides it by the length of the set to find the average. The sum of 1 + 2 + 3 + 4 + 5 is 15, and the length of the set is 5, so the average is 15/5, which is 3.0. The output shows the original set and the calculated average.

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

Set = {1, 2, 3, 4, 5}
print(“Original set1: “, Set)
count = 0
for ele in Set:
    if ele % 2 == 0:
        count += 1
if count == len(Set):
    print(“All elements in the set are even”)
else:
    print(“All elements in the set are not even”)

a) All elements in the set are even
b) All elements in the set are not even
c) Original set1: {1, 2, 3, 4, 5}
d) Original set1: {2, 4}

Correct answer is: b) All elements in the set are not even
Explanation: The code initializes a set called “Set” with values {1, 2, 3, 4, 5}. It then iterates over each element in the set using a for loop. Inside the loop, it checks if the element is even (divisible by 2) using the condition `ele % 2 == 0`. If an even element is found, the `count` variable is incremented. After the loop, the code compares the value of `count` with the length of the set using the condition `count == len(Set)`. If the count is equal to the length, it means that all elements in the set are even. However, in this case, since the set contains both odd and even numbers, the condition `count == len(Set)` evaluates to False.

8). What will be the output of the following code?

Set = {1, 2, 3, 4, 5}
print(“Original set1: “, Set)
count = 0
for ele in Set:
    if ele % 2 != 0:
        count += 1
if count == len(Set):
    print(“All elements in the set are odd”)
else:
    print(“All elements in the set are not odd”)

a) Original set1: {1, 2, 3, 4, 5} All elements in the set are not odd
b) Original set1: {1, 2, 3, 4, 5} All elements in the set are odd
c) Original set1: {1, 2, 3, 4, 5}
d) Error: Invalid syntax

Correct answer is: a) Original set1: {1, 2, 3, 4, 5} All elements in the set are not odd
Explanation: The code defines a set named “Set” with the values {1, 2, 3, 4, 5}. It then iterates over each element in the set using a for loop. If an element is not divisible by 2 (i.e., odd), the count variable is incremented. After iterating through all elements, the code checks if the count is equal to the length of the set. If they are equal, it means all elements are odd, and it prints “All elements in the set are odd.” Otherwise, it prints “All elements in the set are not odd.” 

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

Set = {1, 2, 3, 5}
prime = []

for ele in Set:
    count = 0
    for num in range(2, ele):
        if ele % num == 0:
            count += 1
    if count == 0:
        prime.append(ele)

if len(prime) == len(Set):
    print(“All elements in the set are prime numbers”)
else:
    print(“All elements in the set are not prime numbers”)

a) All elements in the set are prime numbers
b) All elements in the set are not prime numbers
c) The code will raise an error
d) The output cannot be determined

Correct answer is: a) All elements in the set are prime numbers
Explanation: The given code first initializes a set Set containing the elements {1, 2, 3, 5}. It also initializes an empty list prime. Next, the code iterates through each element ele in the set Set. For each element, it checks if there exists any number num between 2 and ele – 1 (exclusive) that divides ele without leaving a remainder. If such a number is found, the variable count is incremented. If the count remains 0 after the inner loop completes, it means the element is a prime number, and it is added to the prime list. After processing all elements in the set, the code checks if the number of prime elements in the prime list is equal to the total number of elements in the set Set. If they are equal, it means all elements in the set are prime numbers, and the corresponding message is printed. Otherwise, it means not all elements are prime numbers, and the appropriate message is printed.

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

a = {1, 2, 4, 5}
print(“Original set: “, a)
a.clear()
print(a)

a) Original set: {1, 2, 4, 5}
{}
b) Original set: {1, 2, 4, 5}
{1, 2, 4, 5}
c) Original set: {1, 2, 4, 5}
Error
d) Original set: {}
{}

Correct answer is: a) Original set: {1, 2, 4, 5}
{}
Explanation: The code defines a set `a` with elements [1, 2, 4, 5]. It then prints the original set `a`, which outputs `{1, 2, 4, 5}`. After that, the `clear()` method is called on set `a`, which removes all elements from the set. Finally, it prints the set `a` again, which results in an empty set `{}`.

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

a = {1, 2, 4, 5}
print(“Original set: “, a)
print(“Remove element from set: “, a.pop())
print(a)

a) Original set: {1, 2, 4, 5} Remove element from set: 1 {2, 4, 5}
b) Original set: {1, 2, 4, 5} Remove element from set: 5 {1, 2, 4}
c) Original set: {1, 2, 4, 5} Remove element from set: 2 {1, 4, 5}
d) Original set: {1, 2, 4, 5} Remove element from set: 4 {1, 2, 5}

Correct answer is: a) Original set: {1, 2, 4, 5} Remove element from set: 1 {2, 4, 5}
Explanation: The initial set “a” is defined as {1, 2, 4, 5}. When the pop() method is called on set “a”, it removes and returns an arbitrary element from the set. In this case, the element 1 is removed. Therefore, the output of “print(“Original set: “, a)” displays the original set as {1, 2, 4, 5}. The output of “print(“Remove element from set: “, a.pop())” displays the removed element, which is 1. 

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

a = {1, 2, 4, 5}
b = {2, 4}
print(“Original set1: “, a)
print(“Original set2: “, b)
print(“Difference between two sets using – operator: “, a – b)

a) Original set1: {1, 2, 4, 5}
Original set2: {2, 4}
Difference between two sets using – operator: {1, 5}
b) Original set1: {1, 2, 4, 5}
Original set2: {2, 4}
Difference between two sets using – operator: {1, 2, 4, 5}
c) Original set1: {1, 2, 4, 5}
Original set2: {2, 4}
Difference between two sets using – operator: {1, 4, 5}
d) Original set1: {1, 2, 4, 5}
Original set2: {2, 4}
Difference between two sets using – operator: {2, 4}

Correct answer is: a) Original set1: {1, 2, 4, 5}
Original set2: {2, 4}
Difference between two sets using – operator: {1, 5}
Explanation: The expression `a – b` represents the difference between set `a` and set `b`, which contains the elements present in set `a` but not in set `b`. In this case, the elements 1 and 5 are not present in set `b`, so they are included in the output set.

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

a = {1, 2, 4, 5}
b = {2, 4}
print(“Original set1: “, a)
print(“Original set2: “, b)
count = 0
for ele in b:
    if ele in a:
        count += 1
if count == len(b):
    print(“True”)
else:
    print(“False”)

a) True
b) False
c) Error
d) None

Correct answer is: a) True
Explanation: The code compares the elements of set `b` with set `a`. If all elements of `b` are found in `a`, the variable `count` will be equal to the length of set `b`. In this case, since all elements of `b` (2 and 4) are present in `a`, the count will be equal to the length of `b` (2). Therefore, the output will be “True”.

14). What will be the output of the following code?

Set = {10, 23, 45, 66, 96, 83}
print(“Original set1: “, Set)
print(“Maximum value: “, max(Set))

a) Original set1: {10, 23, 45, 66, 96, 83} Maximum value: 96
b) Original set1: {10, 23, 45, 66, 96, 83} Maximum value: 83
c) Original set1: {10, 23, 45, 66, 96, 83} Maximum value: 10
d) Original set1: {10, 23, 45, 66, 96, 83} Maximum value: 45

Correct answer is: a) Original set1: {10, 23, 45, 66, 96, 83} Maximum value: 96
Explanation: The given code defines a set named Set with elements 10, 23, 45, 66, 96, and 83. The print statement outputs the original set as {10, 23, 45, 66, 96, 83}. The max() function is used to find the maximum value from the set, which is 96. 

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

a = {1,2,4,5}
b = {2,4}
print(“Original set1: “,a)
print(“Original set2: “,b)
print(“Intersection between two sets using the “&” operator: “,a&b)

a) Original set1: {1, 2, 4, 5}
Original set2: {2, 4}
Intersection between two sets using the “&” operator: {2, 4}
b) Original set1: {1, 2, 4, 5}
Original set2: {2, 4}
Intersection between two sets using the “&” operator: {1, 2, 4, 5}
c) Original set1: {1, 2, 4, 5}
Original set2: {2, 4}
Intersection between two sets using the “&” operator: {1, 5}
d) Original set1: {1, 2, 4, 5}
Original set2: {2, 4}
Intersection between two sets using the “&” operator: {1, 2, 4, 5}

Correct answer is: a) Original set1: {1, 2, 4, 5}
Original set2: {2, 4}
Intersection between two sets using the “&” operator: {2, 4}
Explanation: The “&” operator is used to find the intersection between two sets. In this case, set “a” contains the elements {1, 2, 4, 5} and set “b” contains the elements {2, 4}. The intersection of these two sets is {2, 4}, which is displayed as the output.

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

a = {1, 2, 4, 5}
b = {7, 8}
c = {6, 10, 0}
print(“Original set1: “, a)
print(“Original set2: “, b)
print(“Original set3: “, c)
print(“Union of multiple sets using the | operator: “, a | b | c)

a) Original set1: {1, 2, 4, 5}
Original set2: {7, 8}
Original set3: {6, 10, 0}
Union of multiple sets using the | operator: {0, 1, 2, 4, 5, 6, 7, 8, 10}
b) Original set1: {1, 2, 4, 5}
Original set2: {7, 8}
Original set3: {6, 10, 0}
Union of multiple sets using the | operator: {1, 2, 4, 5, 7, 8, 6, 10, 0}
c) Original set1: {1, 2, 4, 5}
Original set2: {7, 8}
Original set3: {6, 10, 0}
Union of multiple sets using the | operator: {1, 2, 4, 5}
d) Error: Invalid use of the | operator for multiple sets

Correct answer is: b) Original set1: {1, 2, 4, 5}
Original set2: {7, 8}
Original set3: {6, 10, 0}
Union of multiple sets using the | operator: {1, 2, 4, 5, 7, 8, 6, 10, 0}
Explanation: The code initializes three sets: `a`, `b`, and `c`. Then it prints the original sets and performs the union operation using the `|` operator on all three sets. The resulting set contains all the elements from the three sets combined. In this case, the output will be `{1, 2, 4, 5, 7, 8, 6, 10, 0}`.

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

a = {1, 2, 4, 5}
b = {4, 1}
print(“Original set1: “, a)
print(“Original set2: “, b)
print(“Symmetric difference of two sets using the “^” operator: “, a^b)

a) Original set1: {1, 2, 4, 5}, Original set2: {4, 1}, Symmetric difference of two sets using the “^” operator: {2, 5}
b) Original set1: {1, 2, 4, 5}, Original set2: {4, 1}, Symmetric difference of two sets using the “^” operator: {1, 2, 4, 5}
c) Original set1: {1, 2, 4, 5}, Original set2: {4, 1}, Symmetric difference of two sets using the “^” operator: {1, 4}
d) Original set1: {1, 2, 4, 5}, Original set2: {4, 1}, Symmetric difference of two sets using the “^” operator: {1, 2, 4, 5}

Correct answer is: a) Original set1: {1, 2, 4, 5}, Original set2: {4, 1}, Symmetric difference of two sets using the “^” operator: {2, 5}
Explanation: The set a contains elements {1, 2, 4, 5} and set b contains elements {4, 1}. The “^” operator is used to calculate the symmetric difference between two sets, which is the set of elements that are present in either set but not in both. In this case, the symmetric difference of sets a and b is {2, 5}, which is printed as the output.

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

a = {1, 2, 4, 5}
b = {4, 1}
print(“Original set1: “, a)
print(“Original set2: “, b)
print(“A is superset of B: “, a.issuperset(b))

a) Original set1: {1, 2, 4, 5} Original set2: {4, 1} A is superset of B: True
b) Original set1: {1, 2, 4, 5} Original set2: {4, 1} A is superset of B: False
c) Original set1: {1, 2, 4, 5} Original set2: {1, 4} A is superset of B: True
d) Original set1: {4, 1} Original set2: {1, 4} A is superset of B: True

Correct answer is: a) Original set1: {1, 2, 4, 5} Original set2: {4, 1} A is superset of B: True
Explanation: The code initializes two sets, ‘a’ and ‘b’, with some elements. The ‘issuperset()’ method is then used to check if ‘a’ is a superset of ‘b’. In this case, ‘a’ contains all the elements of ‘b’ (1 and 4), making ‘a’ a superset of ‘b’. The output will be “Original set1: {1, 2, 4, 5} Original set2: {4, 1} A is superset of B: True”.

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

a = {1,2,4,5}
b = {4,1}
print(“Original set1: “,a)
print(“Original set2: “,b)
count = 0
for ele in b:
    if ele in a:
        count += 1
if count == len(b):
    print(“A is superset of B”)
else:
    print(“A is not a superset of B”)

a) A is superset of B
b) A is not a superset of B
c) Original set1: {1, 2, 4, 5} Original set2: {4, 1}
d) Original set1: {1, 2, 4, 5} Original set2: {4, 1} A is superset of B

Correct answer is: a) A is superset of B
Explanation: The code checks if set A is a superset of set B. In this case, set A contains all the elements of set B. Therefore, the output will be “A is superset of B.”

20). Which of the following output will be generated by the provided code?

a = {1,2,4,5}
b = {4,1}
print(“Original set1: “,a)
print(“Original set2: “,b)
print(“Common elements: “)
for ele in a:
    if ele in b:
        print(ele)

a) Original set1: {1, 2, 4, 5}
Original set2: {4, 1}
Common elements:
1
4
b) Original set1: {1, 2, 4, 5}
Original set2: {4, 1}
Common elements:
1
4
5
c) Original set1: {1, 2, 4, 5}
Original set2: {4, 1}
Common elements:
4
5
d) Original set1: {1, 2, 4, 5}
Original set2: {4, 1}
Common elements:
2
5

Correct answer is: a) Original set1: {1, 2, 4, 5}
Original set2: {4, 1}
Common elements:
1
4
Explanation: The provided code iterates over the elements in set `a` and checks if each element is present in set `b`. If an element is found in both sets, it is printed. In this case, the common elements between set `a` and `b` are 1 and 4.

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

a = {1, 2, 4, 5}
print(“Original set: “, a)
a.remove(5)
print(“After removing 5 from the given set: “, a)

a) Original set: {1, 2, 4, 5}
After removing 5 from the given set: {1, 2, 4}
b) Original set: {1, 2, 4, 5}
After removing 5 from the given set: {1, 2, 5}
c) Original set: {1, 2, 4, 5}
After removing 5 from the given set: {1, 2, 4, 5}
d) Original set: {1, 2, 4, 5}
After removing 5 from the given set: {1, 2, 4, 5, 6}

Correct answer is: a) Original set: {1, 2, 4, 5}
After removing 5 from the given set: {1, 2, 4}
Explanation: The original set `a` is defined as {1, 2, 4, 5}. When the `remove()` method is called with the argument 5, it removes the element 5 from the set. After removing 5, the set becomes {1, 2, 4}. Therefore, the output will be “Original set: {1, 2, 4, 5}” followed by “After removing 5 from the given set: {1, 2, 4}”.

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

a = {1, 2, 4, 5}
print(“Original set: “, a)
b = {7, 8, 9}
for ele in b:
    a.add(ele)
print(“New set: “, a)

a) Original set: {1, 2, 4, 5}
New set: {1, 2, 4, 5, 7, 8, 9}
b) Original set: {1, 2, 4, 5}
New set: {7, 8, 9}
c) Original set: {1, 2, 4, 5}
New set: {1, 2, 4, 5}
d) Original set: {7, 8, 9}
New set: {1, 2, 4, 5, 7, 8, 9}

Correct answer is: a) Original set: {1, 2, 4, 5} New set: {1, 2, 4, 5, 7, 8, 9}
Explanation: The original set `a` is initialized with elements 1, 2, 4, and 5. The set `b` is initialized with elements 7, 8, and 9. In the for loop, each element from set `b` is added to set `a` using the `add()` method. After the loop, the elements 7, 8, and 9 are added to set `a`.

Leave a Comment