We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4
CLASS XII
PRETEST: PYTHON REVISION TOUR, FUNCTIONS
SECTION A 1. In text file each line is terminated by a special character called _______ 2. (EOL) 3. Can we read file without opening. (T/F) 4. Which mode create new file if the file does not exist? a. write mode b. append mode c. Both of the above d. None of the above 5. Which statement will return one line from a file (file object is ‘f’)? a. f.readline( ) b. f.readlines( ) c. f.read( ) d. f.line( ) 6. Which of the following will read entire content of file(file object ‘f’)? a. f.reads( ) b. f.read( ) c. f.read(all) d. f.read( * ) 7. The process of dividing a computer program into separate independent blocks of code with specific functionalities is known as _________. a. Programming b. Modular Programming c. More Programming d. Step Programming 8. Consider the following code and choose the incorrect statement.: def s(n1): print(id(n1)) n2=4 s(n2) print(id(n2)) a. Function name is ‘s’ b. Function ‘s’ is taking one parameter. c. Both print statement will print the same value. d. Both print statement will print different value. 9. The part of the program where a variable is accessible is known as the __________ of that variable a. scope b. module c. part d. none of the above 10. How many built-in functions are used in the following code: a = int(input("Enter a number: ") b=a*a print(" The square of ",a ,"is", b) a. 1 b. 2 c. 3 d. 4 SECTION B 11. Write a program to read first 10 characters from a file named “data.txt” f = open(“data.txt”,”r”) data = f.read(10) print(data) 12. Accept five sports names from the user and write in a file “sport.txt” (each name should write in separate line) f = open("name.txt","w") for i in range(5): n = input("Enter sport name") f.write(n) f.write('\n') f.close() 13. Name the function / method required for [CBSE SQP 2015] Finding second occurrence of m in madam. get the position of an item in the list. Answer: find index 14. Which string method is used to implement the following: To count the number of characters in the string. To change the first character of the string in capital letter. To check whether given character is letter or a number. To change lowercase to uppercase letter. Change one character into another character. [CBSE TextBook] Answer: len(str) str.capitalize() ch.isalnum() str.upper() str.replace(old,new) 15. Rewrite the following for loop into while loop: [CBSE Text Book] for a in range(25,500,25): print a Answer: a=25 while a < 500: print a a = a + 25 SECTION C: 16. Explain “in” and “not in” membership operator with an example. 17. Rewrite the following code in Python after removing all syntax errors(s). Underline each correction done in the code. [CBSE Delhi-2016] for Name in [Amar, Shveta, Parag] if Name [0] = ‘s’: Print (Name) Answer: for Name in [“_Amar”, ”_Shveta_” , "_Parag_”] : if Name [0] E == ‘S’ : Print (Name) Answer: for Name in [“_Ramesh_”, “_Suraj_” , “_Priya_”] if Name [0] =_=‘S’ : print (Name) SECTION D 18. Accept five hobbies from the user and write in a file “hobby.txt” (each hobby should write in separate line) without using write() function. Ans. f = open("hobby.txt","w") h=[] for i in range(5): n = input("Enter hobby name") h.append(n,'\n') f.writelines(h) f.close() SECTION E 19. Write a function in python to search and display details, whose destination is “Cochin” from binary file “Bus.Dat”. Assuming the binary file is containing the following elements in the list: Bus Number Bus Starting Point Bus Destination Ans: import pickle def countrec(): fobj=open("bus.dat","rb") num = 0 try: while True: rec=pickle.load(fobj) if rec[2]=="Cochin" or rec[2]=="cochin": num = num + 1 print(rec[0],rec[1],rec[2]) except: fobj.close() return num n=countrec() # This function is called just to verify result print(n)