CIS 430 Chapter6ReviewQuestions

Embed Size (px)

Citation preview

  • 8/12/2019 CIS 430 Chapter6ReviewQuestions

    1/9

    CS464 Steve BazinetChapter 6 Review Questions 11/19/05

    Topic Reference

    Table of Contents

    Table of Contents.........................................................................................................................1Problem # 1 !es"riptors............................................................................................................Problem # !e"imal !ata T$pes..............................................................................................Problem # % Chara"ter Strin& T$pes.........................................................................................Problem # 4 Strin& 'en&th (ptions...........................................................................................Problem # 5 (r)inal* +n,meration* an) S,bran&e T$pes........................................................%Problem # 6 -ser!efine) +n,meration T$pes........................................................................%

    Problem # ore (n -ser!efine) +n,meration T$pes.........................................................%Problem # 2rra$s....................................................................................................................%Problem # 9 Stati"* !$nami"* Sta"3s* eaps............................................................................4Problem # 10 2)as 2rra$ nitialization......................................................................................4Problem # 11 2&&re&ate Constant............................................................................................4Problem # 1 2)a Sin&le!imensional 2rra$s..........................................................................5Problem # 1% Sli"es (f 7ortran 8ers,s 2)a.............................................................................5Problem # 14 o: a;or 8ers,s Col,mn a;or (r)er.............................................................5Problem # 15 2""ess 7,n"tion..................................................................................................5Problem # 16

  • 8/12/2019 CIS 430 Chapter6ReviewQuestions

    2/9

    CS464 Steve BazinetChapter 6 Review Questions 11/19/05

    Topic Reference

    Problem # 1 Descriptors@hat is a )es"riptorA p. 253

    t is "onvenient* both lo&i"all$ an) "on"retel$* to thin3 of variables in terms of )es"riptors. 2

    descriptoris the "olle"tion of the attrib,tes of a variable.ore )etailn an implementation* a )es"riptor is a "olle"tion of memor$ "ells that store variable attrib,tes. f theattrib,tes are all stati"* )es"riptors are re,ire) onl$ at "ompile time. These )es"riptors are b,ilt b$ the "ompiler*,s,all$ as a part of the s$mbol table* an) are ,se) ),rin& "ompilation. 7or )$nami" attrib,tes* ho:ever* part orall of the )es"riptor m,st be maintaine) ),rin& eDe",tion. n this "ase* the )es"riptor is ,se) b$ the r,ntimes$stem. n all "ases* )es"riptors are ,se) for t$pe "he"3in& an) to b,il) the "o)e for the allo"ation an))eallo"ation operations.

    Problem # 2 Decimal Data Types@hat are the a)vanta&es an) )isa)vanta&es of )e"imal )ata t$pesA p. 255

    !e"imal )ata t$pes store a fiDe) n,mber of )e"imal )i&its* :ith the )e"imal point at a fiDe)

    position in the val,e. !e"imal t$pes have the a)vanta&e of bein& able to pre"isel$ store)e"imal val,es* at least those :ithin a restri"te) ran&e* :hi"h "annot be )one :ith floatin&point.

    The )isa)vanta&es of )e"imal t$pes are that the ran&e of val,es is restri"te) be"a,se noeDponents are allo:e)* an) their representation in memor$ is :astef,l.

    Problem # 3 Caracter !trin" Types@hat are the )esi&n iss,es for "hara"ter strin& t$pesA p. 257

    2 "hara"ter strin& t$pe is one in :hi"h the val,es "onsist of se,en"es of "hara"ters. The t:omost important )esi&n iss,es that are spe"ifi" to "hara"ter strin& t$pes are the follo:in&

    Sho,l) strin&s be simpl$ a spe"ial 3in) of "hara"ter arra$ or a primitive t$pe E:ith no arra$st$le s,bs"riptin& operationsFA

    Sho,l) strin&s have stati" or )$nami" len&thA

    Problem # !trin" $en"t %ptions!es"ribe the three strin& len&th options. p. 259

    2 static len"t strin"is a strin& :hose len&th is stati" an) set :hen the strin& is "reate).

    2 limited dynamic len"t strin"is a strin& that has a var$in& len&th ,p to a )e"lare) an)fiDe) maDim,m set b$ the variables )efinition. S,"h strin& variables "an store an$ n,mber of"hara"ters bet:een zero an) the maDim,m.

    2 dynamic len"t strin"is a strin& that has a var$in& len&th an) no maDim,m. This optionre,ires the overhea) of )$nami" stora&e allo"ation an) )eallo"ation b,t provi)es maDim,mfleDibilit$.

    of 9

  • 8/12/2019 CIS 430 Chapter6ReviewQuestions

    3/9

    CS464 Steve BazinetChapter 6 Review Questions 11/19/05

    Topic Reference

    Problem # & %rdinal' (numeration' and !ubran"e Types!efine ordinal* enumeration* an) subrangetypes. p. 261

    2n ordinal typeis one in :hi"h the ran&e of possible val,es "an be easil$ asso"iate) :ith the

    set of positive inte&ers. n

  • 8/12/2019 CIS 430 Chapter6ReviewQuestions

    4/9

    CS464 Steve BazinetChapter 6 Review Questions 11/19/05

    Topic Reference

    Problem # 0 !tatic' Dynamic' !tacs' eaps!efine static* fixedstack-dynamic* stack-dynamic* fixed eap-dynamic* an) p. 26!eap-dynamic arrays. @hat are the a)vanta&es of ea"hA

    2 static arrayis one in :hi"h the s,bs"ript ran&es are stati"all$ bo,n) an) stora&eallo"ation is stati" E)one before r,n timeF. The a)vanta&e of stati" arra$s is effi"ien"$Go )$nami" allo"ation or )eallo"ation is re,ire).

    2 fied stac+dynamic arrayis one in :hi"h the s,bs"ript ran&es are stati"all$ bo,n)*

    b,t the allo"ation is )one at )e"laration elaboration time ),rin& eDe",tion. Thea)vanta&e of fiDe) sta"3)$nami" arra$s over stati" arra$s is spa"e effi"ien"$.

    2 stac+dynamic arrayis one in :hi"h the s,bs"ript ran&es are )$nami"all$ bo,n) an)

    the stora&e allo"ation is )$nami" E)one ),rin& r,n timeF. (ne the s,bs"ript ran&es arebo,n) an) the stora&e is allo"ate)* ho:ever* the$ remain fiDe) ),rin& the lifetime of thevariable. The a)vanta&e of sta"3)$nami" arra$s over stati" an) fiDe) sta"3)$nami"arra$s is fleDibilit$. The size of an arra$ nee) not be 3no:n ,ntil the arra$ is abo,t to be,se).

    2 fied eap+dynamic arrayis similar to the fiDe) sta"3)$nami" arra$* in that the

    s,bs"ript ran&es are )$nami"all$ bo,n) an) the stora&e bin)in& is )$nami"* b,t the$are fiDe) after stora&e is allo"ate). The )ifferen"es are that the bin)in&s are )one :henthe ,ser pro&ram re,ests them* rather than at elaboration time* an) the stora&e isallo"ate) from the heap* rather than the sta"3. 2ss,mption Enot state) in teDtF Thea)vanta&e of fiDe) heap)$nami" arra$s is spa"e effi"ien"$.

    2 eap+dynamic arrayis one in :hi"h the bin)in& of s,bs"ript ran&es an) stora&e

    allo"ation is )$nami" an) "an "han&e an$ n,mber of times ),rin& the arra$s lifetime.The a)vanta&e of heap)$nami" arra$s over the others is fleDibilit$.

    Problem # 14 /da5s /rray 6nitiali7ation@hat arra$ initialization feat,re is available in 2)a that is not available in other p. 271"ommon imperative lan&,a&esA

    n 2)a one "an )ire"tl$ assi&n a val,e to an in)eD position ,sin& the =>operator* :hi"h in 2)a

    is "alle) an arrow. 7or eDample*

    List : array(1..5) ofInteger : (1 ! 1", # ! #$, others ! %);

    Problem # 11 /""re"ate Constant@hat is an a&&re&ate "onstantA p. 271

    2 parenthesize) list of val,es ,se) in initializin& arra$s is "alle) an a""re"ate value. "o,l)nt fin) a referen"e to the term Ha&&re&ate "onstantI in the teDt nor on the Jnet.

    4 of 9

  • 8/12/2019 CIS 430 Chapter6ReviewQuestions

    5/9

    CS464 Steve BazinetChapter 6 Review Questions 11/19/05

    Topic Reference

    Problem # 12 /da !in"le+Dimensional /rrays@hat arra$ operations are provi)e) spe"ifi"all$ for sin&le)imensione) arra$s p. 271in 2)aA

    2)a allo:s arra$ assi&nments* in"l,)in& those :here the ri&ht si)e is an a&&re&ate val,erather than an arra$ name. 2)a also provi)es "atenation* spe"ifie) b$ the ampersan) E>F.Catenation is )efine) bet:een t:o sin&le)imensione) arra$s an) bet:een a sin&le)imensione) arra$ an) a s"alar. Gearl$ all t$pes in 2)a have the b,iltin relational operatorsfor e,alit$ an) ine,alit$.

    Problem # 13 !lices %f 8ortran 9ersus /da@hat are the )ifferen"es bet:een the sli"es of 7ortran 95 an) those of 2)aA p. 273

    2 sliceof an arra$ is some s,bstr,"t,re of the arra$. n 2)a* onl$ hi&hl$ restri"te) sli"es areallo:e) those that "onsist of "onse",tive elements of a sin&le)imensione) arra$. n 7ortran95* sli"in& is m,"h more fleDible. The$ "an be an$ ro:* "ol,mn* parts of ro:s* parts of"ol,mns* &ro,ps of ro:s* or &ro,ps of "ol,mns. (ne "an even sele"t ever$ nth"ol,mn in aro:.

    Problem # 1 Row -a:or 9ersus Column -a:or %rder!efine ro"ma#ororderan) columnma#ororder. p. 275-6

    There are t:o "ommon :a$s in :hi"h m,lti)imensional arra$s "an be mappe) to one)imension ro: ma;or or)er an) "ol,mn ma;or or)er. n row ma:or order* the elements of thearra$ that have as their 1sts,bs"ript the lo:er bo,n) val,e of that s,bs"ript are store) first*follo:e) b$ the elements of the se"on) val,e of the first s,bs"ript* an) so forth. n columnma:or order* the elements of an arra$ that have as their last s,bs"ript the lo:er bo,n) val,e

    of that s,bs"ript are store) first* follo:e) b$ the elements of the se"on) val,e of the lasts,bs"ript* an) so forth.

    # $ " would be stored in row major order as #,$,",&,',5,1,#,.

    & ' 5 would be stored in column major order as #,&,1,$,',#,",5,.

    1 #

    Problem # 1& /ccess 8unction@hat is an a""ess f,n"tion for an arra$A p. 276-7

    2n access functionfor a m,lti)imensional arra$ is the mappin& of its base a))ress an) a set

    of in)eD val,es to the a))ress in memor$ of the element spe"ifie) b$ the in)eD val,es. 7oreDample* this is the a""ess f,n"tion for a t:o)imensional arra$ store) in ro: ma;or or)er:here a1,1*is the base a))ress

    +oation( ai,-* address o a1,1* /

    ((((0 o ros a2o3e ithro) 4 (sie o a ro))

    / (0 o e+e6ents +et o the -tho+u6n)) 4 e+e6ent sie

    5 of 9

  • 8/12/2019 CIS 430 Chapter6ReviewQuestions

    6/9

    CS464 Steve BazinetChapter 6 Review Questions 11/19/05

    Topic Reference

    Problem # 1) ;ava /rray Descriptor@hat are the re,ire) entries in a

  • 8/12/2019 CIS 430 Chapter6ReviewQuestions

    7/9

    CS464 Steve BazinetChapter 6 Review Questions 11/19/05

    Topic Reference

    Problem # 10 *nions!efine union* freeunion* an) discriminatedunion. p. 2!!-9

    2 unionis a t$pe that ma$ store )ifferent t$pe val,es at )ifferent times ),rin& pro&ram

    eDe",tion. ts a))ress is one lo"ation an) its len&th is the lon&est t$pe that it nee)s to store.Some ,nion are "alle) free unionsbe"a,se pro&rammers are allo:e) "omplete free)om fromt$pe "he"3in& in their ,se.

    T$pe "he"3in& of ,nions re,ires that ea"h ,nion "onstr,"t in"l,)e a t$pe in)i"ator. S,"h anin)i"ator is "alle) a ta&* or )is"riminant* an) a ,nion :ith a )is"riminant is "alle) adiscriminated union.

    Problem # 24 *nion Desi"n 6ssues@hat are the )esi&n iss,es for ,nionsA p. 2!!

    The primar$ )esi&n iss,es that are parti",lar to ,nion t$pes are the follo:in&

    Sho,l) t$pe "he"3in& be re,ire)A Gote that an$ s,"h t$pe "he"3in& m,st be )$nami".

    Sho,l) ,nions be embe))e) in re"or)sA

    Problem # 21 /da *nions2re the ,nions of 2)a al:a$s t$pe "he"3e)A p. 2!9

    Les* b,t in t:o )ifferent :a$s. n one :a$ the ,ser is allo:e) to spe"if$ :hi"h t$pes areallo:e) in the ,nion. @hen ,sin& the ,nion at r,n time E)$nami"all$F onl$ previo,sl$ spe"ifie)t$pes are allo:e) to be ,se) in the ,nion.

    n the other :a$* ho:ever* the ,nion "an be "han&e) b,t it m,st be "han&e) "ompletel$ b$

    assi&nin& an entire re"or)* in"l,)in& the )is"riminant. This )isallo:s in"onsisten"$ be"a,sethe ne:l$ assi&ne) re"or) :as alrea)$ "he"3e) at "ompile time Estati"all$F for "onsisten"$.

    Problem # 22 Pointer Desi"n 6ssues@hat are the )esi&n iss,es for pointer t$pesA p. 292

    The primar$ )esi&n iss,es parti",lar to pointers are the follo:in&

    @hat are the s"ope an) lifetime of a pointer variableA

    @hat is the lifetime of a heap)$nami" variableA

    2re pointers restri"te) as to the t$pe of val,e to :hi"h the$ "an pointA

    2re pointers ,se) b$ )$nami" stora&e mana&ement* in)ire"t a))ressin&* or bothA Sho,l) the lan&,a&e s,pport pointer t$pes* referen"e t$pes* or bothA

    of 9

  • 8/12/2019 CIS 430 Chapter6ReviewQuestions

    8/9

    CS464 Steve BazinetChapter 6 Review Questions 11/19/05

    Topic Reference

    Problem # 23 Pointer Problems@hat are the t:o "ommon problems :ith pointersA p. 294-5

    (ne "ommon problem :ith pointers is the dan"lin" pointer* or dan"lin" reference:hi"h is a

    pointer that "ontains the a))ress of a heap)$nami" variable that has been )eallo"ate).The "ommon problem is a lost eap+dynamic variable:hi"h is an allo"ate) heap)$nami"variable that is not lon&er a""essible to the ,ser pro&ram* i.e. it hasnt a pointer. S,"hvariables are "alle) "arba"ebe"a,se the$ are ,n,sable.

    Problem # 2 Pointer Restrictions@h$ are the pointers of most lan&,a&es restri"te) to pointin& at a sin&le t$pe p. 296-7variableA

    The main reason for this is pointer arithmeti". 7or eDample* if tris a pointer variable that is

    )e"lare) to point at some variable of some )ata t$pe* then tr / indeGis a le&al

    eDpression. The semanti"s of s,"h an eDpression is as follo:s. nstea) of simpl$ a))in& theval,e of indeGto tr* the val,e of indeGis first s"ale) b$ the size of the memor$ "ell Ein

    memor$ ,nitsF to :hi"h tris pointin& Eits base t$peF. 7or eDample* if trpoints to a

    memor$ "ell for a t$pe that is fo,r memor$ ,nits in size* then indeGis m,ltiplie) b$ 4* an) the

    res,lt is a))e) to tr. The primar$ p,rpose of this sort of a))ress arithmeti" is arra$

    manip,lation.

    Problem # 2& C?? Reference Type@hat is a C== referen"e t$pe an) :hat is its "ommon ,seA p. 29!-9

    C== in"l,)es a spe"ial 3in) of pointer t$pe* "alle) a reference type. t is ,se) primaril$ for the

    formal parameters in f,n"tion )efinitions. 2 C== referen"e t$pe variable is a "onstant pointerthat is al:a$s impli"itl$ )ereferen"e).

    Problem # 2) C?? Reference 9ariables@h$ are referen"e variables in C== better than pointers for formal parametersA p. 29!

    Be"a,se a C?? reference type variableis a "onstant* it m,st be initialize) :ith the a))ress ofsome variable in its )efinition* an) after initialization a referen"e t$pe variable "an never be setto referen"e an$ other variable.

    Problem # 2, ;ava @ C# Reference Type

    @hat a)vanta&es )o

  • 8/12/2019 CIS 430 Chapter6ReviewQuestions

    9/9

    CS464 Steve BazinetChapter 6 Review Questions 11/19/05

    Topic Reference

    Problem # 2. Aarba"e Reclamation!es"ribe a laz$ an) ea&er approa"hes to re"laimin& &arba&e. p. 3%2-3

    The la7y approacor "arba"e collectionis :hen re"lamation onl$ o"",rs :hen the list of

    available spa"e be"omes empt$. @ith this metho)* the r,ntime s$stem allo"ates stora&e "ellsas re,este) an) )is"onne"ts pointers from "ells as ne"essar$ :itho,t re&ar) for the stora&ere"lamation Eallo:in& &arba&e to a"",m,lateF* ,ntil it has allo"ate) all available "ells. 2t thispoint* a &arba&e "olle"tion pro"ess is be&,n to &ather all the &arba&e left floatin& aro,n) in theheap.

    The ea"er approacor reference counter metodis in"remental an) is )one :henina""essible "ells are "reate). This metho) of re"lamation a""omplishes its &oal b$maintainin& in ever$ "ell a "o,nter that stores the n,mber of pointers that are ",rrentl$ pointin&at the "ell. @hen the referen"e "o,nter rea"hes zero* it means that no pro&ram pointers arepointin& at the "ell* an) it has th,s be"ome &arba&e an) "an be ret,rne) to the list of availablespa"e.

    Problem # 20 ;ava @ C# Reference /ritmetic@h$ :o,l)nt arithmeti" on