✅ UNIT-3: Object-Oriented Programming in Python
1. Difference between Class and Object:
A class is a blueprint or template that defines the structure and behavior (attributes and
methods) of objects.
An object is a real-world instance created from a class. It has actual data stored in memory.
2. Why objects are called instances of a class?
Objects are called instances because they are created from the class using the class
constructor. They represent specific versions of the class with actual values.
3. What does the self argument signify in class methods?
self refers to the current instance of the class and is used to access variables and methods
associated with the object from within class methods.
4. Significance of __init__() method with an example:
__init__() is a constructor method in Python that runs automatically when a new object is
created. It initializes object attributes.
Example:
class Student:
def __init__(self, name):
[Link] = name
5. How is a function created in Python?
Functions are defined using the def keyword, followed by the function name and
parentheses.
Example:
def greet():
print("Hello")
6. Describe anonymous functions with examples:
Anonymous functions are defined using the lambda keyword and do not have a name.
Example:
square = lambda x: x ** 2
print(square(4)) # Output: 16
7. Purpose of lambda functions in Python:
Lambda functions are used for writing short, one-line functions without defining a full function
using def. Often used with map(), filter(), and sorted().
8. Differentiate between class variables and instance variables:
Class variables are shared across all instances of a class and defined inside the class but
outside methods.
Instance variables are specific to each object and defined using self inside methods.
9. Differentiate Package and Namespace:
A package is a collection of modules grouped together in a directory with an __init__.py file.
A namespace is a container that holds variable and function names to avoid naming
conflicts.
---
✅ UNIT-4: File Handling and Exceptions
1. Distinguish open() and file() functions:
open() is the built-in function used to open files in Python 3.
file() was used in Python 2 and is not available in Python 3.
2. Purpose of using with open() instead of open():
with open() automatically handles closing the file even if an error occurs, ensuring better
resource management.
3. Difference between try-except and try-finally:
try-except handles specific exceptions and prevents crashes.
try-finally ensures that cleanup code runs regardless of whether an exception occurs.
4. Advantages of command-line arguments:
They allow input to be passed during script execution, making programs more dynamic and
customizable without changing the code.
5. What happens when an exception arises? Example:
The program control jumps to the corresponding except block to handle the error.
Example:
try:
a = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
6. Difference between exception and error:
Errors are severe issues like syntax errors that crash the program.
Exceptions are run-time issues that can be handled using try-except.
7. Exception raised in finally block:
If an exception occurs in finally, it overrides exceptions from try or except, possibly hiding
them.
8. Text files vs Binary files:
Text files store characters and can be read with a text editor.
Binary files store data like images or videos in binary form and need special programs to
read.
9. Difference between try-except and if-else:
try-except is for handling errors that occur during execution.
if-else is for making decisions based on conditions.
10. Assertion vs Exception:
Assertions are used for debugging to test conditions.
Exceptions handle unexpected run-time errors during normal execution.
---
✅ UNIT-5: Regular Expressions, GUI, and Threads
1. What is a regular expression? List 3 regex symbols:
A regex is a pattern used to match character combinations in strings.
Common symbols:
. → Matches any character
* → Matches 0 or more repetitions
^ → Matches start of string
2. What is a GUI? Name 3 GUI toolkits:
A Graphical User Interface (GUI) allows users to interact visually with programs using
windows, buttons, etc.
Toolkits: Tkinter, PyQt, wxPython
3. Purpose of layout managers in GUI:
Layout managers handle the automatic arrangement of widgets (like buttons, labels) within a
window, maintaining consistency and flexibility in design.
4. Distinguish between threads and processes:
A process is an independent program with its own memory space.
A thread is a lightweight subprocess sharing memory with other threads in the same
process, used for multitasking.