1. Which keyword is used to define a function in Python?
A) def
B) function
C) func
D) method
Show Explanation
2. What is the main difference between a for loop and a while loop in Python?
A) for loop is faster than while loop
B) for loop iterates over a sequence; while loop continues until a condition is false
C) while loop cannot iterate over a sequence
D) There is no difference
Show Explanation
3. What is a unique characteristic of a set in Python?
A) A set is ordered
B) A set can contain duplicates
C) A set is an unordered collection of unique elements
D) A set can store multiple data types
Show Explanation
4. What does the len() function do in Python?
A) Returns the length of a string only
B) Returns the last element of a list
C) Returns the first element of a list
D) Returns the number of items in an object
Show Explanation
5. In a class method, what does the 'self' parameter refer to?
A) The class itself
B) The instance of the class
C) A global variable
D) None of the above
Show Explanation
6. What is the primary difference between a dictionary and a list in Python?
A) A dictionary contains key-value pairs, while a list contains ordered items
B) A list can only contain numbers
C) A dictionary is ordered, while a list is unordered
D) There is no difference
Show Explanation
7. What is the purpose of the try-except block in Python?
A) To define a function
B) To create a loop
C) To import a module
D) To handle exceptions or errors
Show Explanation
8. What is the key characteristic of a tuple in Python?
A) A tuple can be modified
B) A tuple is an ordered list
C) A tuple is immutable
D) A tuple can only contain strings
Show Explanation
9. How do you include a module in a Python program?
A) import module_name
B) include module_name
C) require module_name
D) load module_name
Show Explanation
10. Which method is used to add an element to the end of a list in Python?
A) insert()
B) extend()
C) add()
D) append()
Show Explanation
11. What is the relationship between a class and an object in Python?
A) A class is a type of object
B) A class is a blueprint for creating objects
C) Objects define classes
D) There is no relationship
Show Explanation
12. What is a module in Python?
A) A file containing Python code
B) A keyword for defining classes
C) A built-in function
D) A data type
Show Explanation
13. What is the purpose of the 'return' statement in a function?
A) To exit a function and return a value
B) To define a function
C) To print a value
D) To create a loop
Show Explanation
14. What is a list comprehension in Python?
A) A way to create lists using a single line of code
B) A way to create tuples
C) A method for manipulating dictionaries
D) A function to iterate over lists
Show Explanation
15. What does the if __name__ == '__main__': statement do in Python?
A) It imports a module
B) It allows code to run only when the module is executed directly
C) It defines a function
D) It creates a class
Show Explanation
16. What does the 'sorted()' function do in Python?
A) Returns a new sorted list from the elements of an iterable
B) Sorts a list in place
C) Converts a string to a list
D) None of the above
Show Explanation
17. What does the 'map()' function do in Python?
A) Filters items in an iterable
B) Reduces an iterable to a single value
C) Applies a function to all items in an iterable
D) None of the above
Show Explanation
18. What does the 'filter()' function do in Python?
A) Maps a function to an iterable
B) Filters elements from an iterable based on a condition
C) Sorts an iterable
D) None of the above
Show Explanation
19. What does the 'reduce()' function do in Python?
A) Reduces the size of a list
B) Combines two lists
C) Applies a function cumulatively to the items of an iterable
D) Filters elements from an iterable
Show Explanation
20. What does the 'len()' function do in Python?
A) Counts elements in a list
B) Returns the first element of a list
C) Returns the number of items in an object
D) None of the above
Show Explanation