1. Which of the following is not a valid data type in Python?
a) int
b) float
c) char
d) str
Explanation: Python does not have a char data type. Strings are used to represent characters in Python.
2. What is the output of the following code?
x = 5
y = 10
z = x + y
print(z)
a) 15
b) 5
c) 10
d) Error
Explanation: The code adds the values of x and y and prints the result, which is 15.
3. How do you comment a single line in Python?
a) // This is a comment
b) # This is a comment
c) /* This is a comment */
d) — This is a comment
Explanation: The # symbol is used for single-line comments in Python.
4. What is the purpose of the len() function in Python?
a) To find the length of a string
b) To find the length of a list
c) To find the length of a tuple
d) All of the above
Explanation: The len() function can be used to find the length of various data types, including strings, lists, and tuples.
5. Which of the following is a mutable data type in Python?
a) tuple
b) string
c) list
d) None of the above
Explanation: Lists are mutable, meaning their elements can be changed after creation. Tuples and strings are immutable.
6. What is the output of the following code?
x = 5
y = 10
if x > y:
print("x is greater than y")
else:
print("y is greater than x")
a) x is greater than y
b) y is greater than x
c) x is equal to y
d) Error
Explanation: The if condition checks if x is greater than y. Since x is not greater than y, the else block is executed.
7. What is the purpose of the for loop in Python?
a) To iterate over a sequence of values
b) To execute a block of code repeatedly
c) To define a function
d) To create a class
Explanation: The for loop is used to iterate over a sequence of values, such as a list or a range of numbers.
8. What is the output of the following code?
def greet(name):
print("Hello, " + name + "!")
greet("Alice")
a) Hello, Alice!
b) Error
c) Alice
d) None
Explanation: The greet function takes a name as input and prints a greeting message using that name.
9. What is the purpose of the import statement in Python?
a) To define a function
b) To create a variable
c) To load a module
d) To print a message
Explanation: The import statement is used to load modules, which contain functions and classes that can be used in your code.
10. Which of the following is a built-in function in Python?
a) my_function()
b) print()
c) custom_function()
d) user_defined_function()
Explanation: print() is a built-in function in Python that is used to print output to the console.
Additional Questions
11. What is the difference between a list and a tuple in Python?
Lists are mutable, while tuples are immutable.
12. What is the purpose of the while loop in Python?
The while loop is used to execute a block of code repeatedly as long as a condition is true.
13. What is a dictionary in Python?
A dictionary is a data structure that stores key-value pairs.
14. How do you create a class in Python?
Use the class keyword followed by the class name.
15. What is the purpose of the self keyword in Python?
The self keyword is used to refer to the current instance of a class.
16. What is the difference between a function and a method in Python?
A function is a block of code that can be reused, while a method is a function associated with a class.
17. What is the purpose of the try-except block in Python?
The try-except block is used to handle exceptions, which are errors that occur during program execution.
18. What is the difference between == and is in Python?
== compares the values of two objects, while is compares the identity of two objects.
19. What is the purpose of the with statement in Python?
The with statement is used to manage resources, such as files, in a more concise and safe way.
20. What is the difference between a shallow copy and a deep copy in Python?
A shallow copy creates a new object but references the same underlying data, while a deep copy creates a completely new object with its own copy of the data.