Most Probable Questions
Most Probable Questions
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.”
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()
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()
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()
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()
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()
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()
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