Saturday, 20 January 2024

Top 20 Python Set Questions with answer



Question 1:

What is a set in Python?

a) A collection of ordered elements

b) A collection of unordered elements

c) A single element

d) A data type


Question 2:

How do you create an empty set in Python?

a) set()

b) empty_set = {}

c) empty_set = set()

d) Both b and c


Question 3:

How do you add an element to a set in Python?

a) set.insert(element)

b) set.add(element)

c) set.append(element)

d) set.include(element)


Question 4:

What is the key difference between a set and a list in Python?

a) Sets are ordered, while lists are unordered

b) Sets are mutable, while lists are immutable

c) Sets can contain only numeric elements

d) Sets are unordered and do not allow duplicate elements


Question 5:

How do you check if an element is present in a set?

a) element in set

b) set.contains(element)

c) set.exists(element)

d) element.exists(set)


Question 6:

What happens when you try to add a duplicate element to a set?

a) The element is added successfully

b) Python raises an exception

c) The duplicate element is ignored, and the set remains unchanged

d) The set is automatically sorted


Question 7:

How do you remove an element from a set?

a) set.remove(element)

b) set.delete(element)

c) set.pop(element)

d) set.discard(element)


Question 8:

What is the purpose of the len() function when used with a set?

a) It returns the total number of elements in the set

b) It returns the last element of the set

c) It returns the length of each element in the set

d) It returns the sum of all elements in the set


Question 9:

How do you create a set with elements from 1 to 5 in Python?

a) set = {1, 2, 3, 4, 5}

b) set = range(1, 6)

c) set = set(1, 6)

d) set = {range(1, 6)}


Question 10:

What is the purpose of the pop() method in Python sets?

a) Removes the last element from the set

b) Removes a random element from the set and returns it

c) Removes the first occurrence of the specified element

d) Sorts the elements of the set


Question 11:

What is the difference between a set and a frozenset in Python?

a) Sets are mutable, while frozensets are immutable

b) Sets are unordered, while frozensets are ordered

c) Sets can contain only numeric elements

d) Sets allow duplicate elements, while frozensets do not


Question 12:

Which of the following statements is true regarding the union of two sets?

a) The union operator for sets is +

b) The union of two sets is the intersection of their elements

c) The union of two sets contains all unique elements from both sets

d) The union of two sets results in an empty set


Question 13:

What is the purpose of the clear() method in Python sets?

a) Clears all elements from the set

b) Returns a clear copy of the set

c) Clears only the first element from the set

d) Clears the set if a specific element is provided


Question 14:

Which method is used to find the intersection of two sets in Python?

a) set.intersection(set2)

b) set.intersect(set2)

c) set.common(set2)

d) set.and(set2)


Question 15:

What is the output of the following code?

set1 = {1, 2, 3}

set2 = {3, 4, 5}

result = set1.union(set2)

print(result)

a) {1, 2, 3, 4, 5}

b) {1, 2, 3}

c) {3, 4, 5}

d) {1, 2, 4, 5}


Question 16:

How do you check if a set is a subset of another set?

a) set.is_subset(other_set)

b) set.subset_of(other_set)

c) set.issubset(other_set)

d) set.contains_subset(other_set)


Question 17:

What does the difference() method do when applied to two sets?

a) Returns the union of the two sets

b) Returns the intersection of the two sets

c) Returns the difference between the two sets

d) Returns the symmetric difference between the two sets


Question 18:

What is the purpose of the symmetric_difference() method in Python sets?

a) Returns the union of the two sets

b) Returns the intersection of the two sets

c) Returns the difference between the two sets

d) Returns the symmetric difference between the two sets


Question 19:

What is the output of the following code?

set1 = {1, 2, 3}

set2 = {3, 4, 5}

result = set1.difference(set2)

print(result)

a) {1, 2, 3, 4, 5}

b) {1, 2}

c) {3}

d) {4, 5}


Question 20:

What is the purpose of the issuperset() method in Python sets?

a) Checks if the set is a proper superset of another set

b) Checks if the set is a subset of another set

c) Checks if the set is equal to another set

d) Checks if the set contains all elements of another set


Answer Key:

  1. b) A collection of unordered elements
  2. c) empty_set = set()
  3. b) set.add(element)
  4. d) Sets are unordered and do not allow duplicate elements
  5. a) element in set
  6. c) The duplicate element is ignored, and the set remains unchanged
  7. a) set.remove(element)
  8. a) It returns the total number of elements in the set
  9. a) set = {1, 2, 3, 4, 5}
  10. b) Removes a random element from the set and returns it
  11. a) Sets are mutable, while frozensets are immutable
  12. c) The union of two sets contains all unique elements from both sets
  13. a) Clears all elements from the set
  14. a) set.intersection(set2)
  15. a) {1, 2, 3, 4, 5}
  16. c) set.issubset(other_set)
  17. d) Returns the symmetric difference between the two sets
  18. d) Returns the symmetric difference between the two sets
  19. b) {1, 2}
  20. a) Checks if the set is a proper superset of another set

0 Comments:

Post a Comment

Popular Posts

Categories

AI (33) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (146) C (77) C# (12) C++ (82) Course (67) Coursera (198) Cybersecurity (24) data management (11) Data Science (106) Data Strucures (8) Deep Learning (13) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (21) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (46) Meta (18) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (3) PHP (20) Projects (29) Python (893) Python Coding Challenge (285) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses