Python Interview Questions | Anonymous Function | Python ...

January 10, 2017 | Author: Anonymous | Category: Python
Share Embed


Short Description

Top 25 Python Questions. : 1) What is Python? What are the benefits of using Python? Python is a programming language wi...

Description

 Top  Top 25 Python Questions: Questions: 1) What is Python? What are the benefts o using Python? Python is a programming language with objects, modules, threads, exceptions exceptions and automatic memory management The bene!ts o" pythons are that it is simple and easy, portable, extensible, extensible, build#in data structure and it is an open source 2) What is PEP 8? P$P % is a coding con&ention, a set o" recommendation, about how to write your Python code more readable ') What is pickling and unpickling? Pic(le module accepts any Python object and con&erts it into a string representation representation and dumps it into a !le by using dump "unction, this process is called pic(ling hile the process o"  retrie&ing original Python objects "rom the stored string representation is called unpic(ling *) How Python is interpreted? Python language is an interpreted interpreted language Python program runs directly "rom the source code +t con&erts the source code that is written by the programmer into an intermediate language, which is again translated into machine language that has to be executed 5) How memory is managed in Python? Python memory is managed by Python pri&ate heap space ll Python objects and data structures structures are located in a pri&ate heap The programmer does not ha&e an access to this pri&ate heap and interpreter interpreter ta(es care o" this Python pri&ate heap  The allocation o" Python Python heap space "or Python objects objects is done by by Python memory manager The core P+ gi&es access to some tools "or the programmer to code Python also ha&e an inbuilt garbage collector, which recycle all the unused memory and "rees the memory and ma(es it a&ailable to the heap space -) What are the tools that help to fnd bugs or perorm static analysis? Py.hec(er Py.hec(er is a static analysis tool that detects the bugs in Python source code and warns about the style and complexity o" the bug Pylint is another tool that &eri!es whether the module meets the coding standard /) What are Python decorators?  Python decorator is a speci!c change that we ma(e in Python syntax to alter "unctions easily %) What is the dierence between list and tuple?  The di0erence di0erence between between list and tuple tuple is that list list is mutable while tuple is not Tuple Tuple can be hashed "or eg as a (ey "or dictionaries ) How are arguments passed by value or by reerence? $&erything in Python is an object and all &ariables hold re"erences re"erences to the objects The re"erences re"erences &alues are according according to the "unctions as a result you cannot change the &alue o" the re"erences re"erences 3owe&er, you can change the objects i" it is mutable 14) What is ict and !ist comprehensions are?  They are syntax syntax constructions constructions to ease the creation o" a ictionary or 6ist based on existing existing

