0% found this document useful (0 votes)
3 views9 pages

Python Programming Exam Model Answers

Uploaded by

aishwaryarkarle
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)
3 views9 pages

Python Programming Exam Model Answers

Uploaded by

aishwaryarkarle
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

Master of Computer Applications (MCA – I)

Course: IT11 – Python Programming


Term End Examination – Model Answers (50 Marks)

Q1. (a) Create a list Animal = ['Cat','Dog','Tiger'] and perform operations.

Theory:
A list in Python is a mutable, ordered data structure used to store multiple values in a
single variable.
Lists support insertion, deletion, and modification of elements.

Diagram:
[ Cat ] -> [ Dog ] -> [ Tiger ]

Program:
Animal = ['Cat', 'Dog', 'Tiger']

[Link]('Lion') # 1. add Lion

[Link]('Dog') # 2. remove Dog

[Link](1, 'Deer') # 3. insert Deer at index 1

print("Final List:", Animal)

print("Length:", len(Animal))

Explanation:
append() adds element at end,
remove() deletes specified element,
insert() places element at given index.

Q1. (b) Perform string operations on the given sentence.

Course Owner: Dr. Rupali Kalekar


Theory:
Strings are immutable sequences of characters.
replace() is used to substitute a substring.
split() converts a string into a list.

Diagram:
'I LOVE PYTHON PROGRAMMING'
|
replace

'I LOVE JAVA PROGRAMMING'

Program:
sentence = "I LOVE PYTHON PROGRAMMING"
sentence = [Link]("PYTHON", "JAVA")
words = [Link]()
print(sentence)
print(words)

Q1. (c) Program to find the largest of three numbers using if-elif-else.

Theory:
Conditional statements allow decision making based on conditions.

Flow Diagram:
Start → Input a,b,c → Compare → Print Largest → End

Program:
a = int(input("Enter first number: "))

b = int(input("Enter second number: "))

c = int(input("Enter third number: "))

if a >= b and a >= c:

print("Largest:", a)

elif b >= a and b >= c:

Course Owner: Dr. Rupali Kalekar


print("Largest:", b)

else:

print("Largest:", c)

Q2. (a) Write a function returning sum and product.

Theory:
Functions help in modular programming.
Python allows returning multiple values using tuples.

Diagram:
Input → Function → (Sum, Product)

Program:
def calculate(a, b):
return a+b, a*b

s, p = calculate(10, 5)
print("Sum:", s)
print("Product:", p)

Q2. (b) Explain decorator with example.

Theory:
A decorator modifies behavior of a function without changing its code.

Diagram:
Function → Decorator → Modified Output

Program:
def uppercase(func):
def wrapper():
return func().upper()

Course Owner: Dr. Rupali Kalekar


return wrapper

@uppercase
def message():
return "hello world"

print(message())

Module for Simple Interest

[Link]

def simple_interest(p, r, t):


return (p * r * t) / 100

[Link]

import interest

si = interest.simple_interest(1000, 5, 2)
print("Simple Interest:", si)

b) Multiple Exception Handling


try:
x = int(input("Enter number: "))
y = int(input("Enter number: "))
print(x / y)
except ZeroDivisionError:
print("Cannot divide by zero")
except ValueError:
print("Invalid input")
except TypeError:
print("Type error occurred")

Q3. (a) Password validation using Regular Expression.

Course Owner: Dr. Rupali Kalekar


Theory:
Regular Expressions are patterns used to match character combinations.
re module provides regex support in Python.

Diagram:
Password → Regex Check → Valid / Invalid

Program:
import re
pattern = r'^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[@$!%*?&]).+$'

password = input("Enter password: ")


if [Link](pattern, password):
print("Valid Password")
else:
print("Invalid Password")

Q3. (b) Abstract class Shape with Circle and Square.

Theory:
Abstract classes define methods without implementation.
They enforce method overriding in derived classes.

Diagram:
Shape (abstract)
/ \
Circle Square

Program:
from abc import ABC, abstractmethod
import math

class Shape(ABC):
@abstractmethod
def area(self):
pass

Course Owner: Dr. Rupali Kalekar


class Circle(Shape):
def __init__(self, r):
self.r = r
def area(self):
return [Link] * self.r * self.r

class Square(Shape):
def __init__(self, s):
self.s = s
def area(self):
return self.s * self.s

BankAccount Class
class BankAccount:
def __init__(self):
[Link] = 0

def deposit(self, amount):


[Link] += amount

def withdraw(self, amount):


if amount <= [Link]:
[Link] -= amount
else:
print("Insufficient Balance")

def display_balance(self):
print("Balance:", [Link])

acc = BankAccount()
[Link](1000)
[Link](500)
acc.display_balance()

Course Owner: Dr. Rupali Kalekar


Multithreading
import threading

def odd():
for i in range(1, 21, 2):
print("Odd:", i)

def even():
for i in range(2, 21, 2):
print("Even:", i)

t1 = [Link](target=odd)
t2 = [Link](target=even)

[Link]()
[Link]()

Q4. MongoDB program using Python (PyMongo).

Theory:
MongoDB is a NoSQL document-based database.
Data is stored in collections and documents.

Diagram:
Database
|
Collection (Product_info)
|
Documents

Program:
from pymongo import MongoClient

client = MongoClient("mongodb://localhost:27017/")
db = client["StoreDB"]
col = db["Product_info"]

db.Product_info.insertMany([

Course Owner: Dr. Rupali Kalekar


{ProductID:1, ProductName:"Keyboard", Category:"Electronics", Price:700, Stock:50},

{ProductID:2, ProductName:"Mouse", Category:"Electronics", Price:500, Stock:80},

{ProductID:3, ProductName:"Monitor", Category:"Electronics", Price:9000, Stock:20},

{ProductID:4, ProductName:"Chair", Category:"Furniture", Price:1500, Stock:40},

{ProductID:5, ProductName:"Laptop", Category:"Electronics", Price:55000, Stock:10}

])

// Price between 500 and 1000

db.Product_info.find({Price: {$gte:500, $lte:1000}})

// Update stock

db.Product_info.updateMany(

{ProductName:"Keyboard"},

{$set:{Stock:100}}

// Top 5 by Stock

db.Product_info.find().sort({Stock:-1}).limit(5)

// Max price product

db.Product_info.find().sort({Price:-1}).limit(1)

// Electronics category

db.Product_info.find({Category:"Electronics"})

Course Owner: Dr. Rupali Kalekar


Q5. Django Project – CollegeSite.

Theory:
Django follows MVT architecture.
M – Model (Database)
V – View (Business logic)
T – Template (UI)

Diagram:
User → URL → View → Template → Response

Steps:
Create Project & App
django-admin startproject CollegeSite
cd CollegeSite
python [Link] startapp student
[Link]
from [Link] import HttpResponse

def welcome(request):
return HttpResponse("Welcome to Django Programming")
[Link]
from [Link] import path
from [Link] import welcome

urlpatterns = [
path('', welcome),
]

Course Owner: Dr. Rupali Kalekar

You might also like