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

Python Lab End Exam Questions CSE5

Uploaded by

balupersonal224
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)
28 views

Python Lab End Exam Questions CSE5

Uploaded by

balupersonal224
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
You are on page 1/ 14

Problem solving with Python Lab End Exam Important Questions CSE 5

Note: This is only for CSE 5 Section

1. Design an algorithm and draw a flowchart to swap the values of two variables entered
by user.
Flowchart

Output:

Algorithm
Input: Two values to be swapped
Procedure:
Step 1: Start
Step 2: Assign two values to variables a and b
Step 3: Store a into temp, Store b into a, Store temp into b
Step 4: Display a and b
Step 5: End
Output: Values after swapping

2. Draw a flowchart and design an algorithm to compute factorial of any number entered
by user.
Flowchart

Output:
Algorithm
Input: A number
Procedure:
Step 1: Start
Step 2: initialize variable count to 1
Step 3: initialize variable result to 1
Step 4: repeat until count > n
Step 4.1: result = result * count
Step 4.2: count = count + 1
Step 5: Display value of ‘result’
Step 6: End
Output: Factorial of the number

3. Develop an algorithm and flowchart to reverse the digits of an integer entered by user.
Flowchart
Output:

Algorithm
Input: An integer
Procedure:
Step 1: Start
Step 2: Read the number entered by user into variable n
Step 3: initialize variable s to 0
Step 4: repeat until n=0
Step 4.1: Store value of n%2 in variable r
Step 4.2: s = s*10 + r
Step 4.3: n = floor(n/10)
Step 5: Display value of s
Step 6: End
Output: Reverse of entered number
4. Develop an algorithm, flowchart and pseudocode to check whether the given number
is prime or not.
Flowchart
Output:

5. Develop a python program to check whether given year is leap year or not.
Program:
def is_leap_year(year):
if year % 4 != 0:
return False
elif year % 100 == 0:
return year % 400 == 0
else:
return True

year = 2024
if is_leap_year(year):
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")

Output:

6. Design a python program that prints numbers from “start” to “end” using while loop,
where “start” and “end” values are entered by user.
Program:
n1=input("Enter the irst number:")
n2=input("Enter the second number:")
if(n1.isdigit() and n1.isdigit()):
n1=int(n1)
n2=int(n2)
while(n1<=n2):
print(n1)
n1+=1
else:
print("Invalid input entered")

Output

7. Write a python program that prints multiplication table of any number entered by user.
Allow the user to specify the table size also.
Program:
n=int(input("Enter the table number :"))
m=int(input("Enter the table upto which number :"))
for i in range(1,m+1):
print("%d x %d = %d"%(n,i,n*i))
Output:
8. Write a Python program to compute the HCF of two integers.
Program:
n1 = int(input("Enter irst number: "))
n2 = int(input("Enter second number: "))
if(n1<n2):
least=n1
else:
least=n2
for i in range(1,least+1):
if(n1%i==0 and n2%i==0):
hcf=i
print("HCF is ",hcf)

Output:

9. Design a python program that takes a sentence as input and prints the first half and
second half separately.
Program:
s="Python Programming"
l=len(s)
half=l//2
print("First half is ",s[:half])
print("Second half is ",s[half+1:])
Output
First half is Python Pr
Second half is gramming

10. Create a program that utilizes Python’s built-in modules (math, random, etc.) to
perform various calculations and tasks. Develop a custom module with string
manipulation functions, and import it into your main program.
Program for built in modules
import math,random
print(math.sqrt(25))
print(random.randint(1,100))

Output

Program for creating user-defined module (strman.py)


def strcat(s1,s2):
s1 = s1+" "+s2
print("after manipulation",s1)

Program for importing user-defined module


import strman
strman.strcat("hello","world")
Output

11. Design a python program to implement bubble sort that sorts a list of item quantities
entered by the user.
Program:
#Bubble sort program
values=[30,15,40,50,20,10]
for i in range(0,len(values)):
for j in range(0,len(values)-i-1):
if values[j]>values[j+1]:
temp=values[j]
values[j]=values[j+1]
values[j+1]=temp
print(values)

Output:
12. Write a program to generate fibonnacci series upto n terms using recursion.
Program
def ibonacci(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return ibonacci(n - 1) + ibonacci(n - 2)
for i in range(5):
print( ibonacci(i))
Output
0
1
1
2
3

13. Perform the set operations union, intersection and difference with the help of a python
program.
Program:
set1={10,15,30,'hello',5.6}
set2={17,20,5.6,15}
print("union:",set1.union(set2))
print("intersection:",set1.intersection(set2))
print("difference:",set1.difference(set2))
Output:

14. Create a python program to generate list of squares of even numbers between 1 and
20.
Program:
even_squares = [i**2 for i in range(2,21,2)]
print(even_squares)

Output:

15. Implement a personal library system using object-oriented principles. Define classes
for books, authors, and genres. Create methods to: (i) Add new books. (ii) Search for
books by author or genre. (iii) Display a list of books in the library.
Program
class books:
titles = ["Programming in C", "Java complete Reference", "Wednesday Soul"]
authors = ["Balagurusamy", "Herbert Schildt", "Sorabh Pant"]
genres = ["education", "education", "comedy"]

b1 = books()
while(True):
print("enter your choice")
choice = input("1.Add new Book, 2.Search by Genre, 3.Display, 4.End")
if(choice=='1'):
title = input("enter title")
author = input("enter author")
genre = input("enter genre")
b1.titles.append(title)
b1.authors.append(author)
b1.genres.append(genre)
elif(choice=='2'):
genre = input("enter genre")
for i in range(len(b1.genres)):
if(b1.genres[i]==genre):
print(b1.titles[i], b1.authors[i], b1.genres[i])
elif(choice == '3'):
for i in range(len(b1.titles)):
print(b1.titles[i], b1.authors[i], b1.genres[i])
elif(choice == '4'):
break
else:
print("please enter valid choice")

Output
16. Create a base class Person with attributes name and age. Derive two classes Student
and Teacher. Use composition to include an Address class in both derived classes.
Program:
class person:
name = "vikram"
age = 25

class student(person):
address = "Vijayawada"

class teacher(person):
address = "Hyderabad"

st = student()
te = teacher()

print("Details from student class:")


print(st.name, st.age, st.address)
print("Details from teacher class:")
print(te.name, te.age, te.address)

Output:
17. Design a python program to find factorial of a given number using recursion.
Program
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
print(factorial(5))
Output
120

18. Explain the concept of binary search on a list of values with an example program.
Program
prices = [220, 320, 1100, 340, 650, 30, 500, 630]
prices.sort()
print(prices)
key = 630
left = 0
right = len(prices)-1
while(left<=right):
mid = (left+right)//2
if(key==prices[mid]):
print("key found at index",mid)
break
elif(key>prices[mid]):
left=mid+1
else:
right = mid-1
else:
print("key not found")

Output

19. Implement a case-insensitive search algorithm to find a keyword in a large body of


text for a document search feature.
Program
text = "This is a sample Text considered in the python program to Perform case insensitive search"
text = text.lower()
keyword = "PERFORM"
keyword = keyword.lower()
textlist = text.split(" ")
for i in range(len(textlist)):
if(textlist[i]==keyword):
print("keyword is at index",i)
break
else:
print("keyword not found")

Output

20. Explain the concept of linear search on a list of values with an example program.
Program
elements = [22, 32, 11, 34, 65, 3, 5, 63]
key = 3
for i in range(len(elements)):
if(key == elements[i]):
print("key is found at index",i)
break
else:
print("key not found")
Output
key is found at index 5

You might also like