iterable 11) What are the built"in type does python provides?  There are are mutable and +mmutable types o" Pythons built in types 7utable built#in types 6ist 8ets ictionaries +mmutable built#in types 8trings  Tuples  Tuples 9umbers 12) What is namespace in Python? +n Python, e&ery name introduced has a place where it li&es and can be hoo(ed "or This is (nown as namespace +t is li(e a box where a &ariable name is mapped to the object placed hene&er the &ariable is searched out, this box will be searched, to get corresponding object 1') What is lambda in Python? +t is a single expression expression anonymous "unction o"ten used as inline "unction 1*) Why lambda orms in python does not have statements?  lambda "orm in python does not ha&e statements as it is used to ma(e new "unction object and then return them at runtime 15) What is pass in Python? Pass means, no#operation Python statement, or in other words it is a place holder in compound statement, where there there should be a blan( le"t and nothing has to be written there 1-) #n Python what are iterators? +n Python, iterators are used to iterate a group o" elements, containers li(e list 1/) What is unittest in Python?  unit testing "ramewor( in Python is (nown as unittest +t supports sharing o" setups, automation testing, shutdown code "or tests, aggregation o" tests into collections etc 1%) #n Python what is slicing?  mechanism to select a range o" items "rom seuence types li(e li(e list, tuple, strings etc is (nown as slicing 1) What are generators in Python?  The way o" implementing implementing iterators iterators are (nown as generators generators +t is a normal "unction "unction except except that it yields expression in the "unction 24) What is docstring in Python?  Python documentation string is (nown as docstring, it is a way o" documenting Python "unctions, modules and classes 21) How can you copy an ob$ect in Python?  To  To copy an object in Python, you can can try copycopy copycopy ;) or copydeepcopy;) copydeepcopy;) "or the general case

 9 ? a) hello12' b) hello c) $rror d) hellonswer:c $xplanation:.annot concantenate str and int objects / What is the output when ollowing code is e%ecuted ? print;RR, end K J J) print;R.R, end K J J) print;RER, end K J J) print;RR, end K J J) a) .E b) , E, .,  c)  . E  d) , E, .,  will be displayed on "our lines nswer:c $xplanation:$xecute $xplanation:$xecute in the shell % What is the output when ollowing statement is e%ecuted ?3python +%%4 print;"ormat;Re print;"ormat;RelcomeR, lcomeR, R14sR), end K JGJ) print;"ormat;111, print;"ormat;111, R*dR), end K JGJ) print;"ormat;2*-5-, print;"ormat;2*-5-, R'2"R)) a) elcomeG 111G2*-111G2* -b) elcomeG111G2*-c) elcomeG111G-d) elcome G 111G2*-111G2*- nswer:d $xplanation:$xecute in the shell to &eri"y  What will be displayed by print3ord3@bA4 B ord3@aA44 ? a) 4 b) 1 c) #1 d) 2 nswer:b $xplanation:ascii &alue o" b is one more than a 14 *ay s;0hello0 what will be the return value o type3s4 ? a) int b) bool c) str

d) 8tring nswer:c $xplanation:str is used to represent represent strings in python ,his set o Python )uestions - (nswers ocuses on .*trings0+ 1 hat is N3ello]replace;Nl], Ne]) a) 3eeeo b) 3eelo c) 3eleo d) 9one Fiew nswer

nswer:a $xplanation:$xecute in shell to &eri"y 2 ,o retrieve the character at inde%  rom string s;0Hello0 what command do we e%ecute 3multiple answers allowed4 ? a) s' b) sgetitem;') c) sIIgetitemII;') d) sget+tem;') nswer:a, c $xplanation:IIgetitem;) can be used to get character at index speci!ed as parameter ' ,o return the length o string s what command do we e%ecute 3multiple answers allowed4 ? a) sIIlenII;) b) len;s) c) si?e;s) d) ssi?e;) nswer:a,b $xplanation:$xecute in shell to &eri"y *+ # a class defnes the 77str773sel4 method2 or an ob$ect ob$ or the class2 you can use which command to invoke the 77str77 method+3multiple answers allowed4 a) objIIstrII;) b) str;obj) c) print obj d) IIstrII;obj) nswer:a,b,c $xplanation:$xecute in shell to &eri"y 5 ,o check whether string s= contains s>2 use a) s1IIcontainsII;s2) b) s1 in s2 c) s1contains;s2) s1contains;s2) d) siin;s2) nswer:a,b $xplanation:s1 in s2 wor(s in the same way as calling the special "unction IIcontainsII  - *uppose i is C and $ is D2 i 9 $ is same as

a) iIIadd;j) b) iIIaddII;j) c) iIIdd;j) d) iII;j) nswer:b $xplanation:$xecute in shell to &eri"y / What is the output o the ollowing code ? class .ount: de" IIinitII;sel", count K 4): sel"IIcount K count c1 K .ount;2) c2 K .ount;2) print;id;c1) KK id;c2), end K R R) s1 K R@oodR s2 K R@oodR print;id;s1) KK id;s2)) a) True =alse b) True True c) =alse True d) =alse =alse nswer:c $xplanation:$xecute $xplanation:$xecute in the shell objects cannot ha&e same id, howe&er in the case o" strings its di0erent % What is the output o the ollowing code ? class 9ame: de" IIinitII;sel", !rst9ame, mi, last9ame): sel"!rst9ame K !rst9ame sel"mi K mi sel"last9ame K last9ame !rst9ame K RohnR name K 9ame;!rst9ame, J=J, R8mithR) !rst9ame K RPeterR namelast9ame K RPanR print;name!rst9ame, print;name!rst9ame, namelast9ame) a) Peter Pan b) ohn Pan c) Peter 8mith d) ohn 8mith nswer:b $xplanation:$xecute in the shell to &eri"y  What unction do you use to read a string? a) input;N$nter a string]) b) e&al;input;N$nter a string])) c) enter;N$nter a string]) d) e&al;enter;N$nter a string])) nswer:a $xplanation:$xecute in shell to &eri"y

