ST-3 MCQ
ST-3 MCQ
2. Which of the following keywords is used to define a block of code that may raise an exception in
Python?
a) catch
b) try
c) throw
d) error
Answer: b) try
3. What happens if an exception is raised in a try block but not handled in an except block?
a) The program continues execution
b) The program raises an error and crashes
c) The exception is ignored
d) The program enters an infinite loop
Answer: b) The program raises an error and crashes
5. Which of these exception types is raised when a division by zero occurs in Python?
a) Value Error
b) Zero Division Error
c) Arithmetic Error
d) Type Error
Answer: b) Zero Division Error
python
Copy code
try:
x=5/0
except ZeroDivisionError:
print("Division by zero!")
finally:
print("This will always execute.")
a) Division by zero!
This will always execute.
b) Division by zero!
c) This will always execute.
d) Error during runtime.
Answer: a) Division by zero!
This will always execute.
MODULES:
o a) import module_name
o b) include module_name
o c) require module_name
o d) use module_name
3. How can you import a specific function sqrt from the math module?
o a) import math.sqrt
o c) include math.sqrt
python
Copy code
# File: my_module.py
def greet():
print("Hello, World!")
if __name__ == "__main__":
greet()
o a) dir()
o b) help()
o c) list()
o d) show()
Answer: a) dir()
8. What is the file name for a special file required to make a directory a package?
o a) __package__.py
o b) __init__.py
o c) __main__.py
o d) __module__.py
Answer: b) __init__.py
o a) reload(module_name)
o b) importlib.reload(module name)
o c) reimport(module name)
o d) load(module_name)
Answer: b) importlib.reload(module_name)
o b) It imports all names (functions, variables, classes, etc.) from the module.
Answer: b) It imports all names (functions, variables, classes, etc.) from the module.
What is the correct way to open a file named data.txt for reading in Python?
a) open("data.txt", "w")
b) open("data.txt", "r")
c) open("data.txt", "rw")
d) open("data.txt")
3. Which method is used to read the entire content of a file into a string?
a) readline()
b) readlines()
c) read()
d) readstring()
4. What happens if you try to open a non-existent file in write mode ("w") in Python?
a) Raises a FileNotFoundError
b) Opens the file for writing, creating a new file
c) Deletes all content in the directory
d) Opens the file in read mode
a) os.makedir()
b) os.mkdir()
c) os.newdir()
d) os.createdir()
6. Which module in Python is commonly used to work with files and directories?
a) sys
b) os
c) re
d) math
a) os.rename()
b) os.move()
c) os.change()
d) os.replace()
a) os.rmdir()
b) os.unlink()
c) os.delete()
d) os.remove()
10. Which statement is true about the with statement in file handling?