Thursday, 12 June 2025

Revision worksheet

1. This method returns an integer that specifies the current position of the file object.
a) seek() b) load() c) position() d) tell()

2.Answer the below ASSERTION(A) AND REASONING(R) based questions. (5x1=5)
Mark the correct choice as :
(a)Both A and R are true and R is the correct explanation for A.
(b)Both A and R are true and R is not the correct explanation for R.
(c)A is true but R is false.
(d)A is false but R is true.
Assertion (A): For a binary file opened using 'rb' mode, the pickle.dump() displays an error. 
Reason (R): The pickle.dump() method is used to read from a binary file. 

3. Predict the output of :
myfile=open('rhyme.txt','w+')
myfile.write("""Hot cross buns
Hot cross buns
One a penny
Two a penny
Hot cross buns""")
myfile.seek(0)
s=myfile.read(5) ; print(s)
s=myfile.readline(5) ; print(s)
s=myfile.read(5) ; print(s)
s=myfile.readlines() ; print(s)
myfile.close()

4. What will be the output of the following code ?
a) def   fun1( ):
            x=15
            print(x)
   x = 12
   fun1( )
   print(x)
 b)def   test( ):
        print(age)
        age=17
  age = 16
  test( )
  print(age)
c) def grade(a,b,c):
       print(b,a,c)
   a,b,c=9,5,2
   grade(c,a,b)
   print(b,c,a)

5. Which of the following is not a correct statement for binary files?
a. Easy for carrying data into buffer    b. Much faster than other file systems
c. Characters translation is not required d. Every line ends with new line character ‘\n’

6. To read at the most 2 bytes of a line from a file object infile, we use _________
a. infile.read(2) b. infile.readline() c. infile.readline(2) d. infile.readlines(2)

7.Write a program to accept five sports name from the user into a list sname and write them in a file “sports.txt”.Make sure that they’re written on separate lines in the file. Further , display the contents of the file.
8. Write a function that accepts two parameters : a dictionary and a number; and prints only the keys that have values more than the passed number.

9. Rewrite the following code snippet to accept the value of a and to pass it to a function to compute the following. The default value of a should be 25,if no argument has been sent. Also, replace the while loop with a for loop. Predict the output if the value entered by the user is 50.
  a=25
while a < 500:
print a
a = a + 25

10.a)Predict the output for the following python program.  Fill in the blank to ensure that no  new local variable wand is used in the function magic().Predict the new output.
wand=25
def magic(w):
__________________
    wand =w*4
    print("wand : ",wand)
print("wand : ",wand)
magic(wand)
print("wand : ",wand)  

b) What is the output of ?
total=0 
def add(a,b): 
    global total
    print(total)  
    total=a+b 
    print(total)
add(6,6) 
print(total)

11. Read the code given below and answer the questions
f1=open('main.txt','w+') 
f1.write('Bye') 
f1.close() 
If the file contains 'Good ' before execution(cursor on the same line), what will be the content of the file after execution of the code ?

12. Find output generated by the following code:
Str='Computer'
print(Str[-2::-1])
Str=Str[-6:6]*2
print(Str)

13.Answer the following:
i) By default python names the segment with top-level statement as __________________
a) def main() b) main() c) _ _main_ _ d) _main_

ii)Suppose lst is [2,4,6,8,10,12,24], What is  the list after lst.pop(3)?
a)[2,4,6,8] b)[8,10,12,24] c)[2,4,8,10,12,24] d)[2,4,6,10,12,24]

iii)What will happen if you try to open a binary file using a text ?
i) Default value ii) Advance value iii)Garbage value iv)Parameter value

14. What is the output of the following code? 
print("We don\t talk any more")
print(len("We don\t talk any more"))
print("We don\t talk any more"[5:10])
b)


15. Write a function called authenticate(users,loginid,password) which takes following 3 parameters:
> users : a dictionary storing login ids and corresponding password values
> loginid : a string for a login name and 
> password : a string for a password

The function should do the following: 
i) if the user exists and the password is correct for the given loginid, it should print ‘Access granted
ii) if the user exists and the password is incorrect for the given loginid , it should print ‘Incorrect password’.
iii)If the user does not exist for the given loginid , it should print ‘wrong credentials’

16. Write a program to read following details (as long as the user wants) of sports’ performance (sport, competitions, prizes-won) of your school and store into a csv file delimited with tab character, where :
sport => sports name the school participated in
competitions => no. Of interschool competitions participated in
prizes-won=> no. Of competitions won

17. Write a program that copies a  text file ‘source.txt’ onto ‘target.txt’ barring the lines starting with an ‘@’ sign.

18. A binary file "Student.dat" has the structure :
{'stud_id':int, 'stud_name': string,  'percentage': float}
Write a menu driven program using functions for the following:
a> Accept the details of a student.
b> Display the details of all students and the average percentage in tabular format
c> Display the name and marks of students scoring 75% and above

No comments:

Post a Comment