14 *uppose % is DC+CD2 what is ormat3%2 .=+04 37 indicates space4 a) II'*5'55 b) III'*5'55 c) IIII'*5'55 d) IIIII'*5'5* nswer:b $xplanation:$xecute in the shell to &eri"y ,his set o Python )uestions - (nswers ocuses on .!ists0+ 1 Which o the ollowing commands will create a list3multiple answers allowed4 ? a) list1 K list;) b) list1 K  c) list1 K list;1, 2, ') d) list1 K 1, 2, '

nswer:a,b,c,d $xplanation:$xecute $xplanation:$xecute in the shell to &eri"y 2 What is the output when we e%ecute list3.hello04? a) JhJ, JeJ, JlJ, JlJ, JoJ b) JhelloJ c) JlloJ d) JollehJ nswer:a $xplanation:execute in the shell to &eri"y ' *uppose listE%ample is FGhG2GeG2GlG2GlG2GoG2 what is len3listE%ample4? a) 5 b) * c) 9one d) $rror nswer:a $xplanation:$xecute in the shell and &eri"y * *uppose list= is F>DDC2=2=>DCD2=> F>DDC2=2=>DCD2=>2 2 what is ma%3list=4 ? a) 2**5 b) 1'' c) 12*5* d) 12' nswer:c $xplanation:max returns returns the maximum element in the list 5 *uppose list= is F2 C2 >C2 =2 2 what is min3list=4 ? a) ' b) 5 c) 25 d) 1 nswer:d $xplanation:min returns returns the minimum element in the list - *uppose list= is F=2 C2 I2 what is sum3list=4 ?

a) 1 b)  c) 15 d) $rror nswer:c $xplanation:8um returns returns the sum o" all elements in the list / ,o shuJe the list3say list=4 what unction do we use ? a) list1shue;) b) shue;list1) c) randomshue;list1) randomshue;list1) d) randomshue6ist;list1) randomshue6ist;list1) nswer:c $xplanation:$xecute $xplanation:$xecute in the shell to &eri"y  % *uppose list= is FD2 >2 >2 D2 C2 >2 =2 2 Which o the ollowing is correct 3multiple answers allowed4 ? a) print;list14) b) print;list1:2) print;list1:2) c) print;list1:#2) print;list1:#2) d) print;list1*:-) nswer:a, b, c, d $xplanation:8licing $xplanation:8licing is allowed in lists just as in the case o" strings  *uppose list= is F>2 2 >>>2 =D2 >C2 What is list=F"= ? a) $rror b) 9one c) 25 d) 2 nswer:c $xplanation:#1 corresponds corresponds to the last index in the list 14 *uppose list= is F>2 2 >>>2 =D2 >C2 What is list=F6"= ? a) 2, '', 222, 1* b) $rror c) 25 d) 25, 1*, 222, '', 2 nswer:a $xplanation:$xecute in the shell to &eri"y ,his set o Python )uestions - (nswers ocuses on .!ists0+ 1 What is the output when ollowing code is e%ecuted ? names K JmirJ, JEearJ, J.harltonJ, JamanJ print names#1#1 a)  b) aman c) $rror d) n

nswer:d

