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

Most Probable Questions

The document contains a series of Python programming exercises focused on file handling, database interaction, data structures, exception handling, and networking. Each exercise includes a problem statement followed by a sample solution, demonstrating various functionalities such as reading/writing text and CSV files, manipulating stacks, and handling exceptions. Additionally, there are examples of SQL statements for database operations, although the networking section is noted as a case study without further details.

Uploaded by

swastikyadav81
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)
4 views

Most Probable Questions

The document contains a series of Python programming exercises focused on file handling, database interaction, data structures, exception handling, and networking. Each exercise includes a problem statement followed by a sample solution, demonstrating various functionalities such as reading/writing text and CSV files, manipulating stacks, and handling exceptions. Additionally, there are examples of SQL statements for database operations, although the networking section is noted as a case study without further details.

Uploaded by

swastikyadav81
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/ 6

Most Probable Type Questions with Answers:

Chapter – File Handling in Python

TEXT FILE:

Q1)Write a function called AMCount() in Python, which should read each character of a text file notes.txt and
display the total number of occurrences of letters A and M (including lowercase ‘a’ and ‘m’ too).

For example, the content is: ”This is my website. I have displayed my preferences in the choice section.”

The AMCount() function should display the output as


A or a : 4
M or m : 2

ANSWER:

def AMWrite():
str=”This is my website. I have displayed my preferences in the choice section.”
file=open(“notes.txt”,”w”)
file.write(data)
file.close()

def AMCount():
file =open(“notes.txt”,”r”)
data=file.read()
count_a=0
count_m=0
for x in data:
if(x==’A’ or x==’a’):
Count_a=count_a+1
elif(x==’M’ or x==’m’):
Count_m=count_m+1
file.close()
print(‘A or a :’,count_a)
print(‘M or m :’,count_m)

AMWrite()
AMCount()
Q2)Write a function countmy() in Python to read the text file “data.txt” and count the number of times the word
“my” occurs in the file.
For example: if the file “data.txt” contains
“This is my website. I have displayed my preferences in the choice section.”

ANSWER:

def CountmyWrite():
str=”This is my website. I have displayed my preferences in the choice section.”
file=open(“data.txt”,”w”)
file.write(data)
file.close()

def Countmy():
file=open(“data.txt”,”r”)
data=file.read()
count_my=0
word=data.split()
for x in word:
if(x == ‘my’):
count_my=count_my+1
file.close()
print(“Total number of my is : “, count_my)

CountmyWrite()
Countmy()

Q3)A text file name “matter.txt” contains some text, which needs to displayed such that every next character is
separated by a symbol “#”. Write a function definition for hash_display() to display the entire content of the file
“matter.txt” in the desired format.
Consider the file “matter.txt”:
THE WORLD IS ROUND
The hash_display() function should display the following content:
T#H#E#W#O#R#L#D#I#S#R#O#U#N#D#

ANSWER:

def Hash_Write():
str=”THE WORLD IS ROUND”
file=open(“matter.txt”,”w”)
file.write(data)
file.close()

def Hash_Display():
file=open(“matter.txt”,”r”)
data=file.read()
for x in data:
print(x,end=’#’)
file.close()
Hash_Write()
Hash_Display()

CSV FILE:

Q4)Write a program to search the record from “data.txt” according to the admission number input from the user.
The structure of the record saved in “data.csv” is [Adm_no,Name,Class]

ANSWER:

import csv
def adm_no_write():
str=[]
file=open(“data.csv”,”a”)
data=csv.writer(file)
while True:
adm_no=int(input(“Enter admission number”))
name=input(“Enter Name”)
class=input(“Enter Class”)
str=[adm_no,name,class]
data.writerow(str)
file.close()

def adm_no_display():
file=open(“data.csv”, “r”)
data=csv.reader(file)
adm=int(input(Enter admission number: “))
for x in data:
if (x[0]==adm):
print(“Adm_no is:”,x[0])
print(“Name is:”,x[1])
print(“Class is:”,x[2])
break
else:
print(“Record Not Found”)
file.close()

adm_no_write()
adm_no_display()

Q5)Write a program to display all the product from “product.csv” file, whose price is more than 300. The format
of the record stored in “product.csv” is [product_id,product_name,price].

ANSWER:
import csv
def Product_write():
str=[]
file=open(“product.csv”,”a”)
data=csv.writer(file)
while True:
product_id=int(input(“Enter Product_id”))
name=input(“Enter Product Name”)
price=int(input(“Enter Product price”))
str=[product_id,name,price]
data.writerow(str)
file.close()

def Product_display():
file=open(“product.csv”,”r”)
data=csv.reader(file)
for x in data:
if(x[2]>300):
print(“Product_id is:”,x[0])
print(“Product_name is:”,x[1])
print(“Price is:”,x[2])
file.close()

Product_write()
Product_display()

BINARY FILE:

