Download as pdf
Download as pdf
You are on page 1of 26
90 a: Multiple Choice Questions Which of the following function is used to write List of Strings in a file? (a) write() (b) writeline() (c) writelines() (d) write(all) Which of the following statements are true about Text files? (@ A text file stores information in ASCII or Unicode characters (ii) In text file each line of text is terminated with a special character known as EOL, (iii) In text files, some internal translations take place when this EOL character is read or write, (iv) Default EOL character, is newline (*\n’) character. (a) only (i) (b) Only (ii) (c) (ii) and (iii) (d) All the above mentioned . Which of the following is not a text file access mode? (a) ¢ (b) a (©) w (d) wo+ |. Which of the following is not a binary file access mode? (a) tb (b) a (c) wb (@ ab . In file opening function, if no second parameter is specified then by default , the file opens in (a) write mode () read mode (c) append mode (d) none of the mentioned . What does the following statement do? x = open(‘filel.csv', ‘r') (a) Opens an existing text file named file!.csv to write (6) Opens an existing text file named filel.csv to append (©) Opens an existing text file named file csv to read (d) Opens a new file named file|.csv to read Together with” Competency Based and Objective Type Questions —12 4, Interpret the statement below: xx = open( New python.csv', ‘w') (a) Opens or creates a text file named N few-python c (0) Opens or creates a text file named New-epthonsee thon.esv to append {@) Opens or creates a tet file named Newrrtor oy, we ve fp Ome or ie nari reer 6 ea 8, Given below a code: x = open(pythoni2.csv", a") Which of the following statement is True (a) Opens or creates a text file ° . tes a text file named python! 2.csv to write Pens oF creates a fext fle named python12.e to append (©) Opens or creates a text file named ned python! 2.csv to read (@ Opens a new file named python12.esv to append 9, What does the following statement do? x + open(*story.txt", ‘re') (@) Opens a text file named story: to read fiom or write to (B) Opens a text file named story to read (6) Opens a text fle named storysnt to write (@) Opens « new file named story.xt to append 10, What do you understand by the statement given below x = open(*file.txt", “us") (@) Opens a text file named filet 10 read (b) Opens a tent fle named filesxt 0 write to or ead fom (©) Opens a text fle named filesxt to write (@) Opens a new file named files to append 11. Which ofthe following statement is “True’ fr the given code? x = open(*python.txt", ‘a*') (a) Opens a tex ile named python.xt to read (6) Opens a text file named python.nt to read and write (6) Opens a text file named python txt write {a> Opens or creates text fle named pythons 10 ea rom or wit 0 athe end ofthe Sle 12, Which of the following statement is true about the given code x = open('pythont. bat’, ‘x') (a Opens an existing text file named python! at to write (8) Opens an existing binary file named python! batt write (© Opens an existing binary file named python! bat to append (@ Opens an existing binary file named python! bat 0 ae ‘Computer Science with Python 94 x = open(“python: bal of the follow (@) Opens or creates & (6) Opens oc xeates a binary fle {o) Opens or creates @ {ay Opens ane fie named yon. be 1 we 14, Witte purpose of the following statement “ab') x = open( ‘python. bat” ‘inary file named python bat to write file named python-bat to append ython. bat to read (a) Opens or creates & (8) Opens or erates a binary {6 Opens or ereates a binary file named P {Opens a new fle named python bat to append 15, What wil be the output after the following statements? x = open(‘python.txt's ‘r') print (x.nae) (@) python 1 FileNotFoundError (@) python © (©) python.xt oF 116, Interpret the below statements? x = open("python.csw', '') print(x.node) (a) python write 17, Predict the output ofthe following statements? (8) pythonxt @r x = open("hello. csv", print(x. closed) (a) open (b) closed (©) True 18. What willbe the output after the following statements? x = open("hellol.csv', ‘w') x.close() print(x.closed) (@) open (8) closed 19, Tell the output of the following statements? = open(‘python.csv", ‘w") prine(x.readable()) (© Tue (@) readable (6) writable (0) Tne 20, What will be the output after the following statements? x = open("hello2.csv', ‘w) print(x.uritable()) (@) readable idee (6) writable 92 Topechex aceh® Competency Based an Objective Type Questions —12 jing option is correct? Interpret and tell whic inary file named python bat to write named python bat to append binary file named python.bat 0 read (6) python.txt opened @w (@ False (@) False (a False (a) False (@ one 4, Which ofthe following rule isnot corect fer naming fle? (@) Twofiles can have same name in diferent o ‘eo fies con inves {© Two files must have diffrent path names pt {@ Files path name must be different 25, Which ofthe following statement i nt the comet path name? (@ cAPythonthello.tt (©) bellow (0) vhellowt ae a6 Which ofthe following symbol i used to show oot director? om) (8) (double dots) (©) \\@ouble back slash) (@) Nowe of te #7, wiehrie allowing mae ee mb pena a mere catensinm sig {@) binary (©) append (©) write (2) nove ofthe mentioned 28. What will be the default ile mode to open a ile, if itis not specified? (@) read (6) write (6) append @ can't ay 28. Which of the following statement is not used for reading a datafile? (2) real (8) write (6) readies) (@) readline) 238, Which of the following statement isnot used for writing into a datafile? (@ read) (6) writeg) (©) writelines)——(@),_writelined) 31. Which of the following command is used to open a file “emp txt” in read-mode only? (@) infile = opent“e:temp.tt”, ") () infile = opent*e:\temp.txt”, “F) (© infile = openifile = “ectemp.t, 1") G@) infile = opentfile = “e-\temp.et", 7) {When yes open fle fr reading if the file does not exist, an error occurs, SB) When you open ae fer writing. ifthe fle does not ens, an eor occu, fe) Whee you opens ile for reading ifthe file doesnot exis, the program will open ang (oy Wes poe open 2 Be fer writing if the le doesnot exist anew lei creat fe) When gos open a ile for writing. ifthe ie eis, the existing ile is verwrten a ew fie. 3S Which of te following statement is used to read “a” number of characters from a file Sie objet ? fo Sierads) (8) n= flereadX) (6) filereadline(n) (4) fle seadine) FE. Wid of the following statement is used 10 read the entre contents of a file as a string the file object ? (2) tmpblescadia) (6) tmpfileread() _(c) tmpfilereadline() (d) tmpfie IGF. Which of the following statement is used to read the next line in 2 file sing the file objet (0) fieread) (6) file read) (6) filercadline() _(d) filexeadinest) $A. Wich ofthe following commands can be used to read the remaining lines ina file using tel object method returns? (a) (6) list of single characters (@) ist of int o. ad temp.txt” for writing in binary (2) outfle = opent"e:temp txt”, “W") (8) outfile = opent“c2\temp txt”, “wb") (6) ousfile = opent“citemptxt",“w+") _(d) outfile = open(“c:\temp.txt", “wb*") Al. Which ofthe following command is used to open a file “e:\temp txt” for reading in binary (6) filese2d0) (0) fileeadline() (A) file readlines() (6) alist of strings (@) outfile = open(“c:temp 1x”, “¥") (B) outfile = open(“c:\temp.txt”, 1b") ‘outfile = open(“c:\temp.txt"-"r*") (d) outfile = open(“c:\\temp.txt”, “a”) Com sk decir pe te indicates thatthe next rea (6) indicates that the next read or write will occur at that a -many byes from the beginning ofthe (©) move the current file postion to a diferent location (@) it changes the file postion only if allowed to do so else returns an error. 46, Which ofthe following statements coretly explain the function of seek() method? (c) tell the curent positon within the fie (indicate thatthe next read or write occurs from that position ina fle (©) determine if you can move the file position ono (@ move the current file postion to a diferent location ata defined offset. 47. What willbe the output ofthe following code snippet? fo = open(*nyfile.tet*, “u") print ("Nene of the file: *, fo,nane) 4 Assuming that the 1e contains these Lines seqe"Technolegends\nHello Viewers! !* fo,uritelines(seq ) f0.seek for line in fo: print (line) fo.close() {(@) Technolegends (6) nds (©) Techno (2) legends 48, Which of the following statement is not correctly defining pickling method in python? (@) Itis a process to convert a Python object into a byte stream. (b) Itis a process to convert a byte stream to Python object (6) Its done using two methods : dump and load. (@ Serialization is an alternate name for pickling. Computer Science with Python 95) “Assuring file has the following line ext « "This is Ist Line,” fo.uriteLines( x) seq = * This is 2nd line, This is 3rd line” 's lrite sequence of lines at the end of the file. fo,seek(0, 2) fo.writelines(seq ) '¢ Now read corplete file fron beginning. fo.seek(0,0) ine = fo.reaclines() rine ("Read Line: #5" % (1ine)) + Close the file fo.close() (@) Nave of the file: myfile.txt ead Line: ['This is Ast line, This is 2nd line, This is 3rd Line’) (©) Nave of the file: nyfile.txt Read Line: [° This is 2nd line, This is 3rd line’] (©) Read Line: [ “This is ist Line’) (@) Runtime Error S51. Predict the output of the code spnippet veith open("hello.txt", "w") as £: fuwrite("Hello World how are you today?") 3) as f ‘with open( ‘hello. txt", ata = f.readlines() for line in data: words = Line. split() print (words) E.close() 96 Tepechex exch Competency Based and Objective ye Questions —12 fox 4 in range (5): sath open(myfile.exe*, print (f.closed) (@) Runtime Eror (6) True ‘58. What will be the output ofthe ft £ + open(*data.txt", “we") tat = "This is Ist Line\nt furitelines( txt ) (6) False lowing code sine? seq + * This 15 2rd Line\aThis is 3e4 tinge fseet(0, 2) — fiitelines(seq ) mist (] for line in rylist-append(Line) print(ryLise) (@) Runtime Error (b) ("This is 1t linen, * This is 2nd nla (6) [This is 2nd line", “Tis is 3d ine’) oo $4. What willbe the ouput of the following code snippet? £ = open(*data.txt’, *1") C+ "This is ast Line\n" facitelines( ext ) f.seek(0,0) Line + £reeetines() Brint ("Read Line: 45° % (Line)) f.close() (@) (This isto ine] oo (© 10 Bor (d) None isis 3rd line") (@ 10 Em : ding the seek) method? 56 Wich oft folloing statment comet epding te seek) (@) sets the file position to the end of file (©) poston of ie pointer remain unchanged (0) ener occurs (the file pointer is set othe star ofthe ile 57. What willbe the ouput ofthe following code snippet? colors « [red ‘yellows’. “2lue\n'] £ = opencalors.¢', 'v') fvitelines(colos) tciose() seei(0.0) for line in # print (Line) A re yellow sue : (©) Creda, “yellows, bive a] (6, Enor: LO operation on closed fil. (2) Completion eror $8. Which ofthe following statements are corect regarding the file access modes? (© opens fle fer both reading and wen. File obec pont to its begiming- (©) "opens a file for both wring and reading. Overwrtes the exstng file it tex Feats a new on if it doesnot exist : (©) ‘wh’ opens a file for reading and wrii binary format. Overwrites ting in it if ‘and creates a new one, i it does not exist. a “a opens a = ens Sle for appending. The fle ponte sat the end ofthe file, ifthe file ofthe following is nota mode of fie in Python? Om ow — tuonen a file “acs fr reading, we ue isan wile Op te el= speW’cowhstx"."7)—() lead cpetecinadan*F) {ole read = opentil = “cAmarks.t", *) (a) file_read = opentie~ Y\imarks xt", ") 4. wise blog samme ices wrapecee eee ae a {a) file read read2) () file_read(2) ale {6 file readread2) (A) file_readreadlines(2) Ries dice as euhb aperenee (@ fread) () fread) pened : The readines() returns ine) (ad) freadines() (@) only first line from the file (6 al ins from th file «4 Whats the use of tll) method in Python? {a) returns the current positon of record pointer within the file (6) only lst tine from the file (@) none of the above (6) retums the end position of record pointer within the file (6) tetums the current position of record pointer within the line () none of the above (6 Spa to rename a file is (o) rename(currentfile_name, new ile name) () rename(new file name, current fle_name,) (6 rename((current_file_name, new fle. (4) none of these (66, seck() method in files used for (@) sets the file's current position atthe offset sme) | (6) sets the file's previous position atthe offset (6) sets te file's current position within the fle (done of these 67, Which ofthe followin (a) tells you the position within the fle ‘atement is not true about tell) method in Python? (6) tells you the end position within the file (¢) also tells you the file is opened or not (2 none of these (8. What isthe syntax 10 remover) a file in python? (2) removetfile_name) (6) remove(new_fe_name,current file name,) (©) remove((), file_name)) (@ none of these Conte cence wth Pinar 99) ‘69. What isthe pickling? (2) Its used for object serialization (0) Mis used for object deserisization (6) None ofthe mentioned (d) All ofthe mentioned Which ofthe following statement is not tre regarding the opening mode (@) When you open a file fo reading, ifthe fle does not exist, an error occurs (©) When you open a file for writin, ifthe fle does not exist, an error occurs (Wren yep ater ite le di tt ee amg ist, new file is created of a filer (@ When you open a file for writing ifthe file doesnot (6) When you open file for wring if the file exis, the existing es overrite wi gy new fil 71, Whi of he ftlowing commands can be wed oe" number oF Crm fe ig the file object ? (@) Fileready (@) Flleseadline(o) (d) Fileseadinewy 72. Which ofthe following commands canbe wed to read the emire contents ofa file ata ing te le obs He? * (@) Filesead(a) (6) Fileread)_—_(¢)Filesealinel) (4) File eadliney 73. Which ofthe following commands canbe used o read he next ine in file using the fe objgg ? (@) Filesead(o) _(b) Fileseadl) _(¢)Fileseadhinel) (4) File sean) 74. What does te method returns? (@ se (6) Ais of tines (6) List of single characters (4) List of integers 75. Which ofthe following command is used pen ale" temp.” for tin n binary orm ie (2) four= open("ésorytn’, wh") (6) fout = open(*d\temp tt", *wb) (6) fout= openile = "ditemp.xt,"wb*") (4) fout~ open( file ~ a temp tt", “wbe) 76. Which ofthe following functions do you we 1 write deta inthe binary format? (@ Write (©) Output (©) Dump (a) Send 77. Which of the following are the atributes related to a file object? (@) Closed (6) Mode (6) cae (@ Rename 78 What isthe correct syntax of open() function? (a) File = open(file name [, access. modell, buffering}) (6) File object = openfile name [, access. mode, butfering)) (6) File object = open(file_name) (@) None of the above 100 Pagecher with? ompotency Based and Objective Type Questions 12 ia nthe following question no. 79 10 88, a statement of reason (R). Matk the correct choice as i ae i nan sein (A ade (wee and een the correct explanation of assertion cy (9 Bots serion (A) and reason (R) are ve but reason (Ris not he erect explanation of Deion (A) fg aserion (A se ut eso (ie followed by # fo aseron (A) 8 fab ut reason (Rs ‘, Assertion (A): The path tte. an absolute pth ofthe file Reason (R): An absolute path always begins withthe root folder followed by a colon. ‘assertion (A): If the content ofthe text ile: Pract sielto tude The below code vill pr ts! Welcone to your Session for fie handling p.read(10) output: Hello Stud Reason (R): The f Assertion (A): The readlines( returns all lines from the fle P will read 10 characters but wil not print anything Reason (R): It reads the single line from the fle 42, Assertion (A): Pickling is a way to convert a Python object structure (list.dietete) into a character stream Reason (Rt): Pickling is only used for seilizing a Python object structure 48, Assertion (A): The data in text fle sin binary format. Reason (R): The data in the binary file isin binary (ein 0 or 1) format. HA Assertion (A): ‘The siatement Feopen('dairy.txt’, ‘a') hens the file in append node for writing. Reason (R): a stands for append mode 85. Assertion (A): The statement Faleobject.seek(0) snes to the beginning of the file at any tine Reason (R):cemove() is used to remove a file 86. Assertion (A): scck( is used to change the file abject position tothe beginning ofthe file Reason (R): tell) retums an integer giving the file objects current postion inthe file Compete Scene with Python 104 ‘87, Assertion (A): The statement: with open("data.txt", “W") as f sxef.r02d() Tt will read whole character present in data file and store itin C tore 38in vartang © Reason (R): The WITH stement when used with open le Lanta ie is closed when the with block exits, 88. Assertion (A):The readlnes() method retums a list of lines, ‘Reason (R):Every line is not stored in a Hist. Thus eannot be returned, Case-Based MCQs 1. Kriss conftced in Pyion code piven below. Pletseelp him o answer the lowing det TRANSFER 1 — {Hletepe("story.txt" "1" ilezopen"st07)-00."") seriefites-readl) for 4 tn se 1 isipen(: ‘et.amer() elit 1 tslover() 8 det. upper() a wo else: n 2122. Line 33 ‘filet. close() Line a ‘ie. close() Line 35 (@) Which line in the above cade check for capital letter” (9 line 3 (id Tine 4 i) ioe S 9) line 6 (®) Which line in the above code read the file “stort (9 line 1 (9 line 2 (ii) Tine 3 ») fine 4 (©) Which line inthe above code does not affect the execution of program? ( line 10 (49 Tine 11 (ii) ine 12 (ie) line 13, (@ Which line is the above code converts capital letter to small letter? (tines i) Tine 7 (ii) line 8 (i) line 9 (©) Which line isthe above code opens the fle in write mode? (9 tine (i Tine 2 (i) ine 3 (i) bine 4 (D, Which line is the above code saves the data? © tine 1. (ine 12 (4) tine 13, (i) ine 14 102 agecher acek® Competency Based and Onective Tipe Questions —12 Ths $5 oy 600, sere 15 wish yous? Hy 006 4s lying on the table son 9 we concean? det count Poent) fslet + opent striefilel.readhines() 1) © Line 2 count0 for x1 in str # Line 2 # Line 3 # Line & Choose the correct options forthe following: (a) # Line | To open file POEM txt in read mode (9 Gilet ~ open"POEMTXT™'F) (i) Sel = openC TOSSES TES p let = open("POEMTXT.*w") 1) Nemuareeaas 2 To check first character of every line is “W" or“ (®) # Line 2 (9 nifoy—~ 0 H1(0}— 8" i) PMO} oF 0}: (wy 10" (o) AO 4 (©) # Line 3: To increase the value of count by I: (i count ( couss-t (4) count-count} (iv) None ofthe above x (@) # Line 4 To call the function count. Por s ( count_Poer) (ti) countPocen) (w) count) 3. Mohan has writen the following code filet = open("POEH.TXT","r") stri-filel.read(20) print(str) filet. close() (@) What will be the output? © Iwill display fest 20 characters. (i) twill display 20th character ang (Gi) te will display first 20 bytes. () It wil display content st 20h yy, (8) Meenu, Mohan’ friend has written the following Python code, = None for i in range (5) Ath open("LAPS.W.txt", “w") as it i> 2 break print(f.closed) ‘What willbe the output? ( True (i) False (it) None () Error (©) The read() method retum (se (i ist i) tuple (i) None ofthese (@) The readlne() method returns © swing (ip ist it) tuple (©) The readlines() method retums (i) None ofthese © string (i ist (i) tuple () None ofthese 4. Mr. Iqbal isa programmer, who has recently joined the company and python code to perform the following binary file operations withthe h functions/modules: sven a task to write elp of to user defn (a) Addl) to create a binary file called School dat containing student information rame,age and marks of English, Maths and Scienee(out of 100) of each student (®) Display( to display the information of all students, oll number, He has succeeded in writing partial code and as missed out certain statements, so he has leftceain ‘queries in comment lines. You as an expert of Python have to provide the missing statement Anport # statement 1 to import nodule/package ‘ef 244); sel Eile =open("school.dat’,'__*) # Statenent 2 to append the record in binaty file. che'¥" hide chee’ of ches'y 104 agcedex ece® Competency Based and Objective Type Questions —12 Re reressriline “nt ner nae mathmarkse int (ing is receipt ner ath is“) stl nane'enane sage] eage sstL‘english® ‘st ['maths' ]omathnarks st {science oschemarks pickle. cinp(st 5 chug youth cece tel file.close() Got display) sel) {ile-opent school dat’, "b") ty: unite True stepickle, (tite) f statement & to store data in st. print(st) except F0FErrOr print("error") tile. Cchoose the eorrect option for the following: (a) Write the statement to import module in statement 1 1» statement 5 to close file object. 9 osv i ev in Cov (in) None of these . (0) Write the statement append the recordin binary fle Oa ww iy a (i) None ofthese (©) Wie the statement ospify object in statement 3 © fie i object (uy pointer (i) None ofthese (Wt the statement tre dati (0 wite i) toad (i), open (iv) None ofthese (6) Statement close the ile objet i) uit () close Compute Science with Python 105 (9 open (i exit Mathew isa programmer, who has recently joined the company and given © ene pt a ort ney Son whch ea 'No) and fe(Fee paid by student). “He has succeeded in writing partial code and has missed out certain ‘of Python have to provide the missing statements. # Update a record. srport © statenent 1 se) flageo ‘ilecopen bs") # statenent 2 Seorchvint(input("Enter Admission Nurber whose fee 1s to be updateg ” ant=int(input( "Hou much to be increased : °)) ty: while True: Try: posefile statenent 3 stepickde Toad( fale) Af stl'acmo'] == search stl'fee" Jeane Fes) Pickle. (st, tite) Print("Recora Updated") flagen file.close( (@) # statement 1 1 import module package (0 eww (i) math (®) # statement 210 open stud binaryfie in read (stud dat (©) # statement 3 to store file pointer position (9 flere (@) # statement 410 pla filexetly i) fileseeky (©) # statement 5 to store the data (ii) piekte mode (it) stud.bin (0) None of these (stadt (0) None of these ( fileseek) (ui) Se setkpos) (0%) None ofthese file pointer at specified postion ii) Bletell(pos) (None of these ore © mp i), sare ©) im OT St towing comnts wet wie it LST a file object)? “a © pike weieasteie) (2 pike wie, 187) (Wp umpctstiLE) (0) FILE=pickle dump(LIST) 106 apether axed” omptecy sedan Oetve Tipe estos —12 fest in CBSE School. Teacher has yeas sven a task to write a python code to perform 6 Pratowine ‘inary file operations with the help of two user defined functions/modules: esl 1 a8 binary file called school dat containing student information (in lit ah ype)~ tent number, ame marks out of 10) ofeach student 1 sa diy same snd mars of he student by asking the student number form the use ‘write the code inthe missing statement (Fill in the blank) {CBSE Question Bank] ounce io ser rice sesreco on a (ncn ser neta a on one Pee u ‘ckle.loadl) «ie ' statenent 5 anets(aDe if) te faunas nce nen ‘Choose the correct options forthe following: (@) # statement 1 to declare the data-type. © empry (i) create “0 ©) None (0) # statement 2 to store all records in stu tS © load (damp (i) store ) write (©) # Statement 3 function to insert data at end © insert G.append i aa ©) None of @) # statement 4 to save the data = Or Ww ios () wor (©) # Statement 5 1o search for student number. © sto (i seame (i) marks (0) None ofthese 7. Kishna of lass 12s writing a program read the deilsof Sports perfrmance, and stor in <5y fle “gamesesv" delimited with atab character. Asa programmer, help hime achieve that CBSE Question nay inport 8 Line 1 £ = open("ganess.csv",‘a") Ree F Getiniter = “\c-) + Line 2 sobj.vriterou( ['Ganes', ‘Competitions’, “Prizes’] ) ans = 'y lntle ans == “y' print(“Record :*, (3) ‘input (“Gane Name :*) Ane (snput("No. of participitants :)) ‘nt input ("Prizes:")) _# Line 3 (ree) # Line 4 ans = input(Oo u want to continue ? (y/n) :*) f # Line 5 ‘Choose the correct option forthe following (@) Line 1 : Name the module he should import @ csv (iy sv (a csv () None of these (8) Line 2: To create an object to enable to write in the osv file open (i) read (i), waite (0) writer (©) Lime 3 : To store the data in ist (9, ree={sport,comp prize) (i) t2e=(sportcomp.prize) (iit) ree={sportcomp prize} (®) None of these 108 apcchen aek® Competency Based and Objective Type Questions —12 (4: To write a record, ine (own (i) writer iatea pues ‘To close the file. i) writerow (iv) None of these () open (i exit : oe () close on ene eee ae Te renanermmenipemeortece a (cote }. Help him in completing the code omy which erates the desired CSV File. Fale {€RSE Question Bak sant XT ppaseK XIE payt0.X1T av XID A £ itsAE A ) sStatenent-2 fata. appena(nende fox 4 sn 0g2(°) retino > intl Chass « in aoe ata. append Rewriter. —— ‘.ciosel) iy ube cove or lnk Pe I marked as Statement (a ov ) ow (9 owvfle (i csv in Kine marked as Statement-2? (0) Went the missing code for blank P92 (9 "School sv". (iy *Studentcsv"."" (6 Choose the function name (with a8 marked as Statement-3. @ reader) (a reader samen) that should be used aaiyFiley u) wtert) eet sen ne mated a Suomen ple ila was, cass, secnow ROLL. NO, NAME, CLASS, SECTION PE Ter ame ie cie cept scien) corona) Pd ar cai bo nd nt tnt sore meee ment-5 to create the desired CSV File? Sta. (@ dump()_ (ii) load() (ii) a ato ay topes wit fx noir we toe ncaa (2) Aste oct hina le caled STUDENTDAT conaing wea nae (®) GetStudents0 to display the name and percentage of those students who have a per ‘greater than 75. In case there is no student having percentage > 75 the function displays ge 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 let certain queries in comment lines. You as an expert of Python have to provide the missing statemen and other related queries based on the following code of Amvitya, Answer any four questions (out of five) from the below mentioned questions, [CBSE Question Bask writerows) (iv) writerow) import pickle def AddStudents(): ‘1 statenent to open the binary file to write data while True: Roo = int(anput("Rne Name = input(*Nane : *) Percent « float (input("Percent L = [Rno, Nane, Percent) #2 statenent to write the list L into the file ») Choice = input(*enter more (y/n): *) Sf Choice in "mv break F.close() def GetStudents(): Total-0 Countre Countabove75-0 with open("STUOENT.DAT","rb") as F: while True! 10 agether exeh® Competency Based and Objective Type Questions—12 TotaleA(2} Af Wa} > 755 print a}, * hae percent © *A(2)) Countaboneroen ¢ excepts break Af Countabove7 print "There 18 0 student. wa tis percentage ayeragesTotal/Countec ™ a average percent of class « print * awexage) paaseudent() fester) Woh oti oman is sed pen he fle “STUDENTDAT” fo rin ob pnay format (marked as #1 in the Python coe) (pF open STUDENT. ORT", 0") i) open "STENT", 's') By cco ste re) aaa aaa | ich ofthe following commands is sed 0 write the i it te Bay file, STUDENT. DAT? (marked as #2 the Python code) ( pichte.urite( tt) ollsF (i) picktewunstelt, i) picete- in) tepid dng) © te ling comands ia red ch martina le STUDENT DAT? (marked as #3 inthe Python coe) (0 R « pictle.Looa(F) (uy x= pickae.reaalt) (@ Which ofthe following statement (p “1+ opens ile for bah eading and writing {iy “opens ile for bh writing and reading Trias tnd creates anew one fit oes not exis i) “opens a ile for ending and writing in ime and creates «new one it does nok exis loys eyes hr gpd, Th epi ST ifthe fle exists. os cine towing saree conse xn Res sebod? ition within the file postion o 0 curs fom that postion in a fle ven specified poston Computer Science with Pthon 144° | ‘ . i) pickte-n0adte,£) (iv) psekie, toads £) (sare correct regarding the file access modes? File object points to its beginning. ‘Adds at the end of the existing file if it (0 tells the eurent ps (i) determines if you can move the file (i indicates thatthe next read or wie (@) moves the current fike position 10 8 10 Radha Shah isa programmer, who has recently been given a task tt & python perform the fllowing CSV file operations with the help of two use defined function, (@ C8VOpen to create CSV file called BOOKS.CSV in append mode contain tion of books ~ Title, Author and Price. "fora, (©) @SVReads to display te records fom the CSV Hie callsd BOOKS. CSV whee Seg title starts with She has succeded in writing pail code and has mised ay ‘atements, so she has let certain queries in comment lines, in snport csv et C5¥0pen(): with open 'books.csv',"__*,newlines"*) as csv ‘Staten a *Statene mt *Staterenes ‘cusuriterov({ ‘Rapunzel, “Jack,300)) ‘ariterou({ Barbie’, 0011", 900]) c.siriterou{{ ‘Johnny’, "Zane", 260]) de. cSuReaa) oy: With open("books.csv', 2") as csv & *Staterant4 cee Statenents rine (2) except: print (‘File Net Found’) Svpen() SvRead() ‘You as an expert of Python have to provide the missing staemiens and other related queries ‘based on the following code of Radha. Answer any four questions (out of five) feom the below ‘mentioned questions. (@) Choose the appropriate mode in which the file sto be opened in append mode (Statement I) wt (i) ab (iy r+ (oa (®) Which statement will be used to create a esv writer object in Statement 2? ( cowwriteresvi) (i) esvwiteresvt) (ii) esviwriter?) (i) eswriteresvt) (© Choose the correct option for Statement 3 to write the names of the column headings in the CSV file, BOOKS.CSV. (0 ewowriterow(Title'’Author Price’) (i) ewcwrterow({ Title'Author'’Price’)) ""AuthorPrice’) thor Price’) Gil) ew.writerows( Tt (iv) ew.writerows((Tile’ 112 Zagecher wceh® Competency Based and Objective Type Questions —12 zz wich statement wil Be used 10 read a xy file in Statement 42 (p coredlese)_ i) esvreadeest) (i) eavfrea) (i) esvtteaders) rain the appropriate statement to check the field Title starting with ‘Ror Statement § the above program. i a (o stort Gi) [YOR Gi) FOYER (>) LIEN Your teaches has given you a method/anon FiteWas0 npn which ad te fom wt le NewsLetter TXT, and display those words, which are lesser tan 4 characters. Your ‘cers intentionally Kept few blanks in between the ede and asked you to fil the lnk 50 Morte code will unt ind desired result, Do the needful wth he following python code o det Faltersords |) ay hle-open(‘Nevsletter. Txt", '__') sseatenent-1 ine = file. sstaterent-2 word = sseatenent-3 for ¢ in word: if + sstatenent-t print ((c sstatonent-5 Faltenores() (@ Write mode of opening the file in statement-1? a (iy ab ww wr ta from the fle (&) Fillin the blank in statement? to ead the (0 File Read) (ip Slesead) Gi) read tines.) (jv) readies) (6) Fill inthe blank in statement-3 to fead data word by wor (i) Line split) (iv) split word) splay the word having lesser han 4 characters, (H Line Split (i) ine spit) a Fill in the blank in statement-4, which ds (9 leo i lente (ii ten = 3 (©) Fill in the blank in Sater (9 AlecloseO, (4a Close 3 (i) ten ent to close the le i FileCloseQ) (iv) end ampate Science with Python 143 CHAPTER 3: Data File Handling Multiple Choice Questions 1. (©) 2. (d) 3. (@) 4. (6) S 6. (c) 7. (a) 8. (6) 9. (a) 10. . (@) 12. (d) 13. (@) 14. (6) 15, 16. (d) 17. (a) 18. (0) 19. (d) 20. 21. (0) 22. (b) 23. (c) 24, (b) 25, 26. (c) 27. (b) 28. (a) 29. (b) 30. 31. (6) 32. (b) 33. (a) 34, (a), (d)and(e) 35. 36. (b) 37. () 38. (d) 39. (b) 40. 41. (6) 42 (0) 43. (b)and(c) 44. (a), (b) and (c) 45. 116 agecher awith® Competency Based and Objective Type Questions—12 () (a) () (a) and @) ae j 20 ow *O@ 1@ @ 3 @ wi) 4@ 3 @ i @ 6 (@) (iit) 1@ Wi 8. (@) (ii) 9.) @ 10. (a) () 1. (a) (i) 85. (5) (6) (iv) A) (ii) (6) @) 0) @ (6) (iii) 6) @ (A (ii) 6) @ (6) () (b) (ii) () (iii) (6) Gi) (6) Gi) (©) (ii) © @ © @ © @ ©) Ww (©) Gi) (c) iii) © @ © @ (c) (iii) Wi @®@ @@ @ ww (@) (iii) @ (ii) (@) (iii) (4) (@) @@ @ @ wi © @ © @) © @ © @ (©) Gi) © @ © @ © @

You might also like