$xplanation:$xecute in the shell to &eri"y 2 What is the output when ollowing code is e%ecuted ? names1 K JmirJ, JEearJ, J.harltonJ, JamanJ names2 K names1 names' K names1: names24 K JliceJ names'1 K JEobJ sum K 4 "or ls in ;names1, names2, names'): i" ls4 KK JliceJ: sum DK 1 i" ls1 KK JEobJ: sum DK 14 print sum a) 11 b) 12 c) 21 d) 22 Fiew nswer nswer:b $xplanation:hen assigning names1 to names2, we create a second re"erence to the same list .hanges to names2 a0ect names1 hen assigning the slice o" all elements in names1 to names', we are creating a "ull copy o" names1 which can be modi!ed independently '+ *uppose list= is F=2 2 >2 What is list= K > ? a) 2, -, * b) 1, ', 2, 1, ' c) 1, ', 2, 1, ', 2 ) 1, ', 2, ', 2, 1 nswer:c $xplanation:$xecute in the shell and &eri"y * *uppose list= ; F+C K % or % in range32 D42 list= is 6 a) 4, 1, 2, ' b) 4, 1, 2, ', * c) 44, 45, 14, 15 d) 44, 45, 14, 15, 24 nswer:c $xplanation:$xecute in the shell to &eri"y 5 What is the output when ollowing code is e%ecuted ? list1 K 11, 2, 2' list2 K 11, 2, 2 list1  list2 is a) True b) =alse c) $rror d) 9one

nswer:b $xplanation:$lements $xplanation:$lements are compared one by one - ,o add a new element to a list we use which command ? a) list1add;5) b) list1append;5) c) list1add6ast;5) d) list1add$nd;5) nswer:b $xplanation:e $xplanation:e use the "unction append to add an element to the list / ,o insert C to the third position in list=2 we use which command ? a) list1insert;', 5) b) list1insert;2, 5) c) list1add;', 5) d) list1append;', 5) nswer:a $xplanation:$xecute in the shell to &eri"y % ,o remove string .hello0 rom list=2 we use which command ? a) list1remo&e;Nhello]) b) list1remo&e;hello) list1remo&e;hello) c) list1remo&ell;Nhello]) d) list1remo&eLne;Nhello]) nswer:a $xplanation:$xecute in the shell to &eri"y  *uppose list= is F2 D2 C2 >2 C2 what is list=+inde%3C4 ? a) 4 b) 1 c) * d) 2 nswer:d $xplanation:$xecute $xplanation:$xecute help;listindex) to get details 14 *uppose list= is F2 D2 C2 >2 C2 >C2 =2 2 what is list=+count3C4 ? a) 4 b) * c) 1 d) 2 nswer:d $xplanation:$xecute in the shell to &eri"y ,his set o Python )uestions - (nswers ocuses on .!ists0+ 1 *uppose list= is F2 D2 C2 >2 C2 >C2 =2 2 what is list= ater list=+reverse34 ? a) ', *, 5, 24, 5, 25, 1, ' b) 1, ', ', *, 5, 5, 24, 25 c) 25, 24, 5, 5, *, ', ', 1 d) ', 1, 25, 5, 24, 5, *, '

nswer:d $xplanation:$xecute in the shell to &eri"y

