BPLCK205B - Introduction to Python Programming
Answers to Module 4 and Module 5 Question Bank
Module 4 Answers
Q1. Explain the logging module and debug the factorial of a number program
1. logging is a standard module to track events.
2. Supports levels like DEBUG, INFO, etc.
3. Useful for debugging recursive programs.
4. basicConfig() sets up logging format.
5. Used in production for diagnostics.
6. Supports logging to files or console.
7. Factorial uses recursion with logs.
8. Example:
import logging
logging.basicConfig(level=logging.DEBUG)
def fact(n): return 1 if n==0 else n*fact(n-1)
print(fact(5))
Q2. Backup folder to ZIP using modules
1. Use zipfile and os modules.
2. Walk through folder using os.walk().
3. Use ZipFile.write() to add files.
4. zipfile.ZipFile() used to create archive.
5. Make filename dynamic using folder name.
6. Can compress entire directory.
7. Ensures backup safety.
8. Example:
import zipfile, os
def backup(folder): ... (code omitted for brevity)
Q3. Difference between shutil.copy() and shutil.copytree()
1. shutil.copy() copies individual files.
2. shutil.copytree() copies full folder tree.
3. copy() needs src and dst filenames.
4. copytree() creates new dir at dst.
5. copy() overwrites if file exists.
6. copytree() raises error if dir exists.
Module 5 Answers
Q1. Function print_time() that takes a time object
BPLCK205B - Introduction to Python Programming
Answers to Module 4 and Module 5 Question Bank
1. Create class with hour, min, sec.
2. Function prints time as hh:mm:ss.
3. Uses format specifier for leading 0s.
4. Good for displaying clocks.
5. Reusable in other time-related programs.
6. Example:
class Time: ...
def print_time(t): ...
Q2. What is class and how to define/access
1. Class = blueprint for object.
2. Defined with class keyword.
3. __init__() initializes data.
4. Access using object.method/attribute.
5. self refers to instance.
6. Example:
class Car: def __init__(self, brand): self.brand = brand
Q3. Pure function and modifier, square example
1. Pure function = same input = same output.
2. No external dependency.
3. Doesn't modify outside variables.
4. Modifier changes object state.
5. Helps in functional programming.
6. Example:
def square(x): return x*x