Anda di halaman 1dari 9

CS464 Steve Bazinet

Chapter 6 Review Questions 11/19/05


Topic Reference
Table of Contents
Table of Contents.........................................................................................................................1
Problem # 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 # / 0ore (n -ser.!efine) +n,meration T$pes.........................................................%
Problem # 1 2rra$s....................................................................................................................%
Problem # 9 Stati"* !$nami"* Sta"3s* 4eaps............................................................................4
Problem # 10 2)a5s 2rra$ 6nitialization......................................................................................4
Problem # 11 2&&re&ate Constant............................................................................................4
Problem # 1# 2)a Sin&le.!imensional 2rra$s..........................................................................5
Problem # 1% Sli"es (f 7ortran 8ers,s 2)a.............................................................................5
Problem # 14 9o: 0a;or 8ers,s Col,mn 0a;or (r)er.............................................................5
Problem # 15 2""ess 7,n"tion..................................................................................................5
Problem # 16 <ava 2rra$ !es"riptor.........................................................................................6
Problem # 1/ C(B(' 9e"or)s.................................................................................................6
Problem # 11 7iel)s 6n 9e"or)s................................................................................................6
Problem # 19 -nions................................................................................................................./
Problem # #0 -nion !esi&n 6ss,es.........................................................................................../
Problem # #1 2)a -nions........................................................................................................../
Problem # ## Pointer !esi&n 6ss,es........................................................................................./
Problem # #% Pointer Problems................................................................................................1
Problem # #4 Pointer 9estri"tions.............................................................................................1
Problem # #5 C== 9eferen"e T$pe...........................................................................................1
Problem # #6 C== 9eferen"e 8ariables....................................................................................1
Problem # #/ <ava > C# 9eferen"e T$pe.................................................................................1
Problem # #1 ?arba&e 9e"lamation.........................................................................................9
Problem # #9 <ava > C# 9eferen"e 2rithmeti".........................................................................9
1 of 9
CS464 Steve Bazinet
Chapter 6 Review Questions 11/19/05
Topic Reference
Problem # 1 Descriptors
@hat is a )es"riptorA p. 253
6t is "onvenient* both lo&i"all$ an) "on"retel$* to thin3 of variables in terms of )es"riptors. 2
descriptor is the "olle"tion of the attrib,tes of a variable.
0ore )etailB 6n an implementation* a )es"riptor is a "olle"tion of memor$ "ells that store variable attrib,tes. 6f the
attrib,tes are all stati"* )es"riptors are reC,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 or
all of the )es"riptor m,st be maintaine) ),rin& eDe",tion. 6n this "ase* the )es"riptor is ,se) b$ the r,n.time
s$stem. 6n 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 no
eDponents 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 seC,en"es of "hara"ters. The t:o
most important )esi&n iss,es that are spe"ifi" to "hara"ter strin& t$pes are the follo:in&B
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 variable5s )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 option
reC,ires the overhea) of )$nami" stora&e allo"ation an) )eallo"ation b,t provi)es maDim,m
fleDibilit$.
# of 9
CS464 Steve Bazinet
Chapter 6 Review Questions 11/19/05
Topic Reference
Problem # & %rdinal' (numeration' and !ubran"e Types
!efine ordinal* enumeration* an) subrange types. p. 261
2n ordinal type is one in :hi"h the ran&e of possible val,es "an be easil$ asso"iate) :ith the
set of positive inte&ers. 6n <ava* for eDample* the primitive or)inal t$pes are integer* char*
an) boolean.
2n enumeration type is one in :hi"h all of the possible val,es* :hi"h are name) "onstants*
are provi)e) in the )efinition. +n,meration t$pes provi)e a :a$ of )efinin& an) &ro,pin&
"olle"tions of name "onstants* :hi"h are "alle) en,meration "onstants. 2n eDample in C#B
enum days {Mon, Tue, Wed, Thu, Fri, Sat, Sun};
2 subran"e type is a "onti&,o,s s,bseC,en"e of an or)inal t$pe. 7or eDample* p. 264
12 .. 14 is a s,bran&e of inte&er t$pe.
Problem # ) *ser+Defined (numeration Types
@hat are the a)vanta&es of ,ser.)efine) en,meration t$pesA p. 264
+n,meration t$pes "an provi)e a)vanta&es in both rea)abilit$ an) reliabilit$. 9ea)abilit$ is
enhan"e) in a ver$ )ire"t :a$. Game) val,es are easil$ re"o&nize)* :hereas "o)e) val,es
are not. 6n the area of reliabilit$* the en,meration t$pes of 2)a* C#* an) <ava 5.0 provi)e t:o
a)vanta&es. 7irst* no arithmeti" operations are le&al on en,meration t$pes. Se"on)* no
en,meration variable "an be assi&ne) a val,e o,tsi)e its )efine) ran&e.
Problem # , -ore %n *ser+Defined (numeration Types
6n :hat :a$s are the ,ser.)efine) en,merations t$pes of C# more reliable than p. 264
those of C==A
The en,meration t$pes in C# are better than those of C==* be"a,se en,meration t$pe
variables in C# are never "oer"e) to inte&er t$pes.
Problem # . /rrays
@hat are the )esi&n iss,es for arra$sA p. 266
@hat t$pes are le&al for s,bs"riptsA
2re s,bs"riptin& eDpressions in element referen"es ran&e."he"3e)A
@hen are s,bs"ript ran&es bo,n)A
@hen )oes arra$ allo"ation ta3e pla"eA
2re ra&&e) or re"tan&,lar m,lti)imensione) arra$s allo:e)* or bothA
Can arra$s be initialize) :hen the$ have their stora&e allo"ate)A
@hat 3in)s of sli"es are allo:e)* if an$A
% of 9
CS464 Steve Bazinet
Chapter 6 Review Questions 11/19/05
Topic Reference
Problem # 0 !tatic' Dynamic' !tac1s' 2eaps
!efine static* fixed stack-dynamic* stack-dynamic* fixed eap-dynamic* an) p. 26!
eap-dynamic arrays. @hat are the a)vanta&es of ea"hA
2 static array is one in :hi"h the s,bs"ript ran&es are stati"all$ bo,n) an) stora&e
allo"ation is stati" E)one before r,n timeF. The a)vanta&e of stati" arra$s is effi"ien"$B
Go )$nami" allo"ation or )eallo"ation is reC,ire).
2 fi3ed stac1+dynamic array is 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. The
a)vanta&e of fiDe) sta"3.)$nami" arra$s over stati" arra$s is spa"e effi"ien"$.
2 stac1+dynamic array is 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 are
bo,n) an) the stora&e is allo"ate)* ho:ever* the$ remain fiDe) ),rin& the lifetime of the
variable. 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 fi3ed eap+dynamic array is 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 :hen
the ,ser pro&ram reC,ests them* rather than at elaboration time* an) the stora&e is
allo"ate) from the heap* rather than the sta"3. 2ss,mption Enot state) in teDtFB The
a)vanta&e of fiDe) heap.)$nami" arra$s is spa"e effi"ien"$.
2 eap+dynamic array is 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$5s 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
6n 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) of Integer : (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. 6
"o,l)n5t fin) a referen"e to the term Ha&&re&ate "onstantI in the teDt nor on the Jnet.
4 of 9
CS464 Steve Bazinet
Chapter 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. 271
in 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,e
rather 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,ilt.in relational operators
for eC,alit$ an) ineC,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 slice of an arra$ is some s,bstr,"t,re of the arra$. 6n 2)a* onl$ hi&hl$ restri"te) sli"es are
allo:e)B those that "onsist of "onse",tive elements of a sin&le.)imensione) arra$. 6n 7ortran
95* 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$ n
th
"ol,mn in a
ro:.
Problem # 1# Row -a:or 9ersus Column -a:or %rder
!efine ro" ma#or order an) column ma#or order. p. 275-6
There are t:o "ommon :a$s in :hi"h m,lti)imensional arra$s "an be mappe) to one
)imensionB ro: ma;or or)er an) "ol,mn ma;or or)er. 6n row ma:or order* the elements of the
arra$ that have as their 1
st
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 first s,bs"ript* an) so forth. 6n column
ma: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 last
s,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 function for 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. 7or
eDample* this is the a""ess f,n"tion for a t:o.)imensional arra$ store) in ro: ma;or or)er
:here a)1,1* is the base a))ressB
+o,ation( a)i,-* address o. a)1,1* /
((((0 o. ro1s a2o3e i
th
ro1) 4 (si5e o. a ro1))
/ (0 o. e+e6ents +e.t o. the -
th
,o+u6n)) 4 e+e6ent si5e
5 of 9
CS464 Steve Bazinet
Chapter 6 Review Questions 11/19/05
Topic Reference
Problem # 1) ;ava /rray Descriptor
@hat are the reC,ire) entries in a <ava arra$ )es"riptor* an) :hen m,st the$ p. 269
be store) Eat "ompile time or r,n timeFA
6n <ava all arra$s are fiDe) heap.)$nami" arra$s. (n"e "reate)* these arra$s 3eep the same
s,bs"ript ran&es an) stora&e. Se"on)aril$* <ava s,pports ;a&&e) arra$s an) not re"tan&,lar
arra$s. Bein& a fiDe) heap.)$nami" arra$ the entries :ill be establishe) an) fiDe) at r,n time.
2rra$
+lement t$pe 6f m,ltiple.)imensional this :ill be of t$pe 2rra$ ob;e"t.
6n)eD t$pe
in)eD lo:er bo,n)
in)eD ,pper bo,n)
a))ress
This is not sho:n )ire"tl$ in the teDt that 6 "an see. !ould be a problem< 6f 6 &et a "han"e 65ll :rite a pro&ram to
&et the info b$ ,sin& !es"riptor ob;e"t in the ;avaD.mana&ement pa"3a&e.
Problem # 1, C%=%$ Records
@hat is the p,rpose of level n,mbers in C(B(' re"or)s p. 2!4-5
The level n,mbers in C(B(' re"or)s are ,se) to establish a hierar"hi"al str,"t,re of relate)
re"or)s. This is most easil$ ill,strate) :ith an eDample.
%1 7M8L9:77;<7=9<>
%' 7M8L9:77;?@M7.
%5 FI<ST 8I=TA<7 IS B('%).
%5 MI>>L7 8I=TA<7 IS B('%).
%5 L@ST 8I=TA<7 IS B('%).
%' TITL7 8I=TA<7 IS B(#%).
%' C9A<L:;<@T7 8I=TA<7 IS DDEDD.
The hi&hest or lar&est re"or) is the 7M8L9:77 re"or). 6t "onsists of three fiel)s on the %'
levelK the emplo$ee5s name* title* an) ho,rl$ rate. The emplo$ee5s name is f,rther bro3en
)o:n into the three fiel)s on the %5 levelK first name* mi))le* an) last.
Problem # 1. 8ields 6n Records
!efine fully $ualified an) elliptical references to fiel)s in re"or)s. p. 2!6
2 fully >ualified reference to a re"or) fiel) is one in :hi"h all interme)iate re"or) names*
from the lar&est en"losin& re"or) to the spe"ifi" fiel)* are name) in the referen"e. 7or
eDample* 76F+oyee.76F+oyee?a6e.First is a f,ll$ C,alifie) referen"e to an emplo$ee5s
first name.
6n an elliptical reference* the fiel) is name)* b,t an$ of the en"losin& re"or) names "an be
omitte)* as lon& as the res,ltin& referen"e is ,nambi&,o,s in the referen"in& environment.
6 of 9
CS464 Steve Bazinet
Chapter 6 Review Questions 11/19/05
Topic Reference
Problem # 10 *nions
!efine union* free union* an) discriminated union. p. 2!!-9
2 union is a t$pe that ma$ store )ifferent t$pe val,es at )ifferent times ),rin& pro&ram
eDe",tion. 6t5s 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 unions be"a,se pro&rammers are allo:e) "omplete free)om from
t$pe "he"3in& in their ,se.
T$pe "he"3in& of ,nions reC,ires that ea"h ,nion "onstr,"t in"l,)e a t$pe in)i"ator. S,"h an
in)i"ator is "alle) a ta&* or )is"riminant* an) a ,nion :ith a )is"riminant is "alle) a
discriminated 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&B
Sho,l) t$pe "he"3in& be reC,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 *nions
2re 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. 6n one :a$ the ,ser is allo:e) to spe"if$ :hi"h t$pes are
allo: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.
6n 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,se
the 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&B
@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
CS464 Steve Bazinet
Chapter 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 hasn5t a pointer. S,"h
variables are "alle) "arba"e be"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-7
variableA
The main reason for this is pointer arithmeti". 7or eDample* if Ftr is a pointer variable that is
)e"lare) to point at some variable of some )ata t$pe* then Ftr / indeG is a le&al
eDpression. The semanti"s of s,"h an eDpression is as follo:s. 6nstea) of simpl$ a))in& the
val,e of indeG to Ftr* the val,e of indeG is first s"ale) b$ the size of the memor$ "ell Ein
memor$ ,nitsF to :hi"h Ftr is pointin& Eits base t$peF. 7or eDample* if Ftr points to a
memor$ "ell for a t$pe that is fo,r memor$ ,nits in size* then indeG is m,ltiplie) b$ 4* an) the
res,lt is a))e) to Ftr. 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. 6t is ,se) primaril$ for the
formal parameters in f,n"tion )efinitions. 2 C== referen"e t$pe variable is a "onstant pointer
that 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 variable is a "onstant* it m,st be initialize) :ith the a))ress of
some variable in its )efinition* an) after initialization a referen"e t$pe variable "an never be set
to referen"e an$ other variable.
Problem # 2, ;ava @ C# Reference Type
@hat a)vanta&es )o <ava an) C# referen"e t$pe variables have over the pointers p. 3%%
in other lan&,a&esA
The references of ;ava and C# provi)e some of the fleDibilit$ an) the "apabilities of pointers*
:itho,t the hazar)s. 6t remains to be seen :hether pro&rammers :ill be :illin& to tra)e the f,ll
po:er of C an) C== pointers for the &reater safet$ of referen"es.
1 of 9
CS464 Steve Bazinet
Chapter 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 approac or "arba"e collection is :hen re"lamation onl$ o"",rs :hen the list of
available spa"e be"omes empt$. @ith this metho)* the r,n.time s$stem allo"ates stora&e "ells
as reC,este) an) )is"onne"ts pointers from "ells as ne"essar$ :itho,t re&ar) for the stora&e
re"lamation Eallo:in& &arba&e to a"",m,lateF* ,ntil it has allo"ate) all available "ells. 2t this
point* a &arba&e "olle"tion pro"ess is be&,n to &ather all the &arba&e left floatin& aro,n) in the
heap.
The ea"er approac or reference counter metod is in"remental an) is )one :hen
ina""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 are
pointin& at the "ell* an) it has th,s be"ome &arba&e an) "an be ret,rne) to the list of available
spa"e.
Problem # 20 ;ava @ C# Reference /ritmetic
@h$ :o,l)n5t arithmeti" on <ava an) C# referen"es ma3e senseA p. 299
<ava an) C# referen"es refer to "lass instan"es an) not memor$ a))resses. This imme)iatel$
presents arithmeti" on these referen"es from bein& sensible.
9 of 9

Anda mungkin juga menyukai