Class: XII
Subject: COMPUTER SCIENCE
Which of the following statement will show an error?
Q1
a) a,b,c=10,20,30
b) a b c = 10 20 30
c) abc= 10,2000,3000
d) a_b_c=10,000,50
Q2
Select all options that will print
welcome-to-python
a) print("welcome" + "to" + "python")
b) print("welcome", "to", "python", sep="-")
Q3
c) print("welcome", "to", "python", end="-")
d) print("welcome" + "-" + "to" + "-" + "python")
What is the output of the below program ?
x = 50
def func(x):
print('x is', x)
x=2
print('Changed local x to', x)
func(x)
print('x is now', x)
Q4
a) x is now 50
b) x is now 2
c) x is now 100
d) None of the mentioned
What data type is the object below L=1,23,’hello’,1
(a)List (b) array (c) dictionary (d) Tuple
Q5
S=’ ‘(single space’) Then S.isalnum() will return
(a)True (b) False (c)Error (d) None
Q6
Q7-
What will be the output of the following code-print("100+200")?
Assuming the file “new.txt” contains the following t ext.
Better late than never
One should keep trying and never give up
Leave the rest to destiny
Give the output of the following code
obj=open('new.txt','r')
S1=obj.read(5)
S2=obj.read(4)
S3=obj.readline()
print(S1,end='*')
print(S2,end='#')
print(S3)
a)
b)
Bette* r late than never#One s
Bette*r la#te than never
c)Better*late#than never
d) Better@late#than never
Find the output, if the content of sony.txt file is
Python is an interactive language
Q8-
f=open("sony.txt","r")
f.read(4)
x=f.tell()
f.read(8)
x=f.tell()
print(x)
Q9-
Q10
Q11
Q12-
a) 4
b) i
c) h
d) 12
What will be the output of the following code snippet?
f = open("data.txt", "r")
txt = "This is 1st line\n" f.writelines( txt )
f.seek(0,0)
line = f.readlines()
print ("Read Line:” , line) f.close()
a) [‘ This is 1st line\n’]
b) []
c) Error
d) None
What would be the data types of variables data in following statements?
i) Data=f.read( )
ii) Data=f.read(10)
iii) Data=f.readline()
iv)Data=f.readlines()
given text file data.txt contains :
Line1\n
\n
line3
Line 4
\n
line6
What would be the output of the above code?
Write any four functions required to handle Text files.
a)What is name of first computer network?
Q13- b)Expand NSFNET.
Q14-
a)Name the key word used for defining a function.
b)Name the built-in mathematical function / method that is used to return an
absolute value of a number.
Differentiate between Positional Argument and Default Argument of function in
Q15- python with suitable example
Differentiate between Global and Local variable with a suitable example
Q16-
Q17
Q18
Q19
Q.20
Write a function comp_sum(lista) which takes a list of integers and returns the
sum
3. Out of random() and randint(), which function should we use to generate
random numbers between
1 and 5. Justify.
1. Observe the following programs carefully, and identify the error:
def create (text, freq):
for i in range (1, freq):
print text
create(5) #function call
Rohit, a student of class 12th, is learning CSV File Module in Python. During
examination, he has been assigned an incomplete python code (shown below)
to create a CSV File 'Student.csv' (content shown below). Help him in
completing the code which creates the desired CSV File.
CSV File
1,AKSHAY,XII,A
2,ABHISHEK,XII,A
3,ARVIND,XII,A
4,RAVI,XII,A
5,ASHISH,XII,A
Incomplete Code
import
fh = open( , , newline='')
#Statement-1
#Statement-2 stuwriter = csv.
#Statement-3data = []
header = ['ROLL_NO', 'NAME', 'CLASS', 'SECTION']
data.append(header)for i in
range(5):
roll_no = int(input("Enter Roll Number : "))name = input("Enter Name
: ")
Class = input("Enter Class : ") section = input("Enter
Section : ")
rec = [
]
#Statement-4
data.append(rec)
(data)
#Statement-5fh.close()
stuwriter.
i.
Identify the suitable code for blank space in line marked as Statement-1.
a) csv file
b) CSV
c) csv
d) Csv
ii.
Identify the missing code for blank space in line marked as Statement-2?
a) "School.csv","w"
b) "Student.csv","w"
c) "Student.csv","r"
d) "School.csv","r"
iii.
Choose the function name (with argument) that should be used in the blank
space of line marked as Statement-3
a) reader(fh)
b) reader(MyFile)
c) writer(fh)
d) writer(MyFile)
iv.
Identify the suitable code for blank space in line marked as Statement4.
a) 'ROLL_NO', 'NAME', 'CLASS', 'SECTION'
b) ROLL_NO, NAME, CLASS, SECTION
c) 'roll_no','name','Class','section'
d) roll_no,name,Class,sectionc) co.connect()
v.
Choose the function name that should be used in the blank space of line
marked as Statement-5 to create the desired CSV File?
a) dump()
b) load()
c) writerows()
d) writerow()
Q22-
Amritya Seth is a programmer, who has recently been given a task to
write a python code to perform the following binary file operations
with the help of two user defined functions/modules:
a. AddStudents() to create a binary file called STUDENT.DAT
containing student information – roll number, name and
marks (out of 100) of each student.
b. GetStudents() to display the name and percentage of those
students who have a percentage greater than 75. In case
there is no student having percentage > 75 the function
displays an appropriate message. The function should also
display the average percent.
He has succeeded in writing partial code and has missed out certain
statements, so he has left certain queries in comment lines. You as
an expert of Python have to provide the missing statements and other
related queries based on the following code of Amritya.
Answer any four questions (out of five) from the below mentioned
questions.
import pickle
def AddStudents():
#1 statement to open the binary file towrite data
while True:
Rno = int(input("Rno :"))Name
= input("Name : ")
Percent = float(input("Percent :"))L = [Rno,
Name, Percent]
#2 statement to write the list Linto the file
Choice = input("enter more (y/n): ")if Choice
in "nN":
break
F.close()
def GetStudents():
Total=0
Countrec=0
Countabove75=0
with open("STUDENT.DAT","rb") as F:
while True:
try:
#3 statement to read
from the file
Countrec+=1
Total+=R[2] if
R[2] > 75:
print(R[1], " has percent =
",R[2])
Countabove75+=1
except:
break
if Countabove75==0:
print("There is no student who haspercentage
more than 75")
average=Total/Countrec
print("average percent of class = ",average)
i.
AddStudents()
GetStudents()
Which of the following commands is used to open the file
“STUDENT.DAT” for writing only in binary format? (marked as #1 in the
Python code)
a.
F= open("STUDENT.DAT",'wb')
b.
F= open("STUDENT.DAT",'w')
c.
F= open("STUDENT.DAT",'wb+')
d.
F= open("STUDENT.DAT",'w+')
ii.
Which of the following commands is used to write the list L into the
binary file, STUDENT.DAT? (marked as #2 in the Python code)
a.
pickle.write(L,f)
b.
pickle.write(f, L)
c.
pickle.dump(L,F)
d.
f=pickle.dump(L)
iii.
Which of the following commands is used to read each record from the
binary file STUDENT.DAT? (marked as #3 in the Python code)
a.
R = pickle.load(F)
b.
pickle.read(r,f)
c.
r= pickle.read(f)
d.
pickle.load(r,f)
iv.
Which of the following statements correctly explain the function of
seek() method?
a.
tells the current position within the file.
b.
c.
d.
Q23
Q24
Q25
Q26
Q27
determines if you can move the file position or not.
indicates that the next read or write occurs from that position in a
file.
moves the current file position to a given specified position
Write a program that uses a user defined function that accepts name
and gender (as M for Male, F
for Female) and prefixes Mr/Ms on the basis of the gender.
Write a program to check the divisibility of a number by 7 that is
passed as a parameter to the user defined function.
1. Write a Python statement to open a text file “DATA.TXT” so that
new contents can be written on it.
2. Write a Python statement to open a text file “DATA.TXT” so that
new content can be added to the end of file
1.What is the different in file opening mode “a” and “w” ?
2. What is the significance of adding “+‟ with file opening mode, with
context to “r+‟ ?
1. Give one difference between
A. Text file and Binary File
B. readline() and readlines()
Q28-
Write the output of the following code:
v=5
def d(N):
global v
v = 25
if N%9==0:
v=v*N
else:
v = v ** N
print(v, end="#")
d(2)
print(v)
Q29-
Suppose content of ‘story.txt’ is
All the best for your exams
Write the output of the following code :
f = open("story.txt","r")
fn = f.read().split()
print(len(fn))
Q30-
i) ____________ path contains the complete path to the file.
a. absolute path
b. mixed path
c. relative path
d. None of the above
ii) Akshita wants to store some tabular data in file. Which of the following
types of file will be suitable for her?
a. Binary File
b. CSV File
c. Text File
d. None of the above