A sample BS Math Python Exam
A sample BS Math Python Exam
MIDTERM EXAMINATION
Paper: MATH 106 – Computer Programming with Python
Programme: BS (Mathematics)
Maximum Marks: 30 Time Allowed: 90 Minutes
Note: Write, before solving the paper, your roll number with signature here: ___________________
Note: Write neatly and cleanly. Each mark and symbol is meaning full in a source code. So use them carefully. Use
indentations while writing computer programs. NO EXTRA TIME --- NO EXTRA SHEET
Q. 1 What is output of following snippet (tell line-by-line that which output is the result of which statement):
x = "3" 0.5
y = """5""" +
z = x+y 0.5
print(type(z)) +
x1 = x*7 0.5
print(type(x1)) +
print(x1) 1.5
y += x +
print( "First value=" , x , "Second value=" , y , \ 0.5
"Third value=" , x1*2 ) +
y1 = int(y)+9.0 0.5
print(type(y1)) +
print(y1) 0.5
y2 = y+9 +
print(type(y2)) 0.5
print(y2)
Q. 2 Write a program to find the distance of the point (8, −2,4) from the plane 8𝑥 + 2𝑦 + 3𝑧 − 9 = 0.
The distance 𝑑 between a point (𝑥 ∗ , 𝑦 ∗ , 𝑧 ∗ ) and a plane 𝐴𝑥 + 𝐵𝑦 + 𝐶𝑧 + 𝐷 = 0 is given by,
6
|𝐴𝑥 ∗ + 𝐵𝑦 ∗ + 𝐶𝑧 ∗ + 𝐷|
𝑑=
√𝐴2 + 𝐵2 + 𝐶 2
Q. 3 A palindrome is a number or a text phrase that reads the same backward as forward. For example,
each of the following five-digit integers is a palindrome: 12321, 55555, 45554 and 11611. Write a
program that reads in a five-digit integer and determines whether it is a palindrome. 5+2
Hint: Find the reverse of the given number, then compare the original number with its reverse. If
both are same then the number is palindrome.
Q. 4 An electric power supply company charges 7.74 Rupees per unit (KW/Hour) for the first 100 units
of electricity consumed and 10.06 Rupees per unit for the rest of the units (i.e., units after 100)
6
from the consumers. Write a program that asks the user to enter the number of units consumed
and finds the electricity charges for the consumed units.
Q. 5 Write a program that inputs a year and find whether it is leap year or not.
A leap year is the one which is evenly(exactly) divisible by 4. For example, 2016 is a leap year, 6
whereas 2013, 2014, 2015, 2017, 2018, and 2019 are not leap years.
FINAL EXAMINATION
Paper: MATH 106 – Computer Programming with Python
Programme: BS (Mathematics)
Maximum Marks: 50 Time Allowed: 120 Minutes
Note: Write, before solving the paper, your roll number with signature here: ___________________
Note:
• Write nothing except the roll number and signature on the question paper.
• Submit question paper with your answer sheet.
• Write neatly and cleanly.
• Each mark and symbol is meaning full in a source code. So use them carefully.
• Use indentations while writing computer programs.
• NO EXTRA TIME will be allowed.
• NO EXTRA SHEET will be provided.
Q. 1 Write a program to assign two variables by an assignment statement. Interchange the values and
2
print the result on the screen (without using a third variable).
Q. 2 Write a program that asks the annual income of a business from the user and calculate the
income tax according to the following formula/rate issued by FBR for the year 2020-21.
Q. 3 Write down four ways of creating a count-controlled for loop in Python. Give example of each style. 8
Q. 4 A palindrome is a number or a text phrase that reads the same backward as forward. For example,
each of the following integers is a palindrome: 88, 898, 8998, and 57275. Write a program that 7
reads in any integer and determines whether it is a palindrome.
Q. 5 A company pays its employees weekly according to their hourly wage up to 40 hours, and 50%
more for the additional hours (overtime). Write a program that uses a while statement to receive
7
values of “hourly_rate” and “hours_worked” for an arbitrary number of employees and finds and
prints the gross pay for each employee.
Q. 7 What is the output of the following snippet (assume any 14-digit integer to express a value of id):
def cub(q): 3
print("The id of the parameter before modification:" , id(q))
q = q**3
print("The id of the parameter after modification:" , id(q))
return q
p=4
print("The value of the argument:" , p)
print("The id of the argument:" , id(p))
newp = cub(p)
print("The received result: " , newp)
print("The id of the received result:" , id(newp))
print("The id of the argument:" , id(p))
Q. 8 Explain the difference between a void function and a value-returning function. Also tell how the
2
code structures of these two types of functions differs from each other.
Q. 12 Write a simple snippet that creates a one-dimensional numpy array of 6 elements and then add a
2
scalar value to each element of the array using broadcasting.
PRACTICAL EXAMINATION
Paper: MATH 106 – Computer Programming with Python
Programme: BS (Mathematics)
Maximum Marks: 100 Time Allowed: 120 Minutes
Q1 (for all students): Write a Python script to compute the tax chargeable on the capital gain on selling an immovable
property. The tax rate on capital gains on sale of an immovable property is given below:
Moreover, the amount of any gain arising on disposal of an immovable property is computed, for tax purposes, according
to the following formula:
S. Taxabl
Holding Period of Property
No. e Gain
1 Where the holding period of an immoveable property does not exceed one year 100%
2 Where the holding period of an immoveable property exceeds one year but does not exceed two years 75%
3 Where the holding period of an immoveable property exceeds two years but does not exceed three years 50%
4 Where the holding period of an immoveable property exceeds three years but does not exceed four years 25%
5 Where the holding period of an immoveable property exceeds four years 0%
To start, the program needs as input from the user: (1) holding period of the immovable property to be sold, (2) Original
purchase price, and (3) final selling price. Definitely then gain equals selling price minus purchase price. Find and print
the gain and the tax. Make a function to compute the gain tax.
Practically,
Purchase Price Selling Price Gain Holding years Gain Tax
2000000 3200000 1200000 3.5 21000
2000000 3200000 1200000 1.2 31500
2000000 3200000 1200000 0.5 42000
2000000 3200000 1200000 4.1 0
Q2 (for even roll numbers): Write a program to approximate the value of cosine of an angle by using the formula:
𝑛
𝑥2 𝑥4 𝑥6 𝑥8 𝑥 2𝑛 𝑥 2𝑘
cos 𝑥 = 1 − + − + − ⋯ + (−1)𝑛 = 1 + ∑(−1)𝑘
2! 4! 6! 8! 2𝑛! (2𝑛)!
𝑘=1
Here 𝑥 in radians. The program should ask an integer angle in degrees from 0 to 90.
The program should ask and find cosine values for an arbitrary number of degree values. (Not just one value of degree).
Note: Practically,
For number of terms: 7, Angle= 30 degrees. cos( 30 ) = 0.866026
For number of terms: 7, Angle= 45 degrees. cos( 45 ) = 0.707107
For number of terms: 7, Angle= 60 degrees. cos( 60 ) = 0. 500001
Q3 (for odd roll numbers): Write a program to approximate the value of 𝑒 𝑥 by using the formula:
𝑛
𝑥 𝑥2 𝑥3 𝑥4 𝑥𝑛 𝑥𝑛
𝑒𝑥 = 1 + + + + +⋯+ =1+∑
1! 2! 3! 4! 𝑛! 𝑛!
𝑖=1
The program should ask and find cosine values for an arbitrary number of degree values. (Not just one value of degree).
Note: Practically,
For number of terms: 8, x= 6. E power x = 341.8
For number of terms: 6, x= 4. E power x = 48.5556