0% found this document useful (0 votes)
5 views2 pages

MSBTE Python Mock Paper 02 Answers

The document provides a model answer for a Python mock paper, detailing decision-making statements such as if-elif-else and nested if, along with examples. It also includes a function to calculate the sum of digits of a given number, demonstrating its use with an example input. The outputs for both examples are specified, showing the expected results of the code snippets.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

MSBTE Python Mock Paper 02 Answers

The document provides a model answer for a Python mock paper, detailing decision-making statements such as if-elif-else and nested if, along with examples. It also includes a function to calculate the sum of digits of a given number, demonstrating its use with an example input. The outputs for both examples are specified, showing the expected results of the code snippets.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like