












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!













We'llvusevtop- downvdesignvasvavwayvofvorganizingvmanyvofvourvprograms.vWhichvofvthevfollowingvstepsvisvinclude dvinvthevtop-downvdesignvprocess?
A.vBreakvthevproblemvintovindividualv"large"vsteps
B.vBreakvindividualv"large"vstepsvintovsmallervsteps
C.vStopvthevdesignvprocessvwhenvthevcodevshouldvbev"obvious"vfromvthevdescriptionv-vcorrect- answers-A,B,C
Avhierarchyvhelpsvmanagevcomplexity.v-vcorrect-answers-True
Top- downvdesignvrefersvtovtakingvavproblemvandvcreatingvavhierarchyvbyvbreakingvitvdownvfromvthevtop -mostvlevelvofvthevhierarchyvdownward.v-vcorrect-answers-True
Youvcanvusevthevtop- downvdesignvmethodvtovapproachvmanyvproblems,vincludingvengineeringvchallenges.v-vcorrect- answers-True
Example:vUniversityv->vAcademic/Non-Academicv->vColleges/Officesv->vDepartmentsv-
vIndividuals
Questionvtovanswerv(T/F):vWhenvwevencountervavhierarchyvlikevthisvinvcomputing,vwevusuallyvcallvit vav"Branch".v-vcorrect-answers-False
Avhierarchicalvtreevstructurevwillvhavevav"root"vatvthevbase.v-vcorrect-answers-True
Invavhierarchicalvtreevstructure,vanyvonev"leaf"vnodevwillvhavevav"parent"v(thevnodevjustvabove).v- vcorrect-answers-True
Invavhierarchicalvtreevstructure,vanyvonev"leaf"vnodevwillvhavev"children"v(thevnodesvthatvdescendvf romvit,vbelow).v-vcorrect-answers-False
Invavhierarchicalvtreevstructure,vthevnodesvwithoutvchildrenvarevcalledv"roots".v-vcorrect-answers- False
ThevfollowingvarevsomevofvthevadvantagesvofvHierarchies:
Invavhierarchy,vavnodevwithoutvchildrenvisvcalledvav"seed"v-vcorrect-answers-False
Invcomputing,vavhierarchyvisvoftenvcalledvav"tree"v-vcorrect-answers-True
Invavhierarchy,vthev"root"vnodevhasvnovparentsvandvmanyvchildrenv-vcorrect-answers-True
Hierarchiesvhelpvusvmanagevandvorganizevcomplexvthingsv-vcorrect-answers-True
xv=v 5
my_function()
print(x)v-vcorrect-answers-False
Thev"right"vwayvtovaccessvdatavfromvavfunctionvisvtovpassvinvthevdatavviavarguments/parameters.v- vcorrect-answers-True
Functionsvshouldvstandvonvtheirvownv- vyouvshouldvunderstandvthemvbyvlookingvjustvatvthevfunction,vwithoutvknowingvaboutvthingsvoutsi devit!v-vcorrect-answers-True
defvF(a):
av+=v 3
av=v 5
F(a)
print(a)
Thisvcodevwillvoutputv 8 v-vcorrect-answers-False
defvF():
av+=v 3 v
print(a)
av=v 2
F()
Thisvcodevwillvoutputv 5 v-vcorrect-answers-False
defvF():
globalva
av+=v 3
av=v 5
F()
print(a**2)
Thisvcodevwillvoutputv 25 v-vcorrect-answers-False
defvF(a):
print(a)
av=v 10
bv=v 5
F(b)
Thisvcodevwillvoutputv 5 v-vcorrect-answers-True
Whenvthevreturnvcommandvisvencounteredvinvavfunction'svbody,vthevfunctionvimmediatelyvends,vr eturningvthevvaluevspecifiedv(ifvany).v-vcorrect-answers-True
defvdosomething(a,vb):
Booleansv-vcorrect-answers-dictionaries,vlists
Whichvofvthevfollowingvvaluesvforvavandvbvarevvalidvforvthevfunctionvshownvbelowv(willvNOTvresultvi nvanverror)?vPleasevselectvallvthatvapply.
defvmyfunc(a,vb):v
cv=vav*vbv
returnvc
av=v1.0vbv=v 2
av=v'1'vbv=v2.
av=v 1 vbv=v 2
av=v[1]vbv=v 2 v-vcorrect-answers-av=v1.0vbv=v 2
av=v 1 vbv=v 2
av=v[1]vbv=v 2
Whatvisvthevoutputvofvthevfollowing
myintv=v 1
mylistv=v[3,v2,v1]
mytuplev=v(myint,vmylist)
myintv=v 2025
mylist[0]v=v 10
print(mytuple)v-vcorrect-answers-(1,v[10,v2,v1])
Whatvisvthevoutputvofvthevfollowingvcode?
defvmyfunc():v
av=v 8 v
print(a,vendv=v'.')v
returnv
av=v 5
print(av,vendv=v'.')
myfunc()
print(av,vendv=v'.')v-vcorrect-answers-5.8.
WhenvthevPythonvinterpretervencountersvavfunctionvdefinition,vitv"remembers"vthevnamevofvthevf unctionvandvnumbervofvparameters.vItvdoesvnotvgovthroughvthevfunctionvbodyvuntilvthevfunctionvisv called.v-vcorrect-answers-True
Whilevbettervprogrammersvdovtendvtovmakevfewerverrorsvwhenvcoding,vtheyvstillvmakevthem.v- vcorrect-answers-True
Studiesvhavevshownvthatvbettervprogrammersvnotvonlyvmakevfewerverrors,vbutv(evenvmorevsignific antly)vtheyvarevablevtovfindvandvfixverrorsvmorevquickly.v-vcorrect-answers-True
Thevfollowingvcodevhasvavsyntaxverrorv(T/F)
print('Currentvsalaryvis',vend='')
print(45000)
new_salv=v 55000
print('Newvsalaryvis')vprint(new_sal)v-vcorrect-answers-True
Avlogicverrorvisvoftenvcalledvavmoth.v-vcorrect-answers-False
Thevtry-exceptvstatementvcanvbevusedvforvdealingvwithvrun-timeverrors.v-vcorrect-answers-True
Executionvjumpsvtovanvexceptvblockvonlyvifvanverrorvoccursvinvthevprecedingvtryvblock.v-vcorrect- answers-True
Aftervanverrorvoccursvinvavtryvblock,vandvthevfollowingvexceptvblockvhasvexecuted,vexecutionvresum esvaftervwherevtheverrorvoccurredvinvthevtryvblock.v-vcorrect-answers-False
Ifvwevdon'tvhavevavdebugger,vwevcanvprintvoutvvariablevvaluesvalongvthevwayvsovwevcanvseevwhatsvh appening.v-vcorrect-answers-True
xv=vint(input("Pleaseventervavpositivevnumber:v"))
totalv=v 0
forvivinvrange(x):v
totalv+=viv+v 1
print(f"Thevsumvofvnumbersv 1 vtov{x}visv{total}")
Whatvtypevofverror:
syntaxverror
run-timeverror
logicverror
Therevisvnoverror,vthevcodevisvperfect!v-vcorrect-answers-run-timeverror
1 vnumv=vinput(Entervanvinteger:v")
2 vsumdv=v 0
3 vforvnvinvnum:
4 vsumdv+=vint(n)v**vlen(num)
5 vifvsumdv==vnum:
6 vprint(f"{num}visvanvArmstrongvnumber")
7 velse:
8 vprint(f"{num}visvnotvanvArmstrongvnumber")
Whatvlinesvarevthereverrors?v-vcorrect-answers-1,v 5
Herevisvthevbasicvstructurevforvopeningvavfile:
Aftervopeningvavfilevusingvthevopenvcommand,vthevvariablevfileIDvwillvcontainvthevinformationvthev operatingvsystemvneedsvtovaccessvthevfile.v-vcorrect-answers-True
Thevfilevidentifiervcanvbevanyvvalidvvariablevname,vforvexample:vfileID,vmy_file,vfid,vfile1,vetc.v- vcorrect-answers-True
Herevisvthevbasicvstructurevforvopeningvavfile:
Thevdesignatorvsaysvhowvwevarevgoingvtovbevusingvthevfile.vItvisvsometimesvreferredvtovasvthevmode vforvworkingvwithvthevfile.v-vcorrect-answers-True
wherevthevfileIDvisvthevfilevidentifierv(variablevname)vforvavfilevthatvwasvopenedvforvwritingvorvappe nding.v-vcorrect-answers-True
writevwillvwritevonlyvavsinglevstringv-vcorrect-answers-True
writevwillvwritevstrings,vfloats,vintegers,vetc.v-vcorrect-answers-False
writevwillvputvavcarriagevreturn/newvlinevaftervwritingv-vcorrect-answers-False
Whenvwritingvtovavfile,vthevmodevofvthevfilevmustvbevexplicitlyvsetvinvthevopen()vfunctionvcall.v- vcorrect-answers-True
Thevmodev'w'vopensvavfilevforvwriting.vIfvthevfilevisvmissing,vthenvavnewvfilevisvcreated.vContentsvofva nyvexistingvfilevarevoverwritten.v-vcorrect-answers-True
Assumevwevhavevavtextvfilevopenvforvreadingvandvthevvariablevmyfilevisvthevfilevidentifier.
next_linev=vmyfile.readline()
Thisvlinevofvcodevreadsvonevlinevofvthevfilevatvavtime.v-vcorrect-answers-True
Wevcanvusevavversionvofvthevforvloopvtoviteratevthroughvlinesvinvthevfile.
forvlinevinvmyfile:
#Dovstuffvwithvthevstringvlinev-vcorrect-answers-True
Willvreadvtheventirevfilevintovonevsinglevstringv-vcorrect-answers-True
Bothvofvthesevwillvconvertvallvthevlinesvinvthevfilevintovavlistvofvstrings.v-vcorrect-answers-True
Thevsplitvmethodvwillvconvertvavstringvintovavlistvofvstrings,vbasedvonvavseparatorvthatvisvspecified.v- vcorrect-answers-True
Thevjoin()vstringvmethodvperformsvthevinversevoperationvofvsplit().vItvjoinsvavlistvofvstringsvtogetherv tovcreatevavsinglevstring.v-vcorrect-answers-True
xv=v"".join(['1',v'2',v'3'])
Thevtypevofvthevvariablevxvisvinteger.v-vcorrect-answers-False
Thevwritevcommandvautomaticallyvaddsvavnewlinevcharactervatvthevendvofvyourvlinev-vcorrect- answers-False
WhichvofvthevfollowingvisvthevcorrectvwayvtovopenvavfilevinvPython?vChoosevallvthatvapply.
myfile.open("log_file.csv",v'a')
myfilev=vopen("mygrades.txt",v'r+')
InvCaptainvJamesvT.vKirk'svvoice:vComputer,vopenvfilev"StarTrekIsAwesome.csv"
withvopen("datafile.dat",v'w')vasvmyfile:v-vcorrect-answers-myfilev=vopen("mygrades.txt",v'r+')
close(myfile)
close("CoolFilename.bro")
"CoolFilename.bro"v=vclose()v-vcorrect-answers-myfile.close()
Whatvisvthevoutputvofvthevfollowingvcode?
mystrv=v'v\tvAggievengineersvarevawesome!v\n'mylistv=vmystr.strip().split('a')print(mylist)
['Aggievengineersv',v'arev',v'awesome!']
['Aggievengineersvrevwesome!']
['Aggievengineersva',v'reva',v'wesome!']
['Aggievengineersv',v'rev',v'wesome!']v-vcorrect-answers-['Aggievengineersv',v'rev',v'wesome!']
Usingvfunctionsvinvcodevpresentsv(alongvwithvsequential,vconditional,vandvrepetition)vanothervmo delvforvorganizingvcomputation.v-vcorrect-answers-True
Sincevfunctionsvarevdefinedvseparatelyvfromvthevrestvofvthevcode,vtheyvdon'tvnecessarilyvhavevacce ssvtovthevsamevdatavasvinvthevmainvcode.v-vcorrect-answers-True
Wevpassvdatavintovthevfunctionvusingvarguments.v-vcorrect-answers-True
Datavisvreturnedvfromvthevfunctionvbyvavreturnvvalue.vWevcanvthenvassignvthevreturnedvvaluevtovso mevvariable,vorvusevitvinvanvexpression.v-vcorrect-answers-True
Somevfunctionsvwillvtakevvaryingvnumbersvofvarguments.v-vcorrect-answers-True
Avtuplevisvlikevavlist,vwithvonevimportantvdistinction:vitvcannotvchangevvalues.vThatvis,vtuplesvarevim mutable.v-vcorrect-answers-True
Wevcanvbreakvoutvthevelementsvofvavtuplevwithvmultiplevassignmentvinvavsinglevline.vByvputtingvmul tiplevvariablesvonvthevleftvsidevofvthevequalvsign,vPythonvknowsvtovassignvthevelementsvofvthevtuplev sequentiallyvtovthesevvariables.vForvexample,
a,vb,vcv=v(1,v2,v3)v-vcorrect-answers-True
a,vbv=v(5,v10,v15)
Aftervthisvlinevisvexecuted,vthevvaluevofvbvisv(10,v15)v-vcorrect-answers-False
my_tuplev=v(5,v10,v15,v20)
av=vmy_tuple[1:3]
Aftervthevcodevabovevisvexecuted,vthevvaluevofvavisv(5,v10,v15)v-vcorrect-answers-False
my_tuplev=v(5,v10,v15,v20)
my_tuple[1]v=v 25
Aftervthevcodevabovevisvexecuted,vthevvaluevofvmy_tuple[1]visv 25 v-vcorrect-answers-False
a,vb,vcv=v(1,v'True',v25.5)
Thevtypevofvvariablevbvisvstring.v-vcorrect-answers-True
a,vb,vc,vdv=v(1,v'Hello',v[25.5,v3.34],v7)
Returnsvnumvevenlyvspacedvsamples,vcalculatedvovervthevintervalv[start,vstop].vThevendpointvofvth evintervalvcanvoptionallyvbevexcluded.v-vcorrect-answers-True
Invtop-downvdesign,vwevtakevsomevcomplexvtaskvandvbreakvitvintovavsequencevofvsimplervtasks.v- vcorrect-answers-True
Invthevtop- downvhierarchy,vthevleafvnodesvcanvbevindividualvfunctionsvthatvdon'tvcallvanyvothervfunctions.vAn d,vthevhighervlevelsvinvthevhierarchyvarevfunctionsvthatvdovcallvothervfunctions.v-vcorrect-answers- True
Avfunctionvshouldvhavevavsinglevclearvpurposevandvgoal.v-vcorrect-answers-True
Eachvfunctionvshouldvbeveasyvtovunderstandvonvitsvown.vWhenvwevhavevavfunction,vitvshouldvjustvd ovitsvjobvwithvwhatevervparametersvarevpassedvin,vandvnotvworryvaboutvthevbiggervworldvaroundvit .v-vcorrect-answers-True
Invcode,vthevhigher-levelvpartsvofvthevhierarchyvcomevAFTERvthevlower- levelvpartsvofvthevhierarchy.vThevreasonvforvthisvisvthatvwevneedvthevchildrenvfunctionsvtovbevdefine dvfirst,vsovthatvtheirvparentvcanvcallvthem.v-vcorrect-answers-True
Thevideavofvbottom- upvdesignvisvthatvyouvtakevexistingvpiecesvofvcode,vorvexistingvtechniques,vandvcombinevthemvtovdo vsomethingvnew.v-vcorrect-answers-True
Bottom-upvprogramsvstillvresultvinvavhierarchyvofvfunctions.vBut,vthevlower- levelvfunctionsvarevdesignedvfirst,vandvthenvgraduallyvbuiltvtovthevhighervones.v-vcorrect-answers- True
Top-downvandvbottom-upvdesignsvwillvalwaysvresultvinvveryvdifferentvprograms.v-vcorrect-answers- False
Invpractice,vtop-downvapproachesvarevalwaysvusedvtovdevelopvmodules.v-vcorrect-answers-False
Bothvtop-downvandvbottom-upvdesignsvendvupvcreatingvavhierarchyvofvfunctions.v-vcorrect- answers-True
Top- downvfunctionsvcanvonlyvbevtestedvafterveverythingvbelowvthemvinvthevhierarchyvhasvbeenvimplem entedvinvsomevway.v-vcorrect-answers-True
Withvtop-downvdesign,vcodevcanvbecomevtoovspecialized,vandvit'sveasiervtovmissvchancesvtovre- usevcodevandvideas.v-vcorrect-answers-True
Botton-upvdesignvprocessvdoesvnotvhavevanyvdrawbacks.v-vcorrect-answers-False
Programmersvrarelyvusevavstrictvtop-downvorvbottom- upvapproach,vbutvrathervtheyvusevdifferentvaspectsvofveachvone,vorvothervapproachesvlikevobject- orientedvdesign,vdependingvonvthevproblemvandvgoals.v-vcorrect-answers-True
Thevkeyvtovwhyvtop-downvdesign,vbottom- upvdesign,vandvfunctionsvarevusefulvisvthatvwevcanvignorevthevdetailsvofvhowvothervpartsvofvthevpro gramvwork,vwhilevwevfocusvonvonevparticularvpart.vThisvisvcalledvabstraction.v-vcorrect-answers- True