MSBTE PYTHON MOCK PAPER 02 - MODEL ANSWER
Q1 a) Describe decision making statements with example.
i) if-elif-else:
Used to select one block of code among many based on condition.
Example:
x = 10
if x > 0:
print("Positive")
elif x == 0:
print("Zero")
else:
print("Negative")
Output:
Positive
ii) Nested if:
An if block within another if. Used for hierarchical condition checking.
Example:
x = 25
if x > 10:
if x < 30:
print("x is between 10 and 30")
Output:
x is between 10 and 30
Q2 a) Explain use of following operators with example.
i) Bitwise:
a = 5 # 0101
b = 3 # 0011
print(a & b) # AND operation
Output:
1
MSBTE PYTHON MOCK PAPER 02 - MODEL ANSWER
ii) Arithmetic:
a=8
b=3
print(a + b)
Output:
11
iii) Logical:
x = True
y = False
print(x and y)
Output:
False
Q2 b) Difference between list and tuple.
List:
- Mutable (can be changed)
- Defined using []
- Slower due to dynamic behavior
Tuple:
- Immutable (cannot be changed)
- Defined using ()
- Faster and hashable
Q2 c) Sum of digits using function.
def sum_of_digits(n):
total = 0
while n > 0:
total += n % 10
n //= 10
return total
print(sum_of_digits(123))
MSBTE PYTHON MOCK PAPER 02 - MODEL ANSWER
Output:
6
Q2 d) Zero Division Error handling.
try:
result = 10 / 0
except ZeroDivisionError:
print("Division by zero is not allowed.")
Output:
Division by zero is not allowed.
Q3 a) Interchange two tuple variables.
t1 = (1, 2)
t2 = (3, 4)
t1, t2 = t2, t1
print("t1 =", t1)
print("t2 =", t2)
Output:
t1 = (3, 4)
t2 = (1, 2)
Q3 b) Four methods of list in Python.
1. append(x) - Adds an item to the end.
2. pop() - Removes and returns last element.
3. insert(i, x) - Inserts at position i.
4. remove(x) - Removes first occurrence of x.
Q3 c) User-defined module to display program name.
# mymodule.py
def display_program(name):
print("Program Name:", name)
MSBTE PYTHON MOCK PAPER 02 - MODEL ANSWER
# main.py
import mymodule
mymodule.display_program("Calculator")
Output:
Program Name: Calculator
Q3 d) Data Hiding and Abstraction with advantages.
Data Hiding: Prevents access to class internals using private variables.
Abstraction: Hides complexity using abstract layers.
Advantages:
- Secure code
- Easier maintenance
- Reusability
Q4 a) Dictionary operations for students.
students = {1: "Ravi", 2: "Amit", 3: "Raj"}
students[2] = "Shreyas"
del students[1]
print(students)
Output:
{2: 'Shreyas', 3: 'Raj'}
Q4 b) Indexing and slicing in lists.
lst = [10, 20, 30, 40, 50]
print(lst[0]) # Indexing
print(lst[1:4]) # Slicing
Output:
10
[20, 30, 40]
MSBTE PYTHON MOCK PAPER 02 - MODEL ANSWER
Q4 c) Four built-in functions with example.
1. len() - Returns length
2. type() - Returns data type
3. input() - Takes user input
4. print() - Outputs data
Example:
x = input("Enter name: ")
print("Hello", x)
Q4 d) Multiple inheritance example.
class A:
def show(self):
print("Class A")
class B:
def display(self):
print("Class B")
class C(A, B):
pass
obj = C()
obj.show()
obj.display()
Output:
Class A
Class B
Q4 e) Write and append to file.
with open("file.txt", "w") as f:
f.write("Line 1\n")
with open("file.txt", "a") as f:
MSBTE PYTHON MOCK PAPER 02 - MODEL ANSWER
f.write("Line 2\n")
Output:
File will contain:
Line 1
Line 2
Q5 a) Use of data types with examples.
int x = 10
float y = 3.14
str s = "Python"
list l = [1,2,3]
tuple t = (1,2)
dict d = {"a":1}
Each holds different type of data.
Q5 b) Six set functions with example.
s1 = {1,2,3}
s2 = {3,4,5}
s1.add(6)
print(s1.union(s2))
print(s1.intersection(s2))
print(s1.difference(s2))
print(s1.issubset({1,2,3,4,5,6}))
print(s1.isdisjoint({7,8}))
Q5 c) Class with inheritance and method overriding.
class Diploma:
def getdiploma(self):
print("I got a diploma")
class CO(Diploma):
def getdiploma(self):
print("I am with CO diploma")
MSBTE PYTHON MOCK PAPER 02 - MODEL ANSWER
class IF(Diploma):
def getdiploma(self):
print("I am with IF diploma")
d = Diploma()
c = CO()
i = IF()
d.getdiploma()
c.getdiploma()
i.getdiploma()
Output:
I got a diploma
I am with CO diploma
I am with IF diploma
Q6 a) Keywords with example.
continue:
for i in range(5):
if i == 2:
continue
print(i)
pass:
if True:
pass
break:
for i in range(5):
if i == 3:
break
print(i)
Q6 b) User defined exception for password check.
class PasswordError(Exception):
MSBTE PYTHON MOCK PAPER 02 - MODEL ANSWER
pass
password = input("Enter password: ")
try:
if password != "admin123":
raise PasswordError("Wrong password!")
else:
print("Correct password")
except PasswordError as e:
print(e)
Output: (based on input)
Wrong password!
Q6 c) Standard packages with example.
NumPy:
import numpy as np
arr = np.array([1,2,3])
print(arr)
Pandas:
import pandas as pd
df = pd.DataFrame({"Name": ["A", "B"], "Age": [20, 21]})
print(df)
Matplotlib:
import matplotlib.pyplot as plt
plt.plot([1,2,3], [4,5,6])
plt.show()