Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Question Bank (PWP)

1) Differentiate between Local and Global variable.

Aspect Local Variables Global Variables


Scope Defined within a Defined outside any
function or block function or block
Accessibility Accessible only within Accessible throughout
the function or block the entire program
where they are defined
Lifetime Created when the Created when the
function or block is program starts and
executed and destroyed exists until program
when it exits termination or explicitly
deleted
Initialization Must be explicitly Can be initialized
initialized within the outside any function or
function or block where block, or inside functions
they are used using the global keyword
Visibility Not visible outside their Visible and accessible
scope from anywhere in the
program
Modifiability Can be modified only Can be modified from
within the function or anywhere in the
block where they are program
defined
Encapsulation Promotes encapsulation May lead to potential
by limiting access to naming conflicts and
data within specific parts complexity due to wide
of code accessibility
Error Handling Accessing an undefined Accessing an undefined
local variable or global variable usually
misspelling it results in a does not throw an error
NameError
Memory Usage Consumes memory only Consumes memory
when the function or throughout the
block is executed program's execution
2) Explain directory methods in Python.

3) Explain various function of Math Module.

4) Write a python program using functions?

5) Differentiate between Functions, Modules and Packages?


6) Explain the Numpy package, Scipy, Matp lotlib and Panda Package.
7) Write a Python Program to perform following operations on Dictionary?
8) Write a Python Program to perform following operations on Set: Intersection of Sets, Union
of Sets, Set Difference.

9) Design a python program to create class to calculate area of triangle and circle and print the
result?
10) Write a Python Program to calculate factorial of given number.

11) Describe various modes of file object? Explain with an example.


1) r : Opens file for reading mode.
2) r+ : Opens file for both reading and writing.
3) w : Opens file for writing only.
4) w+ : Opens file for both reading and writing.
5) a : Opens file for appending.
6) a+ : Opens file for both appending and reading.
7) rb : Opens file for reading only in binary format.
8) rb+: Opens file for both reading and writing in binary format.
9) wb : Opens file for writing only in binary format.
10)wb+: Opens file for both reading and writing in binary format.
11)ab : Opens file for appending in binary format.
12)ab+: Opens file for both appending and reading in binary format.

file_r = open("example.txt", 'r') # Read Mode


file_r_plus = open("example.txt", 'r+') # Read and Write Mode
file_w = open("example.txt", 'w') # Write Mode
file_w_plus = open("example.txt", 'w+') # Read and Write Mode
file_a = open("example.txt", 'a') # Append Mode
file_a_plus = open("example.txt", 'a+') # Append and Read Mode
file_rb = open("image.jpg", 'rb') # Read Binary Mode
file_rb_plus = open("image.jpg", 'rb+') # Read and Write Binary Mode
file_wb = open("image.jpg", 'wb') # Write Binary Mode
file_wb_plus = open("image.jpg", 'wb+') # Read and Write Binary Mode
file_ab = open("image.jpg", 'ab') # Append Binary Mode
file_ab_plus = open("image.jpg", 'ab+') # Append and Read Binary Mode

12) Write a python program for importing module for addition and subtraction of 2 numbers.
13) Write a python program to define a module for swapping the two values?

14) Write a python program to create class STUDENT with Roll Number and Name and display
its contents.
15) Explain class inheritance in Python with an example?

- Inheritance lets us create new classes that automatically have the attributes and
behaviors of existing classes.
- It's like passing down traits from parents to children. For example, a "Dog" class
can inherit traits like "Animal" from a superclass.
- With inheritance, we can reuse code and avoid repeating the same code in
multiple classes.
- Subclasses can add their own unique features or change existing ones without
affecting the superclass.
- Inheritance helps in organizing code logically and makes it easier to understand
and manage large projects.
16) Write a python program of Inheritance?

OR
17) Differentiate between Inheritance and composition class?

Aspect Inheritance Composition


Definition It's a mechanism where It's a design pattern
a class inherits where a class contains
properties and behaviors an instance of another
from another class. class.
Relationship Represents an "is-a" Represents a "has-a"
relationship, where one relationship, where one
class is a type of another class has an object of
class. another class as a part of
its structure.
Code Reusability Promotes code Promotes code
reusability by inheriting reusability by using
methods and attributes objects as building
from the parent class. blocks to create complex
structures.
Flexibility Provides flexibility in Provides flexibility in
modifying and creating complex objects
extending the behavior by combining simpler
of classes through objects.
inheritance hierarchies.
Example python class Animal: \n python class Engine: \n
def sound(self): \n def start(self): \n
print("Animal sound") print("Engine started")
\n\n class Dog(Animal): \n\n class Car: \n def
\n def sound(self): \n __init__(self): \n
print("Dog barks") \n\n self.engine = Engine()
dog = Dog() \n \n\n car = Car() \n
dog.sound() # Outputs: car.engine.start() #
Dog barks Outputs: Engine started
18) Differentiate between method Overloading and method Overriding? Write an example

Method Overloading:

 Method overloading means having multiple methods with the same name but
different types or numbers of inputs.
 In Python, we use default values or variable arguments to achieve method
overloading.
 It helps us write cleaner code by having one method name for similar operations.
Method Overriding:

 Method overriding means changing the behavior of a method in a subclass that was
defined in the superclass.
 It allows us to customize how a method works in different classes.
 In method overriding, the method name and parameters remain the same.
 Example:

19) Write a python program for importing module for addition and subtraction of 2 numbers.
20) Explain file handling and Exception Handling with Example.

File handling and exception handling are crucial aspects of programming in Python. Here's a
concise explanation along with an example for a 4-mark response:

1. **File Handling** (2 marks):


- File handling in Python involves operations like reading from, writing to, and managing
files on the disk.
- Python provides built-in functions like `open()`, `read()`, `write()`, and `close()` for file
handling operations.

2. **Exception Handling** (2 marks):


- Exception handling in Python is used to handle runtime errors or exceptional situations
gracefully.
- It involves using `try`, `except`, `else`, and `finally` blocks to catch and handle exceptions.

**Example**:

This example combines both file handling and exception handling.


It defines a function `read_file` to read from a file and handles
potential errors such as the file not being found (`FileNotFoundError`)
or being unable to read (`IOError`). The function also catches any unexpected errors
(`Exception`) and prints appropriate error messages.

You might also like