2 *uppose listE%ample is F2 D2 C2 >2 C2 >C2 =2 2 what is list= ater listE%ample+e%tend3FD2 C4 ? a) ', *, 5, 24, 5, 25, 1, ', '*, 5 b) 1, ', ', *, 5, 5, 24, 25, '*, 5 c) 25, 24, 5, 5, *, ', ', 1, '*, 5 d) 1, ', *, 5, 24, 5, 25, ', '*, 5 nswer:a $xplanation:$xecute in the shell to &eri"y ' *uppose listE%ample is F2 D2 C2 >2 C2 >C2 =2 2 what is list= ater listE%ample+pop3=4 ? a) ', *, 5, 24, 5, 25, 1, ' b) 1, ', ', *, 5, 5, 24, 25 c) ', 5, 24, 5, 25, 1, ' d) 1, ', *, 5, 24, 5, 25 nswer:c $xplanation:pop;) remo&es the element at the position speci!ed in the parameter parameter * *uppose listE%ample is F2 D2 C2 >2 C2 >C2 =2 2 what is list= ater listE%ample+pop34? a) ', *, 5, 24, 5, 25, 1 b) 1, ', ', *, 5, 5, 24, 25 c) ', 5, 24, 5, 25, 1, ' d) 1, ', *, 5, 24, 5, 25 nswer:a $xplanation:pop;) by de"ault will remo&e the last element 5 What is the output when ollowing code is e%ecuted ? Relcome Relcome to PythonRsplit;) a) RelcomeR, RtoR, RPythonR b) ;Nelcome], Nto], NPython]) c) VNelcome], Nto], NPython]X d) Nelcome], Nto], NPython] nswer:a $xplanation:split;) $xplanation:split;) "unction returns the elements in a list - What is the output when ollowing code is e%ecuted ? list;RaGbGcGdRsplit;JGJ)) a) JaJ, JbJ, JcJ, JdJ b) Ja b c dJ c) JaGbGcGdJ d) JabcdJ nswer:a $xplanation:$xecute in the shell to &eri"y / What is the output when ollowing code is e%ecuted ? my6ist K 1, 5, 5, 5, 5, 1 max K my6ist4 indexL"7ax K 4 "or i in range;1, len;my6ist)):

i" my6isti  max: max K my6isti indexL"7ax K i print;indexL"7ax) a) 1 b) 2 c) ' d) * nswer:a $xplanation:=irst $xplanation:=irst time the highest number n umber is encountered is at index 1 %+ What is the output when ollowing code is e%ecuted ? my6ist K 1, 2, ', *, 5, - "or i in range;1, -): my6isti # 1 K my6isti "or i in range;4, -): print;my6isti, end K R R) a) 2 ' * 5 - 1 b) - 1 2 ' * 5 c) 2 ' * 5 - d) 1 1 2 ' * 5 nswer:c $xplanation:$xecute in the shell to &eri"y  What is the output when ollowing code is e%ecuted ? list1 K 1, ' list2 K list1 list14 K * print;list2) a) 1, ' b) *, ' c) 1, * d) 1, ', * nswer:b $xplanation:6ists should be copied by executing executing : operation 14 What is the output when ollowing code is e%ecuted ? de" ";&alues): &alues4 K ** & K 1, 2, ' ";&) print;&) a) 1, ** b) 1, 2, ', ** c) **, 2, ' d) 1, 2, ' Fiew nswer nswer:c

$xplanation:$xecute in the shell to &eri"y ,his set o Python )uestions - (nswers ocuses on .!ists0+ 1 What will be the output? de" ";i, &alues K ):   &aluesappend;i) return &alues

";1) ";2) & K ";') print;&) a) 1 2 ' b) 1 1, 2 1, 2, ' c) 1, 2, ' d) 1 2 ' nswer:c $xplanation:execute $xplanation:execute in the shell to &eri"y 2 What will be the output? names1 K JmirJ, JEalaJ, J.halesJ i" JamirJ in names1: print 1 else: print 2 a) 9one b) 1 c) 2 d) $rror Fiew nswer nswer:c $xplanation:execute in the shell to &eri"y ' What will be the output? names1 K JmirJ, JEalaJ, J.harlieJ names2 K namelower;) "or name in names1 print names224 a) 9one b) a c) b d) c nswer:d $xplanation:6ist .omprehension .omprehension are a shorthand "or creating new lists * What will be the output? numbers K 1, 2, ', * numbersappend;5,-,/,%) print len;numbers) a) * b) 5 c) %

