University Question Paper Solutions 2022
Unit 3: Functions and Modules
Que. Write python program using function to find greatest of three
numbers by passing numbers as argument.
def maximum(a, b, c):
list = [a, b, c]
return max(list)
print(maximum(10,12,14))
Output: 14
Que. Write python program using function to find whether number is odd
or even.
def evenodd(num):
if(num%2==0):
print(num," is even")
else:
print(num," is odd")
evenodd(3)
Output: 3 is odd
Que. Write python program using function to accept the number from user and
compute
i. Square root of Number
ii. Cube of Number
iii. Square of Number
def calc(num1,num2,num3):
sqroot = num1 ** 0.5
cube = num2 * num2 * num2
sqr = num3 * num3
print("Square root of num1 is", sqroot)
print("Cube of num1 is", cube)
print("Square of num1 is", sqr)
calc(16,5,8)
Output:
Square root of num1 is 4.0
Cube of num1 is 125
Square of num1 is 64
Que. Program to convert binary number to decimal number using function.
def decimalToBinary(n):
return bin(n).replace("0b","")
print(decimalToBinary(8))
print(decimalToBinary(18))
print(decimalToBinary(7))
Output:
1000
10010
111
Unit 4: Strings
Que. Write a python program to display tables from 1 to 10 using
formatting character
for x in range(1,11):
print("MULTIPLICATION TABLE FOR %d\n" %(x))
for y in range(1,11):
print("%d * %d = %d" %(x, y, x*y))
Output: It displays tables from 1 to 10
Que: Write python, program to find whether a given character is present in
a string or not. In case it is present print the index at which it is present. Do
not use built in string method.
str1 ="Python Programming"
str2="thon"
if str2 in str1 :
print ("Yes, String found")
print("Index number is",str1.find("thon")
else :
print ("No, String not found")
Que. Write a Python program to remove the characters which have odd index values
of a given string.
str="abcdef"
result = ""
for i in range(len(str)):
if i % 2 == 0:
result = result + str[i]
print(result)
Output: ace
Que. Write python program to find
i. Length of input string
ii. Maximum of the character of input string without using built-in function.
str="hi"
print("length of string is", len(str))
if str[0]>str[1]:
print(str[0],"is max")
else:
print(str[1],"is max")
Output:
length of string is 2
i is max
Unit 5: Object Oriented Programming
Que. Write a python program that uses class to store exam number and
marks of four subjects. Use list to store the marks of four subjects.
class student:
def __init__(self, examno, marks):
self.examno = examno
self.marks = marks
list = []
list.append(student(101,48) )
list.append(student(102,40) )
list.append(student(103,44) )
for obj in list:
print( obj.examno, obj.marks, sep =' ' )
output:
101 48
102 40
103 44
Que. Write a python program to create class car with two attributes name &
cost. Create two objects and display information.
class car:
name="maruti"
cost=500000
obj1=car()
print(obj1.name)
print(obj1.cost)
Output:
maruti
500000
Unit 6: File Handling and Directories
Que. Write a python program that reads data from one file and write into
another file and line by line
f1 = open('file1.txt', 'r') #open file1 in reading mode
f2 = open('file2.txt','w') #open file2 in writing mode
for line in f1: #read from file1 and write to file2
f2.write(line)
f1.close() #close f1 and f2
f2.close()
Que. Write a python program that counts the number of tabs and new line
characters in a file.
with open("data.txt") as file:
text = file.read()
tab = 0
space = 0
newline = 0
for char in text:
if char == '\t':
tab += 1
if char == ' ':
space += 1
if char == '\n':
newline += 1
print("Tabs:", tab)
print("Spaces:", space)
print("Newlines:", newline)
Output:
Tabs: 0
Spaces: 2
Newline: 2