30 Python Quiz Questions and Answers

Python is a high-level, versatile, and interpreted programming language known for its simplicity and readability. Guido van Rossum created Python in the late 1980s and released it publicly in 1991. Since then, Python has gained widespread popularity and has become one of the most widely used programming languages.

Just to let you know

Sign up for a free OnlineExamMaker account to create an interactive online quiz in minutes – automatic grading & mobile friendly.

Article outline

Part 1: 30 Python quiz questions & answers

1. What type of programming language is Python?
a) Low-level language
b) High-level language
c) Machine language
d) Assembly language
Answer: b) High-level language

2. Who is the creator of Python?
a) Steve Jobs
b) Guido van Rossum
c) Mark Zuckerberg
d) Bill Gates
Answer: b) Guido van Rossum

3. Which of the following is the correct way to print “Hello, World!” in Python?
a) print(“Hello, World!”)
b) print(“Hello, World!”)
c) echo “Hello, World!”
d) console.log(“Hello, World!”)
Answer: a) print(“Hello, World!”)

4. Python uses __________ to delimit blocks of code.
a) Curly braces {}
b) Square brackets []
c) Parentheses ()
d) Indentation (whitespace)
Answer: d) Indentation (whitespace)

5. What type of programming paradigm does Python support?
a) Object-oriented only
b) Procedural only
c) Functional only
d) Multi-paradigm
Answer: d) Multi-paradigm

6. Which of the following is an example of a Python comment?
a) # This is a comment
b) /* This is a comment */
c) // This is a comment
d)
Answer: a) # This is a comment

7. What does the “if” statement do in Python?
a) Loops through a sequence of elements
b) Defines a function
c) Creates a conditional block of code
d) Prints the value of a variable
Answer: c) Creates a conditional block of code

8. How do you define a function in Python?
a) func my_function():
b) function my_function():
c) def my_function():
d) define my_function():
Answer: c) def my_function():

9. Which of the following data types is mutable in Python?
a) int
b) float
c) str
d) list
Answer: d) list

10. What is the output of the following code snippet?

“`python
x = 5
y = 2
result = x / y
print(result)
“`

a) 2.5
b) 2.0
c) 2
d) 2.6666666666666665
Answer: a) 2.5

11. In Python, how do you access the last element of a list named “my_list”?
a) my_list[-1]
b) my_list[0]
c) my_list[len(my_list)]
d) my_list[last]
Answer: a) my_list[-1]

12. What is the purpose of the “elif” statement in Python?
a) To define a new function
b) To handle exceptions
c) To add an extra condition to an “if” statement
d) To create a loop
Answer: c) To add an extra condition to an “if” statement

Part 2: Download Python questions & answers for free

Download questions & answers for free

13. What does the “len()” function do in Python?
a) Finds the largest element in a list
b) Computes the square root of a number
c) Returns the length of a sequence (e.g., list, string)
d) Converts a string to lowercase
Answer: c) Returns the length of a sequence (e.g., list, string)

14. Which operator is used for exponentiation in Python?
a) ^
b) **
c) //
d) *
Answer: b) **

15. What is the output of the following code snippet?

“`python
x = 10
y = 3
result = x % y
print(result)
“`

a) 1
b) 0.3333333333333333
c) 3
d) 10
Answer: c) 1

16. How do you convert a string to an integer in Python?
a) int(string)
b) to_int(string)
c) str_to_int(string)
d) string_to_int(string)
Answer: a) int(string)

17. What is the purpose of the “for” loop in Python?
a) To define a new function
b) To create a conditional block of code
c) To iterate over a sequence (e.g., list, string)
d) To handle exceptions
Answer: c) To iterate over a sequence (e.g., list, string)

18. What is the output of the following code snippet?

“`python
x = [1, 2, 3]
y = x
y[0] = 100
print(x)
“`

a) [1, 2, 3]
b) [100, 2, 3]
c) [1, 2, 3, 100]
d) [100]
Answer: b) [100, 2, 3]

19. How do you check if a value exists in a list in Python?
a) value in list
b) list.contains(value)
c) list.exists(value)
d) list.find(value)
Answer: a) value in list

20. What is the purpose of the “append()” method in Python?
a) To add an element to the beginning of a list
b) To concatenate two strings
c) To add an element to the end of a list
d) To remove an element from a list
Answer: c) To add an element to the end of a list

21. What is the correct way to define a tuple in Python?
a) (1, 2, 3)
b) [1, 2, 3]
c) {1, 2, 3}
d) “1, 2, 3”
Answer: a) (1, 2, 3)

22. How do you access the value associated with a specific key in a dictionary?
a) dict[key]
b) dict.value(key)
c) dict[key]()
d) dict.key(value)
Answer: a) dict[key]

You might like to know

Create an auto-grading quiz/assessment without any coding – try OnlineExamMaker today!

23. What is the output of the following code snippet?

“`python
my_dict = {“name”: “John”, “age”: 30, “city”: “New York”}
print(my_dict.get(“gender”, “Unknown”))
“`

a) “Unknown”
b) “gender”
c) None
d) KeyError
Answer: a) “Unknown”

24. What does the “set()” function do in Python?
a) Removes duplicate elements from a list
b) Converts a list to a tuple
c) Finds the maximum value in a list
d) Converts a tuple to a list
Answer: a) Removes duplicate elements from a list

25. What is the

purpose of the “try-except” block in Python?
a) To create a loop
b) To define a new function
c) To handle exceptions
d) To add an extra condition to an “if” statement
Answer: c) To handle exceptions

26. What is the correct way to open a file named “data.txt” for reading in Python?
a) file = open(“data.txt”, “r”)
b) file = open(“data.txt”, “w”)
c) file = open(“data.txt”, “a”)
d) file = open(“data.txt”, “rb”)
Answer: a) file = open(“data.txt”, “r”)

27. Which library is commonly used for data manipulation and analysis in Python?
a) matplotlib
b) pandas
c) requests
d) numpy
Answer: b) pandas

28. What is the output of the following code snippet?

“`python
x = “Hello, Python!”
print(x[7:])
“`

a) “Hello, ”
b) “Python!”
c) “Python”
d) “o, Python!”
Answer: b) “Python!”

29. Which of the following is NOT a valid way to create a Python set?
a) {1, 2, 3}
b) set([1, 2, 3])
c) {1: “one”, 2: “two”, 3: “three”}
d) set()
Answer: c) {1: “one”, 2: “two”, 3: “three”}

30. How do you remove an item from a list in Python?
a) list.remove(item)
b) list.pop(item)
c) list.delete(item)
d) list.del(item)
Answer: a) list.remove(item)

Part 3: Free online quiz creator – OnlineExamMaker

With OnlineExamMaker software, you can easily enhance your assessment procedures, save time on grading, and gain valuable insights into learner performance. OnlineExamMaker grades quizzes automatically, and gives you access to detailed exam reports and statistics instantly. The insightful analytics help teachers and trainers gain valuable insights, enabling them to optimize their teaching methods.

Create Your Next Quiz/Exam with OnlineExamMaker

SAAS, free forever
100% data ownership