d) 12 nswer:b $xplanation:a list is passed in append so the length is 5 5+ ,o ,o which o the ollowing the .in0 operator can be used t o check i an item is in it? a) 6ists b) ictionary c) 8et d) ll o" The bo&e nswer:d $xplanation:in can be used in all data structures structures - What will be the output? list1 K 1, 2, ', * list2 K 5, -, /, % print len;list1 D list2) a) 2 b) * c) 5 d) % Fiew nswer nswer:d $xplanation:D appends all the elements indi&idually into a new list / What will be the output? de" add+tem;listParam): add+tem;listParam): listParam DK 1 mylist K 1, 2, ', * add+tem;mylist) print len;mylist) a) 1 b) * c) 5 d) % Fiew nswer nswer:c $xplanation:D will append the element to the list % What will be the output? de" incrementIitems;6, increment): iK4 while i  len;6): 6i K 6i D increment increment iKiD1 &alues K 1, 2, ' print;incrementIitems;&alu print;incrementIitems;&alues, es, 2)) print;&alues) a) 9one

', *, 5 b) 9one 1, 2, ' c) ', *, 5 1, 2, ' d) ', *, 5 9one nswer:a $xplanation:$xecute in the shell to &eri"y  What will be the output? de" example;6): JJJ ;list) # list JJJ iK4 result K  while i  len;6):   resultappend;6i) iKiD' return result a) Aeturn a list containing e&ery third item "rom 6 starting at index 4 b) Aeturn an empty list c) Aeturn a list containing e&ery third index "rom 6 starting at index 4 d) Aeturn a list containing the items "rom 6 starting "rom index 4, omitting e&ery third item nswer:a $xplanation:Aun $xplanation:Aun the code to get a better understanding with many arguements 14 What will be the output? &eggies K JcarrotJ, JbroccoliJ, JpotatoJ, JasparagusJ &eggiesinsert;&eggiesindex;JbroccoliJ), JceleryJ) print;&eggies) a) JcarrotJ, JceleryJ, JbroccoliJ, JpotatoJ, JasparagusJ b) JcarrotJ, JceleryJ, JpotatoJ, JasparagusJ c) JcarrotJ, JbroccoliJ, JceleryJ, JpotatoJ, JasparagusJ d) JceleryJ, JcarrotJ, JbroccoliJ, JpotatoJ, JasparagusJ nswer:a $xplanation:$xecute in the shell to &eri"y

.orrect

,his set o Python )uestions - (nswers ocuses on .!ists0+ 1+ What will be the output? m K x, x D 1, x D 2 "or x in range;4, ') a) 1, 2, ', *, 5, -, /, %,  b) 4, 1, 2, 1, 2, ', 2, ', * c) 1, 2, ', *, 5, -, /, %,  d) 4, 1, 2, 1, 2, ', 2, ', * nswer:b $xplanation:$xecute $xplanation:$xecute in the shell to &eri"y

2 How many elements are in m? m K x, y "or x in range;4, *) "or y in range;4, *) a) % b) 12

144

c) 1d) '2 nswer:c $xplanation:$xecute in the shell to &eri"y ' What will be the output? &alues K ', *, 5, 1, '', -, 1, 2 & K &alues44 "or row in range;4, len;&alues)): len;&alues)): "or column in range;4, len;&aluesrow)): len;&aluesrow)): i" &  &aluesrowcolumn: &aluesrowcolumn: & K &aluesrowcolumn &aluesrowcolumn print;&) a) ' b) 5 c) d) '' nswer:d $xplanation:$xecute in the shell to &eri"y *+ What will be the output? &alues K ', *, 5, 1, '', -, 1, 2 & K &alues44 "or lst in &alues: "or element in lst: i" &  element: & K element print;&) a) 1 b) ' c) 5 d) nswer:a $xplanation:$xecute in the shell to &eri"y 5 What will be the output? &alues K ', *, 5, 1 , '', -, 1, 2 "or row in &alues:   rowsort;) "or element in row: print;element, print;element, end K R R)   print;) a) The program prints two rows ' * 5 1 "ollowed by '' - 1 2 b) The program prints on row ' * 5 1 '' - 1 2 c) The program prints two rows ' * 5 1 "ollowed by '' - 1 2 d) The program prints two rows 1 ' * 5 "ollowed by 1 2 - '' nswer:d $xplanation:$xecute in the shell to &eri"y - What is the output?

