=========================preview======================
(COMP151)comp151_01s_midterm_sol.pdf
Back to COMP151 Login to download
======================================================
COMP151{Lectures1,2,3
MidtermApril4,2001
ExamTime{90minutes

Name: E-mail: StudentNo: Lecture/LabSection:
Answerallofthefollowingquestions.Doyourworkontheexampaper. Ifnecessary,youmaywriteonthebackofthesheets.
1.(4points)Considerthefollowingclassde.nitions
classLength
{public:
Length(intf,inti).
Length(intf).
intGetLength().

private:
intfeet.intinches.

}.
Length::Length(intf,inti)
{feet.f.inches.i.}

Length::Length(intf)
{feet.f.inches.0.}

intLength::GetLength()
{return12*feet+inches.}

Foreachofthefollowingdeclarationsunderlinewhetheritisalegalorillegaldeclaration ofanobjectAintheclassLength:Eachlineworthonepoint
LegalIllegalLengthA.
LegalIllegalLengthA().
LegalIllegalLengthA(1,2).
LegalIllegalLengthA(1,2,3).

2.(4points)Considerthefollowingfunctionimplementation:
voidDo(int*p1,int&p2)
{*p1.*p1-p2.
p2.*p1+p2.
}
Supposeinti.5,j.7..
WhatvaluesarestorediniandjafterthecallDo(&i,j).1pointeach.

i..-2.j..5.
Nowsupposeinti.5.
WhatisthevaluestorediniafterthecallDo(&i,i).2points

i..0.
3.(10points)Youaregiventhefunctionde.nition
voidf(constint&x,int&y){cout.."x:"..x.."".."y:"..y..endl.}
andthefollowingvariablede.nitions/initializations.
intx.5.
constinty.6.
int*constI1ptr.&x.
intconst*I2ptr.&x.
Assumingtheabovede.nitionsanddeclarations,decide,foreachofthefollowing statements,whetheritislegalorillegal,andunderlinetheappropriateanswer: 2ptseachfor.rstandseventhlines.1ptforeachoftheothers.
LegalIllegalf(x,y).
LegalIllegalf(y,x).
LegalIllegal*I1ptr.6.
LegalIllegal*I2ptr.5.
LegalIllegalx.6.
LegalIllegalint*ptr.&y.
LegalIllegalI1ptr.I2ptr.
LegalIllegalI2ptr.I1ptr.

4.(14points)Whatistheoutputofthefollowingprogram.Iftheprogramcontainsan errorthatwouldcauseittofail(memoryleakageisnotconsideredsuchafault)(i).rst printouttheoutputuptothefailurepoint,(ii)thenpointouttheerror(underlineits exactlocationintheprogram)and(iii).nallyexplainwhyitisanerror.Ifthereis morethanoneerroryouonlyhavetoidentifythe.rstone.
#include.iostream.h.
main()
{intX.1.
int*P.newint.
int*Q.newint.
int*R.

R.P. *P.5. P.&X. *Q.6.
cout..X..""..*P..""..*Q..""..*R..endl.

X.2. *R.*P. *P.7.
cout..X..""..*P..""..*Q..""..*R..endl.

Q.P. P.newint.*P.8. R.P.
cout..X..""..*P..""..*Q..""..*R..endl.

deleteP. *Q.4. P.Q.
cout..X..""..*P..""..*Q..""..*R..endl.
}

Output
1165
7762
7878
444

errorisintheverylastlinewhenrunning
cout..""..*P..""..*Q..""..*R..endl.
whentryingtooutput*R.Theerrorisadanglingpointer.Rhadbeenpointingto thesamethingasPbutafterdeleteP,Rhadbeenleftdanglingpontingtonothing. Accessing*Rmaythereforecauseafailure.
MarkingGuidlines:
.1/2ptpercorrectanswerinlines1and2
.1ptpercorrectanswerinlines3and4
.1ptforcorrectlycirclingtheerror(1/2ptif"deleteP"iscircled)
.2ptforcorrectexplanationofwhyitisanerror
5.(16points)Thispagecontainsthede.nitionofaclassthingandsomefunctions.The nextpagecontainsaprogramthatusesthing.Writedowntheoutputoftheprogram.
#include.iostream.h.