Python Question Bank - Class Test 2
Python Question Bank - Class Test 2
2 Marks Questions
Local Scope: A variable defined inside a function is local to that function and cannot be accessed
outside.
def f():
x = 10 # Local variable
print(x)
f()
Global Scope: A variable defined outside any function is global and can be accessed inside
functions, but to modify it inside a function, you need to use the global keyword.
x = 10 # Global variable
def f():
global x
f()
print(x) # Output: 20
Q.3.Write use of matplotlib package in python and give its type - 2 mks
Matplotlib is a powerful library in Python used for creating various types of plots and charts. It's
widely used for data visualization.
def Add(self):
print("Addition: ", self.num1+self.num2)
def Sub(self):
print("Subtraction: ", self.num1-self.num2)
class B(A):
def Multi(self):
print("Multiplication: ", self.num1*self.num2)
obj=B()
obj.Add()
obj.Sub()
obj.Multi()
Q.6. Differenciate between method overloading and method overriding in
Python
Q.7. WAP to read contents of first.txt file and write same content
in the second.txt file. - 2 mks
# Open the first file in read mode and read its content
with open('first.txt', 'r') as first_file:
content = first_file.read()
# Open the second file in write mode and write the content to it
with open('second.txt', 'w') as second_file:
second_file.write(content)
● 'r': Read Mode → Opens the file for reading (default mode).
● 'w': Write Mode → Opens the file for writing. Creates a new file or overwrites an
existing file.
● 'a': Append Mode → Opens the file to append content. Creates a file if it doesn’t exist.
● 'r+': Read and Write Mode → Opens the file for both reading and writing.
● 'x': Exclusive Creation → Creates a file but raises an error if the file already exists.
● 'b': Binary Mode → Opens the file in binary mode (used with other modes).
● 't': Text Mode → Opens the file in text mode (default, used with other modes).
4 Marks Questions
Q.1.Explain three built-in list functions. - 4 mks
Python provides a wide range of built-in functions that simplify programming tasks. These
functions can be categorized based on their purposes, such as type conversion, mathematical
operations, etc.
int(x)
c): Converts x to an integer.
int("42") # Output: 42
3. Utility Functions
The open() function in Python is used to open a file and return a file object, which allows you to
interact with the file (reading, writing, etc.). It takes at least one argument, the file path, and
optionally a mode (to specify how the file should be opened).
ii) The write() Function
The write() function is used to write data to a file. It is available after opening a file in a write or
append mode ('w', 'a')
Q.3. Explain Numpy package in detail. 4 mks
NumPy is a key package in Python. It helps you work with arrays and matrices, which are
collections of numbers, and provides many tools to perform mathematical operations on
them efficiently.
1. NumPy Arrays
import numpy as np
● You can perform mathematical operations on entire arrays at once, which makes
your code shorter and faster.
arr = np.array([1, 2, 3, 4])
print(arr * 2) # Output: [2 4 6 8]
● You can easily get parts of an array, like selecting a row from a table or a slice of a
list.
class A:
num1 = int(input("Enter First Number: "))
num2 = int(input("Enter Second Number: "))
def Add(self):
print("Addition: ", self.num1 + self.num2)
def Sub(self):
print("Subtraction: ", self.num1 - self.num2)
class B(A):
def Multi(self):
print("Multiplication: ", self.num1 * self.num2)
def Div(self):
print("Division: ", self.num1 / self.num2)
Method overloading allows a class to have multiple methods with the same name but
different parameters. However, Python does not support traditional method overloading
as seen in languages like Java. Instead, Python handles similar functionality using default
arguments or by checking arguments within a single method.
Method Overriding in Python
Definition: Method overriding happens when a subclass provides its own version of a method
that is already defined in its superclass. The method in the subclass has the same name and
parameters (signature) as the method in the superclass, but it gives a new implementation.
By restricting direct access to data, data hiding promotes encapsulation, which enhances code
security, prevents unintended modifications, and ensures that object properties remain in a
consistent and valid state.
Q.8. Describe the method of handling exceptions in Python using try and
except blocks. Include an example and its syntax.
Exception Handling
Exception handling is a mechanism that prevents programs from crashing when errors
occur. An exception is an error that happens during program execution, like dividing by
zero or trying to open a missing file.
try-except Block
The try block contains the code that may cause an error, while the except block
catches and handles the exception.
Q.9.Write a Python program to open a file, write data to it, and then read the
content also mention the steps in file handling.