matrix K 1, 2, ', *, *, 5, -, /, %, , 14, 11, 12, 1', 1*, 15 "or i in range;4, *): print;matrixi1, print;matrixi1, end K R R) a) 1 2 ' * b) * 5 - / c) 1 ' % 12 d) 2 5  1' nswer:d $xplanation:$xecute in the shell to &eri"y / What will be the output? de" m;list): & K list4 "or e in list: i" &  e: & K e return & &alues K ', *, 5, 1, '', -, 1, 2 "or row in &alues: print;m;row), print;m;row), end K R R) a) ' '' b) 1 1 c) 5 d) 5 '' nswer:d $xplanation:$xecute in the shell to &eri"y % What will be the output? data K 1, 2, ', *, 5, -, /, % print;data144) a) 1 b) 2 c) * d) 5 nswer:d $xplanation:$xecute in the shell to &eri"y  What will be the output? data K 1, 2, ', *, 5, -, /, % de" ttt;m): & K m44 "or row in m: "or element in row: i" &  element: & K element return & print;ttt;data4))

a) 1 b) 2 c) * d) 5 nswer:c $xplanation:$xecute in the shell to &eri"y 14 What will be the output? points K 1, 2, ', 15, 45, 45 pointssort;) print;points) a) 1, 2, ', 15, 45, 45 b) ', 15, 1, 2, 45, 45 c) 45, 45, 1, 2, ', 15 d) 45, 45, ', 15, 1, 2 nswer:c $xplanation:$xecute in the shell to &eri"y ,his set o Python )uestions - (nswers ocuses on .ictionaries0+

1 Which o the ollowing statements create a dictionary?3multiple answers allowed4 a) d K VX b) d K VNjohn]:*4, Npeter]:*5X c) d K V*4:]john], V*4:]john], *5:]peter]X d) d K ;*4:]john], *5:]peter]) nswer:a,b,c $xplanation:ictionaries are created by speci"ying (eys and &alues 2 hat are the (eysZ d K VRjohnR:*4, RpeterR:*5X a) Njohn], *4, *5, and Npeter] b) Njohn] and Npeter] c) *4 and *5 d) d K ;*4:]john], *5:]peter]) nswer:b $xplanation:ictionaries $xplanation:ictionaries appear in the "orm o" (eys and &alues ' What will be the output? d K VRjohnR:*4, RpeterR:*5X RjohnR in d a) True b) =alse c) 9one d) $rror nswer:a $xplanation:+n can be used to chec( i" the (ey is int dictionary * What will be the output? d1 K VRjohnR:*4, RpeterR:*5X d2 K VRjohnR:*--, RpeterR:*5X d1 KK d2 a) True b) =alse

c) 9one d) $rror nswer:b $xplanation:+" d2 was initiali?ed as d2 K d1 the answer would be true 5 What will be the output? d1 K VRjohnR:*4, RpeterR:*5X d2 K VRjohnR:*--, RpeterR:*5X d1  d2 a) True b) =alse c) $rror d) 9one nswer:c $xplanation:rithmetic $xplanation:rithmetic  operator cannot be used with dictionaries -+ What is the output? d K VRjohnR:*4, RpeterR:*5X dRjohnR a) *4 b) *5 c) Njohn] d) Npeter] nswer:a $xplanation:$xecute in the shell to &eri"y / *uppose d ; L.$ohn06D2 .peter06DCM2 .peter06DCM2 to delete the entry or .$ohn0 what command do we use a) ddelete;Njohn]:*4) b) ddelete;Njohn]) c) del dRjohnR d) del d;Njohn]:*4) Fiew nswer nswer:c $xplanation:$xecute in the shell to &eri"y % *uppose d ; L.$ohn06D2 .peter06DCM2 to obtain the number o entries in dictionary what command do we use a) dsi?e;) b) len;d) c) si?e;d) d) dlen;) nswer:b $xplanation:$xecute in the shell to &eri"y  What will be the output? d K VRjohnR:*4, RpeterR:*5X print;list;d(eys;))) a) RjohnR, RpeterR b) RjohnR:*4, RpeterR:*5

