Python Set MCQ : Set 4

Python Set MCQ

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

a = {1, 2, 4, 5, 7, 8, 9}
print(“Original set1: “, a)
print(type(a))

a) Original set1: {1, 2, 4, 5, 7, 8, 9}
\<class ‘set’>
b) Original set1: {1, 2, 4, 5, 7, 8, 9}
\<class ‘list’>
c) Original set1: {1, 2, 4, 5, 7, 8, 9}
\<class ‘tuple’>
d) Original set1: {1, 2, 4, 5, 7, 8, 9}
\<class ‘dict’>

Correct answer is: a) Original set1: {1, 2, 4, 5, 7, 8, 9} \<class ‘set’>
Explanation: The code initializes a set named ‘a’ with the elements 1, 2, 4, 5, 7, 8, and 9. The print() function is then used to display the original set, which outputs {1, 2, 4, 5, 7, 8, 9}. The type() function is used to determine the data type of the set ‘a’, which is a set (class ‘set’).

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

a = {1, 2, 4, 5, 7, 8, 9}
print(“Original set1: “, a)
b = frozenset(a)
print(b)

a) Original set1: {1, 2, 4, 5, 7, 8, 9}
frozenset({1, 2, 4, 5, 7, 8, 9})
b) Original set1: {1, 2, 4, 5, 7, 8, 9}
frozenset([1, 2, 4, 5, 7, 8, 9])
c) Original set1: {1, 2, 4, 5, 7, 8, 9}
frozenset((1, 2, 4, 5, 7, 8, 9))
d) Original set1: {1, 2, 4, 5, 7, 8, 9}
frozenset({1: None, 2: None, 4: None, 5: None, 7: None, 8: None, 9: None})

Correct answer is: c) Original set1: {1, 2, 4, 5, 7, 8, 9}
frozenset({1, 2, 4, 5, 7, 8, 9})
Explanation: The code initializes a set `a` with elements 1, 2, 4, 5, 7, 8, and 9. The line `b = frozenset(a)` creates a frozen set `b` from `a`. The `frozenset()` function returns an immutable frozenset object. The output will be “Original set1: {1, 2, 4, 5, 7, 8, 9}” followed by “frozenset({1, 2, 4, 5, 7, 8, 9})”.

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

a = {1,2,4,6}
b = {7,8,9,6}
c = {5,8,9}
print(“Original set1: “,a)
print(“Original set2: “,b)
print(“Original set3: “,c)
print(“Difference between above sets: “)
print(a.difference(b.difference(c)))

a) Original set1: {1, 2, 4, 6}
Original set2: {7, 8, 9, 6}
Original set3: {5, 8, 9}
Difference between above sets: {1, 2, 4}
b) Original set1: {1, 2, 4, 6}
Original set2: {7, 8, 9, 6}
Original set3: {5, 8, 9}
Difference between above sets: {1, 2, 4, 6, 7, 8, 9}
c) Original set1: {1, 2, 4, 6}
Original set2: {7, 8, 9, 6}
Original set3: {5, 8, 9}
Difference between above sets: {1, 2, 4, 5, 6, 7, 8, 9}
d) Original set1: {1, 2, 4, 6}
Original set2: {7, 8, 9, 6}
Original set3: {5, 8, 9}
Difference between above sets: {5, 7}

Correct answer is: a) Original set1: {1, 2, 4, 6}
Original set2: {7, 8, 9, 6}
Original set3: {5, 8, 9}
Difference between above sets: {1, 2, 4}
Explanation: The code demonstrates the difference between sets using the difference() method. The difference() method is applied to the set ‘a’ with the argument ‘b.difference(c)’, which computes the difference between set ‘b’ and set ‘c’. Then, this difference is subtracted from set ‘a’ using the difference() method. The resulting set contains the elements that are present in ‘a’ but not in ‘b’ or ‘c’, which in this case is {1, 2, 4}.

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

