0% found this document useful (0 votes)
2 views6 pages

ST-3 MCQ

The document contains a series of questions and answers related to exception handling, modules, and file operations in Python. Key topics include the purpose of exception handling, how to define and import modules, and methods for file manipulation. It serves as a quiz format to assess knowledge on these programming concepts.

Uploaded by

Harshita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

ST-3 MCQ

The document contains a series of questions and answers related to exception handling, modules, and file operations in Python. Key topics include the purpose of exception handling, how to define and import modules, and methods for file manipulation. It serves as a quiz format to assess knowledge on these programming concepts.

Uploaded by

Harshita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1. What is the primary purpose of exception handling in programming?

a) To improve the efficiency of code execution


b) To handle errors and prevent program crashes
c) To make code easier to understand
d) To debug code faster
Answer: b) To handle errors and prevent program crashes

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

4. In Python, which of the following is used to handle multiple exceptions?


a) Multiple try blocks
b) Multiple except blocks
c) Nested try-except blocks
d) Multiple finally blocks
Answer: b) Multiple except blocks

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

6. What is the output of the following Python code?

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.

7. Which of the following is true about the finally block in Python?


a) It is executed only if there is an exception.
b) It is executed only if no exception occurs.
c) It is executed always, whether an exception occurs or not.
d) It is optional and not executed in any case.
Answer: c) It is executed always, whether an exception occurs or not.

8. What is the correct way to manually raise an exception in Python?


a) throw Exception("Error")
b) raise Exception("Error")
c) raise new Exception("Error")
d) throw new Exception("Error")
Answer: b) raise Exception("Error")

9. Which of the following is NOT an in-built exception in Python?


a) Key Error
b) File Not Found Error
c) Memory Error
d) Parse Error
Answer: d) Parse Error

10. What is the purpose of the else block in a try statement?


a) To execute code only if an exception occurs
b) To execute code after the finally block
c) To execute code only if no exception occurs
d) To execute code before the finally block
Answer: c) To execute code only if no exception occurs

MODULES:

1. Which of the following is true about Python modules?

o a) A module is a file containing Python definitions and statements.

o b) A module can be reused in other Python programs.

o c) A module allows logical organization of Python code.


o d) All of the above.

Answer: d) All of the above

2. What is the correct syntax to import a module in Python?

o a) import module_name

o b) include module_name

o c) require module_name

o d) use module_name

Answer: a) import module_name

3. How can you import a specific function sqrt from the math module?

o a) import math.sqrt

o b) from math import sqrt

o c) include math.sqrt

o d) from math include sqrt

Answer: b) from math import sqrt

4. What is the purpose of the __name__ variable in a Python module?

o a) To determine the module's version.

o b) To check if the module is executed as a script or imported.

o c) To store the module's name in memory.

o d) To list all functions in the module.

Answer: b) To check if the module is executed as a script or imported.

5. What will the following code print?

python

Copy code

# File: my_module.py

def greet():

print("Hello, World!")

if __name__ == "__main__":

greet()

o a) "Hello, World!" if run directly.

o b) Nothing, as __name__ is not defined.


o c) The module will throw an error.

o d) Always "Hello, World!", regardless of how it's executed.

Answer: a) "Hello, World!" if run directly.

6. Which function is used to list all functions and variables in a module?

o a) dir()

o b) help()

o c) list()

o d) show()

Answer: a) dir()

7. What is a package in Python?

o a) A collection of related Python modules.

o b) A single Python module.

o c) A library of precompiled code.

o d) An IDE feature for module management.

Answer: a) A collection of related Python modules.

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

9. Which statement is used to reload a module in Python?

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)

10. What does from module import * do?

o a) It imports only the functions from the module.

o b) It imports all names (functions, variables, classes, etc.) from the module.

o c) It imports only the classes from the module.


o d) It imports a module and renames it to *.

Answer: b) It imports all names (functions, variables, classes, etc.) from the module.

FILES AND DIRECTORIES:

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")

2. What does the r+ mode do when opening a file?

a) Opens the file for writing only


b) Opens the file for reading only
c) Opens the file for both reading and writing
d) Appends data to the end of the file

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

5. Which function is used to create a new directory in Python?

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

7. What does the os.path.exists(path) function return?

a) The size of the file or directory


b) True if the path exists, else False
c) The name of the file or directory
d) The type of the file or directory

8. Which method is used to rename a file in Python?

a) os.rename()
b) os.move()
c) os.change()
d) os.replace()

9. How can you delete a file in Python?

a) os.rmdir()
b) os.unlink()
c) os.delete()
d) os.remove()

10. Which statement is true about the with statement in file handling?

a) It is used to handle exceptions in file operations.


b) It automatically closes the file after its block of code is executed.
c) It allows only writing operations on the file.
d) It replaces the need for file modes.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy