0% found this document useful (0 votes)
44 views

Python Exam Paper Solved1

The document contains solved questions from a university question paper on Python programming. It includes Python code snippets to calculate area of shapes, validate email addresses using regular expressions, find factorials, merge dictionaries, read files, perform set operations and add matrices.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Python Exam Paper Solved1

The document contains solved questions from a university question paper on Python programming. It includes Python code snippets to calculate area of shapes, validate email addresses using regular expressions, find factorials, merge dictionaries, read files, perform set operations and add matrices.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Solved University Question paper Set1

March-2019

Subject- Python Programming Class- MCA-II Sem-IV


_____________________________________________________________________________________

1.a).write a python function to calculate area of circle &area of square.

Answer-:

pi = 3.14159265

choice = input('Enter Choice [1 or 2]:')

choice = int (choice)

if choice == 1:

radius = input('Enter x:')

area = ( radius ** 2 ) * pi

print 'The Area is=', area

if choice == 2:

side = input('Enter x:')

area = side ** 2

print 'The Area is=', area

1.b).write a python code to validate email address using regular


expression.

Answer-:

email2="[email protected]"

email1="[email protected]'"

email="[email protected]"
email3="[email protected]"

email4="[email protected]"

email5="[email protected]"

email6="[email protected]"

re.search('\w+[.|\w]\w+@\w+[.]\w+[.|\w+]\w+',email)

x=re.search('\w+[.|\w]\w+@\w+[.]\w+[.|\w+]\w+',email2)

x.group()

[email protected]'

x=re.search('\w+[.|\w]\w+@\w+[.]\w+[.|\w+]\w+',email1)

x.group()

[email protected]'

x=re.search('\w+[.|\w]\w+@\w+[.]\w+[.|\w+]\w+',email)

x.group()

'[email protected]'

2.) Explain multilevel and multiple inheritance in python with Example

Answer-:

1) Multilevel inheritance

class Animal:   
    def eat(self):  
      print 'Eating...'  
class Dog(Animal):  
   def bark(self):  
      print 'Barking...'  
class BabyDog(Dog):  
    def weep(self):  
        print 'Weeping...'  
d=BabyDog()  
d.eat()  
d.bark()  
d.weep()  

2) Multilevel inheritance

class First(object):  
  def __init__(self):  
    super(First, self).__init__()  
    print("first")  
  
class Second(object):  
  def __init__(self):  
    super(Second, self).__init__()  
    print("second")  
  
class Third(Second, First):  
  def __init__(self):  
    super(Third, self).__init__()  
    print("third")  
  
Third();  

3.a).write a python code to find factorial of a number.

Answer-:

def factorial(n):

num = 1

while n >= 1:

num = num * n

n=n-1

return num
3.b).write a python code to merge two dictionary

Answer-:

dict1 = {'bookA': 1, 'bookB': 2, 'bookC': 3}

dict2 = {'bookC': 2, 'bookD': 4, 'bookE': 5}

print dict2.update(dict1)

4)file program

lines =[]

with open(file_name) as f:

lines.extend(f.readline() for i in xrange(N))

for last m lines

def readline_backwards(self, f):

backline = ''

last = ''

while not last == '\n':

backline = last + backline

if f.tell() <= 0:

return backline

f.seek(-1, 1)

last = f.read(1)

f.seek(-1, 1)

backline = last

last = ''

while not last == '\n':

backline = last + backline

if f.tell() <= 0:
return backline

f.seek(-1, 1)

last = f.read(1)

f.seek(-1, 1)

f.seek(1, 1)

return backline

Q5) Program to perform different set operations like in mathematics

Answer-:

# define three sets

E = {0, 2, 4, 6, 8};

N = {1, 2, 3, 4, 5};

# set union

print("Union of E and N is",E | N)

# set intersection

print("Intersection of E and N is",E & N)

# set difference

print("Difference of E and N is",E - N)

# set symmetric difference

print("Symmetric difference of E and N is",E ^ N)


6.b)to add two matrices

Answer-:

X = [[1,2,3],

[4,5,6],

[7,8,9]]

Y = [[10,11,12],

[13,14,15],

[16,17,18]]

Result = [[0,0,0],

[0,0,0],

[0,0,0]]

# iterate through rows

for i in range(len(X)):

# iterate through columns

for j in range(len(X[0])):

result[i][j] = X[i][j] + Y[i][j]

for r in result:

print(r)

You might also like