a = {1,2,4,6}
b = {7,8,9,6}
c = {5,8,9,6}
print(“Original set1: “,a)
print(“Original set2: “,b)
print(“Original set3: “,c)
print(“Intersection between above sets: “)
print(a.intersection(b.intersection(c)))

a) {6}
b) {1, 2, 4, 6}
c) {6, 8, 9}
d) {6, 8, 9, 5}

Correct answer is: a) {6}
Explanation: The code calculates the intersection between sets a, b, and c using the intersection() method. First, b.intersection(c) finds the common elements between sets b and c, which results in {6, 8, 9}. Then, a.intersection(b.intersection(c)) finds the common elements between set a and the result of the previous intersection, which is {6}.

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

a = {1,2,4,6}
b = {7,8,9,6}
c = {5,8,9,6}
print(“Original set1: “,a)
print(“Original set2: “,b)
print(“Original set3: “,c)
print(“Difference between above sets: “)
print(a.union(b.union(c)))

a) {1, 2, 4, 5, 6, 7, 8, 9}
b) {1, 2, 4, 6, 7, 8, 9}
c) {1, 2, 4, 5, 6, 7}
d) {1, 2, 4, 6}

Correct answer is: a) {1, 2, 4, 5, 6, 7, 8, 9}
Explanation: The union() method is used to find the union of multiple sets. In this case, we are finding the union of set a, set b, and set c. The resulting set contains all the unique elements from all the sets involved. Therefore, the output will be {1, 2, 4, 5, 6, 7, 8, 9}.

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

String = “Learning python is fun”
Set = {“fun”,”python”}
count = 0
for word in Set:
    if word in String:
        count += 1
if count > 0:
print(“An element in a set is a substring of a given string”)
else:
print(“An element in a set is not a substring of a given string”)

a) An element in a set is a substring of a given string
b) An element in a set is not a substring of a given string
c) String: Learning python is fun
Set: {‘fun’, ‘python’}
d) String: “Learning python is fun”
Set: {“fun”,”python”}

Correct answer is: a) An element in a set is a substring of a given string
Explanation: The code checks if any element in the set is a substring of the given string. In this case, both “fun” and “python” are substrings of the string “Learning python is fun”. Therefore, the count variable is incremented, and the condition count > 0 is satisfied. As a result, the output will be “An element in a set is a substring of a given string”.

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

Set = {1, 2, 4, 6}
print(“Set: “, Set)
List = list(Set)
print(“The index of 4: “, List.index(4))

a) Set: {1, 2, 4, 6} | The index of 4: 2
b) Set: {1, 2, 4, 6} | The index of 4: 3
c) Set: [1, 2, 4, 6] | The index of 4: 2
d) Set: [1, 2, 4, 6] | The index of 4: 3

Correct answer is: a) Set: {1, 2, 4, 6} | The index of 4: 2
Explanation: The code initializes a set named “Set” with the elements 1, 2, 4, and 6. The set is then converted to a list using the list() function and stored in the variable “List”. When using the index() method on the list “List” and passing the argument 4, it returns the index position of the element 4, which is 2.

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

a = {1, 2, 4, 6}
print(“Original set:”, a)
Dict = {}
for ele in a:
    Dict[ele] = {}
print(“Dictionary:”, Dict)

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

Correct answer is: a) Original set: {1, 2, 4, 6} Dictionary: {1: {}, 2: {}, 4: {}, 6: {}}
Explanation: In the code, a set `a` is defined with elements [1, 2, 4, 6]. The set is then printed as “Original set: {1, 2, 4, 6}”. An empty dictionary `Dict` is created. The for loop iterates over each element in set `a` and assigns an empty dictionary as a value to each element in the `Dict` dictionary. Finally, the dictionary is printed as “Dictionary: {1: {}, 2: {}, 4: {}, 6: {}}”.

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

Set = set()
for num in range(1, 21):
    if num % 2 == 0:
        Set.add(num)
print(“Set of even numbers:”, Set)

