Python Programming Quiz
Python Programming Quiz
Module-I
1. What is the correct way to write a comment in Python?
A) // This is a comment
B) /* This is a comment */
C) # This is a comment
D) <!-- This is a comment -->
20. How can you format strings in Python using the format method?
A) "{0} {1}".format('Hello', 'World')
B) "{} {}".format('Hello', 'World')
C) "{} {}".format(Hello, World)
D) "{0} {1}".format(Hello, World)
Module-II
Iterative Statements
8. Which loop in Python is best suited for iterating over a sequence?
A) while
B) for
C) repeat
D) do-while
11. Which statement is used to skip the rest of the code inside a loop for the current
iteration?
A) skip
B) continue
C) pass
D) next
15. Which statement is used to end a loop early and skip the remaining code inside the
loop?
A) continue
B) exit
C) stop
D) break
17. Which keyword is used to handle code that should execute regardless of whether an
exception was raised or not?
A) finally
B) else
C) except
D) raise
20. How do you create a loop that iterates through a list in Python?
A) for item in list:
B) while item in list:
C) loop item in list:
D) for each item in list:
Module-III
1. How do you create a list in Python?
A) list = {}
B) list = ()
C) list = []
D) list = <>
Tuples
8. How do you create a tuple in Python?
A) tuple = []
B) tuple = ()
C) tuple = {}
D) tuple = <>
Strings
13. How do you initialize a string in Python?
A) string = ''
B) string = ""
C) string = '' or ""
D) Both A and B
16. What method is used to remove whitespace from both ends of a string?
A) strip()
B) trim()
C) remove()
D) delete()
Sets
17. How do you create a set in Python?
A) my_set = []
B) my_set = ()
C) my_set = {}
D) my_set = set()
Dictionaries
20. How do you create a dictionary in Python?
A) my_dict = ()
B) my_dict = {key: value}
C) my_dict = []
D) my_dict = {}
Regular Expressions
23. What symbol is used to denote zero or more occurrences of the preceding element in a
regular expression?
A) *
B) +
C) ?
D) .
26. Which quantifier matches exactly one or more occurrences of the preceding element?
A) *
B) +
C) ?
D) {n}
27. What does the special character ^ signify in a regular expression when used at the
beginning of a pattern?
A) End of the string
B) Start of the string
C) Any character
D) Grouping
Answer: B) Start of the string
Module-IV
1. What is the primary purpose of using functions in Python?
A) To reuse code and avoid repetition.
B) To store data permanently.
C) To define variables.
D) To create user interfaces.
9. What is the purpose of the *args and **kwargs syntax in a function definition?
A) To define optional and keyword arguments, respectively.
B) To define default arguments and positional arguments.
C) To handle variable-length arguments and keyword arguments.
D) To define multiple return values.
gen = simple_gen()
print(next(gen))
print(next(gen))
A) 1 2
B) 1 2 3
C) 1 1
D) 1 2 3
File Handling
15. What is the correct way to open a file for reading in Python?
A) open('file.txt', 'w')
B) open('file.txt', 'r')
C) open('file.txt', 'a')
D) open('file.txt', 'x')
19. What will be the output of the following code if data.txt contains "Hello, World!"?
with open('data.txt', 'r') as file:
content = file.read()
print(content)
A) Hello, World!
B) data.txt
C) None
D) Error
Exception Handling
11. What is an error in Python?
A) A type of variable that stores error messages.
B) An issue that occurs during the execution of a program.
C) A warning message for debugging purposes.
D) A syntax issue detected during coding.