Q6)Write a program to display all the product from “product.dat” file, whose price is more than 300. The format
of the record stored in “product.dat” is [product_id,product_name,price].

ANSWER:
import pickle

def Product_write():
str=[]
file=open(“product.dat”,”ab”)
while True:
product_id=int(input(“Enter Product_id”))
name=input(“Enter Product Name”)
price=int(input(“Enter Product price”))
str=[product_id,name,price]
pickle.dump(str,file)
file.close()

def Product_display():
file=open(“product.dat”,”rb”)
str=pickle.load(file)
data=list(str)
for x in data:
if(x[2]>300):
print(“Product_id is:”,x[0])
print(“Product_name is:”,x[1])
print(“Price is:”,x[2])
file.close()

Product_write()
Product_display()

Chapter:- Interface of Python with SQL Database

Q1)Question related to insert/update/delete

ANSWER:
import mysql.connector
mydb=mysql.connector.connect(host=”localhost”, user=”root”,password=”root”, databse=”stud”)
mycursor=mydb.cursor()
myststement=”insert into student (Name, roll_no,class,section) values(‘{}’,{},’{}’,’{}’) .format
(Name_in,roll_no_in,class_in,section_in)”
mycursor.execute(mystatement)
mydb.commit()
mydb.close()

Q2)Question related to display/select/fetch/fetchone/fetchall

ANSWER:
import mysql.connector
mydb=mysql.connector.connect(host=”localhost”, user=”root”,password=”root”, databse=”stud”)
mycursor=mydb.cursor()
myststement=”Select * from Students”
mycursor.execute(mystatement)
myresult=mycursor.fetchall()
for x in myresult:
print(x)
mydb.close()

Q3) Other(or rest questions)

ANSWER:
import mysql.connector
mydb=mysql.connector.connect(host=”localhost”, user=”root”,password=”root”, databse=”stud”)
mycursor=mydb.cursor()
myststement=”alter table students add roll_no integer”
mycursor.execute(mystatement)
mydb.close()

Chapter:Data Structures: Stack


Q1)Julie has created a dictionary containg names and marks as key value pairs of 6 students. Write a program,
with separate user defined functions to perform the following operations
a) Push_element()- push the key(name of the student) of the dictionary into a stack, where the
corresponding value(marks0 is greater than 75.
b) Pop_element()- pop and display the content of the stack
c) Peek_element()- display the element without deletion.
For example: if the sample content of the dictionary is as follows
R={“OM” : 76,”JAI” : 45,”BOB” : 89,”ALI” : 65,”ANU” : 90,”TOM” : 82}

ANSWER:
R={“OM” : 76,”JAI” : 45,”BOB” : 89,”ALI” : 65,”ANU” : 90,”TOM” : 82}
stack=[]
def Push_element():
for x in R:
if(R[x]>75):
stack.appned(x)

def Pop_element():
while(True):
if(stack==[]):
print(“Empty or underflow”)
break
elif(stack !=[]):
print(satck.pop())

def Peek_element():
flag=0
while(flag<1):
if(stack==[]):
print(“Empty or underflow”)
break
elif(stack!=[]):
print(stack[-1])
flag=1

Push_element()
Peek_element()
Pop_element()

Q2) A list, NList contains following record as list elemnts:


[City, Country, Distance from Delhi]
Each of these records are nested together to form a nested list. Write the following user defined functions in
Python to perform the specified operations on the stack.

a) Push_element()- it takes the nested list a an argument and pushes a list object containing name of the
city and country, which are not in india and distance is greater than 3500 km from Delhi.
b) Pop_element()- it pops the objects from the satck and displays them. Also, the function should display
“Stack Empty” when there are no element in the stack.
c) Peep_element()- display the element without deletion.

ANSWER

NList=[[“New York”,”USA”,11734],[“Dubai”,”UAE”,2194],[“London”,””England”,6693]]
stack=[]
def Push_element():
for x in NList:
if(x[2]>3500):
city=x[0]
country=x[1]
data=[city,country]
stack.appned(data)

def Pop_element():
while(True):
if(stack==[]):
print(“Empty or underflow”)
break
elif(stack !=[]):
print(satck.pop())

def Peek_element():
flag=0
while(flag<1):
if(stack==[]):
print(“Empty or underflow”)
break
elif(stack!=[]):
print(stack[-1])
flag=1

Push_element()
Peek_element()
Pop_element()

Chapter: Exception Handling

Q1)Give an example code to handle “ZeroDivisionError”? the code should display the message “Division by Zero is
not allowed” in case of ZeroDivisionError exception, and the message “ Some error occurred” in case of any other
exception.

ANSWER:
result=0
try:
result=10/0
print(“Result is : “, result)
except ZeroDivisionError:
print(“Division by Zero is not allowed”)
else:
print(“Division Performed successful”)
finally:
print(“Finally block executed”)

Chapter: Networking

Case study from networking must

Chapter: Database Concept

Complete Chapter (All SQL statements) must

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy