MSBTE PYTHON MOCK PAPER 02 - MODEL ANSWER
Q1 a) Describe decision making statements with example.
i) if-elif-else:
Used to execute 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 multiple levels of decision-making.
Example:
x = 25
if x > 10:
if x < 30:
print("x is between 10 and 30")
Output:
x is between 10 and 30
Q2 c) Write a program to calculate the sum of digits of a given number 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