a) size(set) b) count(set) c) len(set) d) length(set)
Correct Answer: c) len(set)
Explanation: The len() function returns the number of elements in a set.
Question 22
Which operator is used for logical AND in Python?
a) && b) || c) and d) &
Correct Answer: c) and
Explanation: The and operator is used for logical AND in Python.
Question 23
How do you create a dictionary with keys ‘a’, ‘b’, and ‘c’ and corresponding values 1, 2, and 3?
a) {‘a’: 1, ‘b’: 2, ‘c’: 3} b) [(‘a’, 1), (‘b’, 2), (‘c’, 3)] c) dict(a=1, b=2, c=3) d) Both a and c
Correct Answer: d) Both a and c
Explanation: Both methods are valid ways to create a dictionary in Python.
Question 24
What is the output of print(“Hello”.replace(“l”, “x”))?
a) Hexxo b) Hxxlo c) Hello d) HeXXo
Correct Answer: a) Hexxo
Explanation: The replace() method replaces all occurrences of "l" with "x" in the string "Hello".
Question 25
How do you remove duplicates from a list in Python?
a) remove_duplicates(list) b) list.remove_duplicates() c) list(set(list)) d) set(list)
Correct Answer: c) list(set(list))
Explanation: Converting a list to a set removes duplicates, and then converting it back to a list restores the original structure.
Question 26
What is the correct way to check if a key exists in a dictionary?
a) if key in dict b) if dict.has_key(key) c) if dict.contains(key) d) if dict.exists(key)
Correct Answer: a) if key in dict
Explanation: The in operator checks if a key exists in a dictionary.
Question 27
What is the result of type(3.14) in Python?
a) int b) float c) double d) decimal
Correct Answer: b) float
Explanation: The number 3.14 is of type float.
Question 28
How do you iterate over the keys and values of a dictionary simultaneously?
a) for key, value in dict: b) for key, value in dict.items(): c) for key in dict: d) for value in dict:
Correct Answer: b) for key, value in dict.items():
Explanation: The items() method returns both keys and values, allowing iteration over both.
Question 29
What is the difference between remove() and pop() for lists in Python?
a) remove() removes by value, pop() removes by index b) remove() removes by index, pop() removes by value c) Both remove by value d) Both remove by index
Correct Answer: a) remove() removes by value, pop() removes by index
Explanation:remove() removes the first matching value, while pop() removes an element at a specific index.
Question 30
What does the zip() function do in Python?
a) Combines two or more lists into a dictionary b) Combines elements from multiple iterables into tuples c) Creates a compressed version of a file d) Returns a list of sets
Correct Answer: b) Combines elements from multiple iterables into tuples
Explanation: The zip() function pairs elements from two or more iterables into tuples.
Question 31
How do you stop an infinite loop in Python?
a) continue b) break c) exit d) stop
Correct Answer: b) break
Explanation: The break statement is used to exit a loop prematurely, preventing infinite execution.
Question 32
What will be the output of print(bool([]))?
a) True b) False c) None d) Error
Correct Answer: b) False
Explanation: An empty list evaluates to False in a Boolean context.
Question 33
What is the function of the with statement in Python?
a) To create a new variable b) To define a loop c) To simplify the handling of exceptions d) To ensure proper cleanup of resources
Correct Answer: d) To ensure proper cleanup of resources
Explanation: The with statement is used to wrap the execution of a block of code and ensures resources are properly released, such as when working with file handling.
Question 34
What does the del keyword do in Python?
a) Deletes a file b) Removes an item from memory c) Stops the execution of a program d) Deletes a variable or object
Correct Answer: d) Deletes a variable or object
Explanation: The del keyword is used to delete variables or objects in Python.
Question 35
Which operator is used for exponentiation in Python?
a) * b) ** c) ^ d) %
Correct Answer: b) **
Explanation: The ** operator is used for exponentiation, raising a number to the power of another.
Question 36
What will be the result of print(3 + 2 * 2)?
a) 10 b) 7 c) 12 d) 8
Correct Answer: b) 7
Explanation: The multiplication is performed first, so the expression becomes 3 + 4, which equals 7.
Question 37
What is the default value of the step parameter in the range() function?
a) 0 b) 1 c) -1 d) None
Correct Answer: b) 1
Explanation: The default value of the step parameter in range() is 1, meaning the sequence increments by 1 unless otherwise specified.
Question 38
How do you check if a string contains only digits in Python?
a) isalpha() b) isdigit() c) isnumeric() d) isdecimal()
Correct Answer: b) isdigit()
Explanation: The isdigit() method returns True if all characters in the string are digits, otherwise False.
Question 39
What is the output of print(“ABCD”[1:3])?
a) AB b) ABC c) BC d) BCD
Correct Answer: c) BC
Explanation: The slice [1:3] returns characters from index 1 up to, but not including, index 3.
Question 40
Which of the following is not a valid way to create a string in Python?
a) “Hello” b) ‘Hello’ c) ”’Hello”’ d) “Hello’
Correct Answer: d) "Hello'
Explanation: Strings in Python must start and end with the same type of quotation marks.