a) Set of even numbers: {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}
b) Set of even numbers: {1, 3, 5, 7, 9, 11, 13, 15, 17, 19}
c) Set of even numbers: {}
d) Set of even numbers: {2, 4, 6, 8, 10, 12, 14, 16, 18}

Correct answer is: a) Set of even numbers: {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}
Explanation: The given code initializes an empty set, `Set`. It then iterates over the numbers from 1 to 20 using a `for` loop. For each number, if the number is divisible by 2 (i.e., an even number), it adds the number to the set using the `add()` method. Finally, it prints the set of even numbers, which includes all even numbers from 1 to 20. The correct answer is option a.

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

Set = set()
for num in range(1,21):
    if num%2 != 0:
        Set.add(num)
print(“Set of odd numbers: “, Set)

a) Set of odd numbers: {1, 3, 5, 7, 9, 11, 13, 15, 17, 19}
b) Set of odd numbers: {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}
c) Set of odd numbers: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
d) Set of odd numbers: {}

Correct answer is: a) Set of odd numbers: {1, 3, 5, 7, 9, 11, 13, 15, 17, 19}
Explanation: The code initializes an empty set and then iterates over numbers from 1 to 20. If a number is odd, it is added to the set using the add() method. Finally, the set is printed, showing the set of odd numbers from 1 to 20.

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

Set = {“Shan Rukh Khan”, “Akshay Kumar”, “Tom Hardy”, “Robert Pattinson”}
print(“Set of actors: “, Set)

a) Set of actors: {“Shan Rukh Khan”, “Akshay Kumar”, “Tom Hardy”, “Robert Pattinson”}
b) Set of actors: {“Shan Rukh Khan, Akshay Kumar, Tom Hardy, Robert Pattinson”}
c) Set of actors: {‘Shan Rukh Khan’, ‘Akshay Kumar’, ‘Tom Hardy’, ‘Robert Pattinson’}
d) Set of actors: [‘Shan Rukh Khan’, ‘Akshay Kumar’, ‘Tom Hardy’, ‘Robert Pattinson’]

Correct answer is: c) Set of actors: {‘Shan Rukh Khan’, ‘Akshay Kumar’, ‘Tom Hardy’, ‘Robert Pattinson’}
Explanation: The code defines a set named `Set` with four elements: “Shan Rukh Khan”, “Akshay Kumar”, “Tom Hardy”, and “Robert Pattinson”. When the `print()` function is called, it displays the set of actors enclosed in curly braces and single quotes.

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

Set1 = {“Lord of the Rings”, “Harry Potter and the Sorcerer’s Stone”, “The Magic Tree”, “Tower To The Stars”}
Set2 = {“Wizards of Ice”, “Call of the Forest”, “Lord of the Rings”}
print(“Original Set1: “, Set1)
print(“Original Set2: “, Set2)
print(“Common books: “)
for book_name in Set1:
    if book_name in Set2:
        print(book_name)

a) Original Set1: {“Lord of the Rings”, “Harry Potter and the Sorcerer’s Stone”, “The Magic Tree”, “Tower To The Stars”}
Original Set2: {“Wizards of Ice”, “Call of the Forest”, “Lord of the Rings”}
Common books: Lord of the Rings
b) Original Set1: {“Lord of the Rings”, “Harry Potter and the Sorcerer’s Stone”, “The Magic Tree”, “Tower To The Stars”}
Original Set2: {“Wizards of Ice”, “Call of the Forest”, “Lord of the Rings”}
Common books: Lord of the Rings, The Magic Tree
c) Original Set1: {“Lord of the Rings”, “Harry Potter and the Sorcerer’s Stone”, “The Magic Tree”, “Tower To The Stars”}
Original Set2: {“Wizards of Ice”, “Call of the Forest”, “Lord of the Rings”}
Common books: The Magic Tree
d) Original Set1: {“Lord of the Rings”, “Harry Potter and the Sorcerer’s Stone”, “The Magic Tree”, “Tower To The Stars”}
Original Set2: {“Wizards of Ice”, “Call of the Forest”, “Lord of the Rings”}
Common books: Wizards of Ice

Correct answer is: a) Original Set1: {“Lord of the Rings”, “Harry Potter and the Sorcerer’s Stone”, “The Magic Tree”, “Tower To The Stars”}
Original Set2: {“Wizards of Ice”, “Call of the Forest”, “Lord of the Rings”}
Common books: Lord of the Rings
Explanation: The code compares the elements in Set1 with Set2 and prints the books that exist in both sets. In this case, “Lord of the Rings” is the only book that appears in both sets, so it will be printed as the common book.

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

Set = {“I”, “am”, “Learning”, “Python”}
max_len = 0
max_word = 0
print(“Original Set: “, Set)
for word in Set:
    if len(word) > max_len:
        max_len = len(word)
        max_word = word
print(“Word having maximum length: “, max_word)
print(“Length of the word: “, max_len)

a) Original Set: {“I”, “am”, “Learning”, “Python”}
Word having maximum length: “Learning”
Length of the word: 8
b) Original Set: {“I”, “am”, “Learning”, “Python”}
Word having maximum length: “Python”
Length of the word: 6
c) Original Set: {“I”, “am”, “Learning”, “Python”}
Word having maximum length: “Learning”
Length of the word: 7
d) Original Set: {“I”, “am”, “Learning”, “Python”}
Word having maximum length: “am”
Length of the word: 2

Correct answer is: a) Original Set: {“I”, “am”, “Learning”, “Python”}
Word having maximum length: “Learning”
Length of the word: 8
Explanation: The code initializes the `Set` variable with a set of strings. It then iterates over each word in the set and checks if the length of the current word is greater than the current maximum length (`max_len`). If it is, it updates `max_len` and stores the current word as `max_word`. After the loop, it prints the original set, the word with the maximum length, and the length of that word.

14). 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(“b is subset of a”)
else:
    print(“b is not a subset of a”)

a) Original set1: {1, 2, 4, 5} Original set2: {2, 4} \n b is subset of a
b) Original set1: {1, 2, 4, 5} Original set2: {2, 4} \n b is not a subset of a
c) Original set1: {2, 4} Original set2: {1, 2, 4, 5} \n b is subset of a
d) Original set1: {2, 4} Original set2: {1, 2, 4, 5} \n b is not a subset of a

Correct answer is: a) Original set1: {1, 2, 4, 5} Original set2: {2, 4} \n b is a subset of a
Explanation: The code checks if every element in set b is also present in set a. If all elements in b are found in a, then b is considered a subset of a. Otherwise, it is not a subset. In this case, set a contains the elements [1, 2, 4, 5] and set b contains the elements [2, 4]. Since all elements in b (2 and 4) are present in a, the condition count == len(b) is satisfied, and the output is “b is a subset of a”.

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

a = {1,2,4,5}
b = {7,8,9}
print(“Original set1: “,a)
print(“Original set2: “,b)
print(“Are two sets dis-joint: “,a.isdisjoint(b))

a) Original set1: {1, 2, 4, 5} Original set2: {7, 8, 9} Are two sets disjoint: True
b) Original set1: {1, 2, 4, 5} Original set2: {7, 8, 9} Are two sets disjoint: False
c) Original set1: {1, 2, 4, 5} Original set2: {7, 8, 9} Are two sets disjoint: None
d) Error: The code contains a syntax error.

Correct answer is: a) Original set1: {1, 2, 4, 5} Original set2: {7, 8, 9} Are two sets disjoint: True
Explanation: The code initializes two sets, a and b, with the values {1, 2, 4, 5} and {7, 8, 9} respectively. The isdisjoint() method is then used to check if the two sets are disjoint (have no common elements). In this case, the sets have no common elements, so the output will be True.

16). 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(“b is subset of a: “, b.issubset(a))

