POC Modules, Libraries & Debugging 1 — Questions and Answers
Question 1: What is the purpose of the 'import' statement in Python?
- To define a new function
- To execute code line-by-line
- To include external modules or libraries (Correct answer)
- To comment out code
Correct answer: To include external modules or libraries
The `import` statement in Python is used to bring external modules or libraries into the current program's namespace. This allows you to access functions, classes, and variables defined in those modules, promoting code reusability and modularity. It does not define new functions or execute code line-by-line in the way described.
Question 2: Which module is commonly used for debugging in Python?
- debug
- trace
- inspect
- pdb (Correct answer)
Correct answer: pdb
The `pdb` (Python Debugger) module is Python's standard interactive source code debugger. It allows developers to set breakpoints, step through code execution, inspect variables, and evaluate expressions at runtime. This functionality is crucial for identifying and resolving issues in Python programs.
Question 3: Which function can display the attributes of a module?
- vars()
- help()
- dir() (Correct answer)
- type()
Correct answer: dir()
The `dir()` function is used to get a list of valid attributes for an object, including functions, classes, and variables. When called on a module, it returns a sorted list of names defined in that module. This is useful for exploring the contents and capabilities of an imported module.
Question 4: What does 'pip' do in Python?
- Runs Python code
- Compiles Python programs
- Installs Python packages (Correct answer)
- Uninstalls Python itself
Correct answer: Installs Python packages
`pip` stands for 'Pip Installs Packages' and is the standard package-management system used in Python. Its primary function is to install and manage software packages (libraries) from the Python Package Index (PyPI) or other sources. It simplifies the process of adding external dependencies to your Python projects.
Question 5: How do you check for syntax errors in Python?
- Only during runtime
- Using a linter or running the code (Correct answer)
- Through print statements
- Cannot be detected
Correct answer: Using a linter or running the code
Syntax errors are fundamental errors in the structure of the code that prevent Python from understanding and executing it. They are typically detected either by a linter (a static code analysis tool) before runtime, or by the Python interpreter itself when you attempt to run the code. The interpreter will raise a `SyntaxError` immediately upon parsing.
Question 6: Which library is used for numerical operations in Python?
- os
- numpy (Correct answer)
- random
- json
Correct answer: numpy
NumPy (Numerical Python) is a foundational library in Python, widely used for scientific computing and data analysis. It provides powerful N-dimensional array objects and a collection of functions for performing complex mathematical and numerical operations efficiently. Other modules like `os`, `random`, and `json` serve different purposes.
Question 7: What does the traceback module provide?
- Memory usage report
- List of installed modules
- Error traceback information (Correct answer)
- Import history
Correct answer: Error traceback information
The `traceback` module in Python provides a standard interface to extract, format, and print stack trace information. When an exception occurs, the traceback details the sequence of function calls that led to the error. This information is invaluable for debugging and understanding the root cause of program failures.
Question 8: Which of the following is used to handle exceptions?
- if-else
- try-except (Correct answer)
- switch-case
- for loop
Correct answer: try-except
The `try-except` block is Python's primary mechanism for handling exceptions, which are errors that occur during program execution. Code that might raise an error is placed within the `try` block, and if an exception occurs, the corresponding `except` block is executed, allowing the program to gracefully recover or handle the error without crashing.
Question 9: Which file extension is used for Python modules?
- .py (Correct answer)
- .pym
- .pt
- .exe
Correct answer: .py
Python source code files, including those intended to be imported as modules, are conventionally saved with the `.py` file extension. This extension helps the operating system and Python interpreter identify the file as a Python script or module. Other extensions like `.pym` or `.pt` are not standard for Python modules.
What is the purpose of the 'import' statement in Python?