Python MCQ Questions and Answers - Part 1
The following multiple choice questions test will test your Python knowledge:
1. Which of the following is the correct way to comment out multiple lines of code in Python?
a) // This is a comment
b) /* This is a comment /
c) # This is a comment
d) / This is a comment */
2.What will the following code output?
3. What
does the len() function do in Python?
a) Returns the logarithm of a number
b) Returns the length of a string
c) Converts a value to lowercase
d) Rounds a floating-point number
4. What is the result of 3 * 3 ** 3?
a) 9
b) 27
c) 81
d) 729
5. Which data type is used to store a sequence of characters in Python?
a) char
b) string
c) text
d) str
6. Which of the following is NOT a valid way to declare and initialize a variable in Python?
a) x = 5
b) 5 = x
c) y=5,10
d) x=y=5
7. What will the following code output?
a) 5
b) 4
c) IndexError
d) None
8.Which of the following methods is used to remove an item from a list in Python?
a) delete()
b) remove()
c) pop()
d) discard()
9. What is the output of print(list(range(2, 6)))?
a) [2,3,4,5]
b) [2,3,4,5,6]
c) [2,4,6]
d) [2,5]
10. In Python, how do you create a function?
a) create function my_function():
b) function my_function():
c) def my_function():
d) def function my_function():
11. What is the purpose of the "pass" statement in python
a) To print a message to the console
b) To Halt the program execution
c) To indicate an empty block of code
d) To return a value from a function
12. Which of the following is a mutable data type in Python?
a) int
b) str
c) tuple
d) list
13. What is the correct way to open and read a text file named "example.txt"?
a) file = open("example.txt", "r")
b) file = read("example.txt", "r")
c) file = open("example.txt")
d) file = read("example.txt")
14. What does the import statement do in Python?
a) Imports a library/module
b) Imports a variable
c) Imports a function
d) Imports a class
15. What will the following code output?
a) 15
b) 105
c) "105"
d) TypeError
16. How do you create a dictionary in python?
a) dict = {}
b) dict = {()}
c) dict = {1,2,3}
d) dict = {1:"one",2:"two"}
17. what is the purpose of the break statement in a loop ?
a) To exit the loop immediately
b) To continue to the next iteration of the loop
c) To restart the loop from the beginning
d) To skip a specific number of iteration
18. which operator is used for exponentiation in python ?
a) ^
b) **
c) //
d) %
19. what will be the following code output ?
a) [1,2]
b) [2,3,4]
c) [2,3]
d) [1,2,3]
20. what is the result of `10/3` in python ?
a) 3.333333
b) 3.0
c) 3
21. What is the purpose of the continue statement in the loop?
a) To Exit the loop.
b) To restart the loop from the beginning.
c) To skip the remaining code in the loop for the current iteration and move to the next iteration.
d) To print the loop variable.
22. what is the correct way to check if key exists in a dictionary ?
a) `if key in dict:`
b) `if dict.exists(key):`
c) `if dict.contains(key):`
d) `if key.exissts(dict):`
23. which of the following is the correct way to define a class names 'Car' in python?
a) new class Car:
b) create class Car:
c) class Car():
d) class Car:
24. what is the output of the following code snippet ?
a) 1
b) 2
c) 3
d) 10
25. which of the following is not valid way to open a file in python ?
a) `file = open("example.txt", "w")`
b) `file = open("example.txt", "r")`
c) `file = open("example.txt", "b")`
d) `file = open("example.txt", "x")`
Answer : 1-c,2-c,3-b,4-d,5-d,6-b,7-c,8-c,9-a,10-c,11-c,12-d,13-a,14-a,15-a,16-d,17-a,18-b,19-b,20-a
21-c,22-a,23-c,24-d,25-c