c) ;Njohn], Npeter]) d) ;Njohn]:*4, Npeter]:*5) nswer:a $xplanation:$xecute in the shell to &eri"y 14 *uppose d ; L.$ohn06D2 .peter06DCM2 what happens when retieving a value using dFNsusanN? a) 8ince Nsusan] is not a &alue in the set, Python raises a ey$rror exception b) +t is executed !ne and no exception is raised, and it returns 9one c) 8ince Nsusan] is not a (ey in the set, Python raises a ey$rror exception d) 8ince Nsusan] is not a (ey in the set, Python raises a syntax error nswer:c $xplanation:$xecute in the shell to &eri"y ,his set o Python )uestions - (nswers ocuses on .tuples0+ 1 hich o" the "ollowing is a Python tupleZ a) 1, 2, ' b) ;1, 2, ') c) V1, 2, 'X d) VX

nswer:b $xplanation:Tuples are characterised by their round brac(ets 2 *uppose t ; 3=2 >2 D2 42 which o the ollowing is incorrect? a) print;t') b) t' K *5 c) print;max;t)) d) print;len;t)) Fiew nswer nswer:b $xplanation:Falues $xplanation:Falues cannot be modi!ed in the case o" tuple ' What will be the output? tK;1,2,*,') t1:' a) ;1, 2) b) ;1, 2, *) c) ;2, *) d) ;2, *, ') nswer:c $xplanation:8licing $xplanation:8licing just as in the case o" strings ta(es place in tuples * What will be the output? tK;1,2,*,') t1:#1 a) ;1, 2) b) ;1, 2, *) c) ;2, *) d) ;2, *, ') nswer:c $xplanation:8licing $xplanation:8licing just as in the case o" strings ta(es place in tuples

5 hat will be the outputZ t K ;1, 2, *, ', %, ) ti "or i in range;4, len;t), 2) a) 2, ',  b) 1, 2, *, ', %,  c) 1, *, % d) ;1, *, %) nswer:c $xplanation:$xecute in the shell to &eri"y - What will be the output? d K VRjohnR:*4, RpeterR:*5X dRjohnR a) *4 b) *5 c) Njohn] d) Npeter] nswer:a $xplanation:$xecute in the shell to &eri"y / What will be the output? t K ;1, 2) 2 \ t a) ;1, 2, 1, 2) b) 1, 2, 1, 2 c) ;1, 1, 2, 2) d) 1, 1, 2, 2 nswer:a $xplanation:\ operator concatenates tuple %+ What will be the output? t1 K ;1, 2, *, ') t2 K ;1, 2, ', *) t1  t2 a) True b) =alse c) $rror d) 9one nswer:b $xplanation:$lements $xplanation:$lements are compared one by one in this case  What will be the output? myItuple K ;1, 2, ', *) myItupleappend; myItupleappend; ;5, -, /) ) print len;myItuple) a) 1 b) 2 c) 5 d) $rror nswer:d $xplanation:Tuples are immutable and donUt ha&e an append method n exception is thrown in this case

14 What will be the output? number@ames K VX number@ames;1,2,*) number@ames;1,2,*) K % number@ames;*,2,1) number@ames;*,2,1) K 14 number@ames;1,2) number@ames;1,2) K 12 sum K 4 "or ( in number@ames: sum DK number@ames( print len;number@ames) D sum a) '4 b) 2* c) '' d) 12 nswer:c $xplanation:Tuples can be used "or (eys into dictionary The tuples can ha&e mixed length and the order o" the items in the tuple is considered when comparing the euality o" the (eys ,his set o Python )uestions - (nswers ocuses on .fles0+

1 ,o open a fle c6
View more...

Comments

Copyright © 2017 DATENPDF Inc.