a) Original set1: {1, 2, 4, 5}
Original set2: {2, 4}
b is subset of a: True
b) Original set1: {1, 2, 4, 5}
Original set2: {2, 4}
b is subset of a: False
c) Original set1: {1, 2, 4, 5}
Original set2: {2, 4}
b is subset of a: None
d) Error: Invalid syntax in the code.

Correct answer is: a) Original set1: {1, 2, 4, 5}
Original set2: {2, 4}
b is subset of a: True
Explanation: The code defines two sets, `a` and `b`. `a` contains elements [1, 2, 4, 5], while `b` contains elements [2, 4]. The `issubset()` method is then used to check if `b` is a subset of `a`. Since all elements of `b` are present in `a`, the output is `True`.

17). Which statement is used to check if an element exists in a set?

a) if element in set:
b) if element exists(set):
c) if element.has(set):
d) if element.exists(set):

Correct answer is: a) if element in set:
Explanation: The “in” keyword is used to check if an element exists in a set.

18). What is the result of executing the following code:

set = {1, 2, 3}
print(2 in set)?

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

Correct answer is: a) True
Explanation: The “in” keyword checks if the element 2 exists in the set, which returns True.

19). Which method is used to return the maximum element from a set?

a) max()
b) maximum()
c) largest()
d) get_max()

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

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

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

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

Correct answer is: c) Original set: {1, 2, 4, 5, 7, 8, 9} New set: {1, 2, 4, 5}
Explanation: The code starts with a set `a` containing elements {1, 2, 4, 5, 7, 8, 9}. It then defines another set `b` with elements {7, 8, 9}. The code then iterates over each element in `b` and checks if it exists in `a`. If an element is found in `a`, it is removed using the `remove()` method. After this process, the code prints the updated set `a`.

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

a = {1, 2, 4, 5, 7, 8, 9}
print(“Original set1: “,a)
if len(a) == 0:
    print(“Set is empty”)
else:
    print(“Set is not empty”)

a) Original set1: {1, 2, 4, 5, 7, 8, 9}
Set is empty
b) Original set1: {1, 2, 4, 5, 7, 8, 9}
Set is not empty
c) Original set1: {1, 2, 4, 5, 7, 8, 9}
Error: Set object has no attribute ’empty’
d) Original set1: {1, 2, 4, 5, 7, 8, 9}
Error: Set object has no attribute ‘len’

Correct answer is: b) Original set1: {1, 2, 4, 5, 7, 8, 9}
Set is not empty
Explanation: The code initializes a set a with elements [1, 2, 4, 5, 7, 8, 9]. The len(a) function returns the number of elements in the set, which is not equal to 0 in this case. Therefore, the condition len(a) == 0 evaluates to False. As a result, the code executes the else block and prints “Set is not empty”

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

a = {1, 2, 4, 5, 7, 8, 9}
b = {2,3,4}
print(“Original set1: “,a)
print(“Original set2: “,b)
if a == b:
   print(“Both sets are equal”)
else:
    print(“Both sets are not equal”)

a) Original set1: {1, 2, 4, 5, 7, 8, 9}
Original set2: {2, 3, 4}
Both sets are equal
b) Original set1: {1, 2, 4, 5, 7, 8, 9}
Original set2: {2, 3, 4}
Both sets are not equal
c) Original set1: {1, 2, 4, 5, 7, 8, 9}
Original set2: {2, 3, 4}
Error: Invalid comparison between sets
d) Error: Invalid syntax

Correct answer is: b) Original set1: {1, 2, 4, 5, 7, 8, 9}
Original set2: {2, 3, 4}
Both sets are not equal
Explanation: The code compares two sets, a and b, using the equality operator (==). Since the sets have different elements (set a has elements {1, 2, 4, 5, 7, 8, 9} and set b has elements {2, 3, 4}), the condition evaluates to False and the statement “Both sets are not equal” is printed.

Leave a Comment