c++ questions chapter 2(1)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Her

ear
e100quest
ionsbasedont
hepr
ovi
dedt
ext
,wi
thanswer
satt
heend:

*
*Tr
ueorFal
se*
*

1. Funct ionsinC++canonl ybecal l


edoncewi thinapr ogr am.[ True/ False]
2. Passi ngar gument sbyv aluecr eatesacopyoft hedat a,pr ev enti
ngchangeswi t
hinthe
functi
onf rom af fectingt heoriginalargument s.[True/ False]
3. Def aultpar amet ersinC++f uncti
onsar eusedwhennoar gumenti spr ov i
deddur i
ngthe
functi
oncal l.[
Tr ue/ False]
4. Recur sioni sat echniquewher eaf unctioncal l
sadi fferentf unctiont osolveasmal lerversi
on
ofthesamepr obl em.[ True/False]
5. Thebasecasei nr ecursi
oni scrucialtopr ev entinf i
nitel oops.[True/ False]
6. Iterati
v eappr oachesar ealway slessef ficientt hanr ecur siveappr oaches.[ True/False]
7. Memoi zationcanbeusedt oopt i
mi zer ecur sivef unct ionsbyst ori
ngpr eviousl
ycal culat
ed
resul
ts.[True/Fal se]
8. Deepr ecur sioncanl eadt ost ackov erfl
ower r
or s,highl i
ghtingthei mpor t
anceofar eachabl
e
basecase.[ Tr
ue/ Fal se]
9. Whenaf unct i
onneedst omodi fyt heor iginaldat a,passi ngar gument sbyr eferenceis
recommended.[ Tr ue/False]
10.Passi ngar gument sbypoi nterisawayt opassbyv al ue,asi tinv olvespassi ngthememor y
addressoft hev ariable.[True/False]

*
*Chooset
heCor
rectAnswer
**

11.Whi
choft hefol
l
owi
ngi
sNOTabenef
itofusi
ngf
unct
ionsi
nC++pr
ogr
ammi
ng?
a)Modulari
ty
b)Reusabil
it
y
c)I
ncreasedCompl
exi
ty
d)Readabil
it
y

12.Whati
sthepurposeofaf uncti
ondeclarat
ioninC++?
a)Toprovi
dethecompl etefunct
ionbody
b)Toinf
ormthecompi l
eraboutthefunct
ion'
sname,ret
urnt
ype,
andpar
amet
ers
c)Toexecut
et hefuncti
on
d)Todefi
nethef unct
ion'
sdefaul
tpar ameter
s

13.Whatisthedifferencebet weenparametersandar gument si


nC++f unct
ions?
a)Parametersar ev al
uespassedt othefunction,whileargumentsareplacehol
dersinthe
functi
ondefi
nit
ion.
b)Par
amet ersar eplaceholder
sint hefunct
iondef i
nit
ion,whil
eargumentsareactualv
alues
passedtothefunction.
c)Par
amet er
sandar gument sarethesamet hing.
d)Par
amet ersdef i
net hefunct
ion'
sr et
urntype,whil
ear gumentsspeci
fythedatatypeofthe
i
nput.

14.Wheni
sitpreferabletouser ecursi
onov erit
erat
ion?
a)Whendealingwi thlargedatasets
b)Whenrecursiondept hcouldbeani ssue
c)Whenthepr oblem nat ur
all
yfit
sar ecursi
vepatter
n,suchast
reet
rav
ersal
s
d)Whenperformancei sacr i
ti
calfactor

15.Whatistheprimar
yconcernwi t
hdeepr
ecur
sion?
a)Increasedcodereadabi
l
ity
b)Reducedmemor yusage
c)Stackov er
fl
ower r
ors
d)I
mpr
ovedpr
ogr
am ef
fi
ci
ency

*
*Fi
l
lint
heBl
ank*
*

16.Afuncti
oni sasel f
-contai
ned\_\_
\_\_\_ofcodedesignedt operf
orm aspeci f
ictask.
17.\_\_\
_\_\
_br eaksdowncompl expr obl
emsi ntosmal ler
,manageabl et asks,makingcode
easi
ertounder standandmai ntai
n.
18.\_\_\_
\_\_al lowst he same code to be used multipletimes,reducing redundancyand
promoti
ngefficiency.
19.The\_\_\
_\_ \_caseinrecursi
ondefinesthecondit
ionthatstopstherecursivecall
s.
20.Passi
ngar gument sby\ _\
_\_\
_\_all
owsaf uncti
ont omodifytheori
ginaldata.

*
*Mat
chi
ng*
*

*
*Mat
cht
hef
oll
owi
ngt
ermswi
tht
hei
rdef
ini
ti
ons:
**

**Ter
m* *|**Def
ini
ti
on* *
--
--
--
-|--
--
--
--
21.Functi
onDeclarati
on| a)Var
iableslist
edinthefuncti
on'
sdefi
nit
ion.
22.Functi
onDefini
tion|b)Actualvaluespassedt oafuncti
on.
23.Parameters|c)Infor
mst hecompi leraboutafunct
ion'
sname,retur
ntype,andpar
ameter
s.
24.Argument s|
d)Pr ovi
desthebodyofaf uncti
on,i
ncludi
ngthecodethatexecut
esthet
ask.

*
*Mat
cht
hef
oll
owi
ngr
ecur
sioncomponent
swi
tht
hei
rexpl
anat
ions:
**

**Component*
*| **Expl
anat
ion**
--
--
--
-|-
--
--
--
-
25.BaseCase| e)Theconditi
onthatst
opstherecur
sion.
26.Recur
siveCase| f)Thepartoft
hefunct
ionwherethefunct
ioncal
l
sit
sel
f.

*
*Mat
cht
hef
oll
owi
ngar
gumentpassi
ngmet
hodswi
tht
hei
rdescr
ipt
ions:
**

**Method**|*
*Descr
ipti
on**
--
--
--
-|--
--
--
--
27.PassbyValue|g)Changestopar
ameter
swit
hinthefuncti
onaf
fecttheori
ginalar
guments.
28.PassbyRef er
ence|h)Acopyoft hedatai
screated,andmodif
icati
onsinsidethef
uncti
on
don'ti
mpacttheori
ginaldat
a.

*
*Shor
tAnswer
**

29.Br iefl
yexplaintheconceptoff unct
ionsi nC++andpr ovideanexampl eofasi mpl
efuncti
on
thatgr eetstheuser.
30.Whati sthediff
erencebetweendeclari
nganddef i
ningaf uncti
oni nC++?
31.Descr i
bet hetwomai nwayst opassar gumentst oaf unction:passbyv al
ueandpassby
reference.Useexampl est oi
ll
ustr
atethedif f
erence.
32.Expl aintheconceptofr ecur
sion.Provideanexampl eofar ecursi
vefuncti
ontocal
cul
atethe
factorialofanumber .
33.Di scusst headvantagesanddisadvantagesofusingr ecursi
vef unct
ionscomparedt
oiter
ati
ve
solutions.

*
*Tr
ueorFal
se(
Cont
inued)
**

34. C++all
owsfuncti
onstoretur
nv al
ues,enabli
ngt hem t
oproduceresul
tsthatcanbeused
el
sewher
einthepr
ogram.[Tr
ue/Fal
se]
35. Functi
on over
loadi
ng permit
s multi
plef unct
ions wit
ht he same name butdif
fer
ent
parameters,providi
ngf lexi
bil
it
yandcodecl ar
it
y.[
True/False]
36. I nli
nef uncti
ons,suggest edt othecompi l
erf orinli
ning,canenhanceper f
ormanceby
reduci
ngf unctioncallov er
head.[Tr
ue/False]
37. Lambdaexpr essions,intr
oducedinmoder nC++,of ferconcisewayst odefi
neanonymous
functi
ons,oftenusedf orshor t
,speci
fi
ct asks.[Tr
ue/False]
38. Funct ionpoi nters,vari
ablesthatst orememor yaddr essesoff uncti
ons,enabledynami
c
functi
oninvocat i
onatr untime,addi
ngflexibil
i
tytoprogram logic.[
True/
False]

*
*Chooset
heCor
rectAnswer(
Cont
inued)
**

39.Whatisthepr i
maryadv antageofusi
nginl
inefunct
ions?
a)Increasedcodesi ze
b)Potentialperf
ormancei mprovementbyreduci
ngf unct
ioncal
lov
erhead
c)Reducedcoder eadabil
i
t y
d)Increasedmemor yconsumption

40.Whi
chfeatur
einC++al l
owsf
unct
ionswi
tht
hesamenamebutdi
ff
erentpar
amet
ers?
a)Funct
ionoverr
idi
ng
b)Funct
ionoverl
oading
c)Funct
iontemplat
es
d)Funct
ionpoint
ers

41.Whatarelambdaexpressionsi nC++?
a)Namedf uncti
onswi t
hcompl exlogic
b)Conciseanonymousf unctionsof t
enusedf orshor
ttasks
c)Functi
ondeclarat
ionswithoutdef ini
ti
ons
d)Predefi
nedfuncti
onsint hest andardli
brar
y

42.Howdof unctionpointer
scont ri
butet oprogram fl
exibi
li
ty?
a)Theyall
owf orst
aticfuncti
oncal l
satcompi l
et i
me.
b)Theyenforcestr
ictfuncti
onsi gnatures,l
imit
ingadaptabil
i
ty.
c)Theyenabledynami cfuncti
oni nvocationatrunti
me.
d)Theyrestr
ictt
hescopeoff unctions,makingcodel essadapt
abl
e.

43.Whi
choft hef
oll
owi
ngi
sNOTav
ali
dret
urnt
ypef
oraC++f
unct
ion?
a)void
b)int
c)str
ing
d)functi
on

*
*Fi
l
lint
heBl
ank(
Cont
inued)
**

44.Functionscanr etur
n\_\_
\_\_\
_,all
owi
ngthem toproduceresultsthatcanbeusedi nother
partsoftheprogram.
45.\ _
\_\
_\_\_functionshavethesamenamebutdi f
ferentpar
amet er
s,providi
ngflexi
bil
i
tyinhow
functi
onsarecalled.
46.\ _\_
\_\_\_funct i
ons are suggest
ed tothe compilerfori nl
i
ning,pot ent
ial
lyimproving
performance.
47.\_\_\
_\_\_expressionsoff
eraconcisewaytodefi
neanony mousfunctions.
48.\_\_\
_\_\_pointersstor
ethememor yaddr
essesoffuncti
ons,enabli
ngdy namicfunct
ioncall
s.

*
*Mat
chi
ng(
Cont
inued)
**

*
*Mat
cht
hef
oll
owi
ngC++f
unct
ionf
eat
ureswi
tht
hei
rdescr
ipt
ions:
**
**Featur
e* *|*
*Descr ipti
on**
--
--
---|
---
--
---
49.FunctionOv er l
oading|i) Vari
ablesthatst
orememor yaddressesoff
uncti
ons.
50.Inl
ineFunctions| j
)Conciseanonymousf unct
ions.
51.LambdaExpr essi
ons| k)Funct
ionswi t
hthesamenamebutdi f
fer
entpar
ameters.
52.FunctionPoi nters|l)Suggest
edtot hecompil
erforinl
i
ning.

*
*Shor
tAnswer(
Cont
inued)
**

53.Explainther ol
eofr eturnvaluesi nC++f unctions.Provi
deanexampl eofaf unct
ionthat
ret
urnsthesum oft wointegers.
54.Whati sfunct
ionoverl
oading,andhowdoesi timpr ovecodeorgani
zat
ionandreadabil
i
ty?
55.Discusstheconceptofi nl
i
nef uncti
onsandt hei
rpot enti
ali
mpactonprogram per
formance.
56.Brief
lydescri
belambdaexpr essionsinC++andpr ovideanexampleofthei
rusage.
57.Explai
nthepur poseandusageoff unct
ionpointersinC++.

*
*Tr
ueorFal
se(
Cont
inued)
**

58. I nC++,f unct i


onpar amet ersarealway spassedbyv alue,meaningacopyoft hedat ais
made.[ True/False]
59. Whenpassi nganar rayt o af uncti
on,t heent ir
ear r
ayi scopied,leadingto pot
enti
al
performanceov erhead.[True/False]
60. Passi ngar gument sbyr eferencecani mpr oveper formance,especial
lywhenwor kingwith
l
argedat ast ruct
ur es,asitavoidscopy i
ngtheent ir
edat a.[True/Fal
se]
61. Poi nterscanbeusedt opassar raystof unctions,prov i
dingawayt omodi f
ytheorigi
nal
arr
ayel ement s.[True/False]
62. Funct i
ont empl atesinC++al low thecr eati
onofgener icfunct
ionst hatcanwor kwith
di
fferentdat atypes, enhanci
ngcoder eusabi
li
ty.[True/False]

*
*Chooset
heCor
rectAnswer(
Cont
inued)
**

63.Howar earray sty


pical
lypassedt ofunct
ionsinC++?
a)Byvalue, copy
ingtheentir
ear r
ay
b)Byreference,passi
ngapoi ntertothefir
stel
ement
c)Bycreatinganewar r
aywithinthefuncti
on
d)Byusingaspeci alarraydatatype

64.Whatistheprimarybenefi
tofpassingar gument sbyref
erence?
a)Ital
way sreducescodereadabi
lity.
b)Itsi
mpl i
fi
esthefuncti
onsignature.
c)Itavoi
dscopy i
nglargeamountsofdat a,potent
ial
l
yimpr ov
ingper
for
mance.
d)Itprevent
smodi f
icati
onstotheor i
ginaldata.

65.Howcanpoi nt
ersbeusedinconjunct
ionwitharrayswhenpassingthem t
ofunct
ions?
a)Poi
nter
scanbeusedt ocreat
eacopyoft hearr
ay .
b)Poi
nter
scanbeusedt opassthememor yaddressofthefir
star
rayel
ement.
c)Poi
nter
sarenotcompatibl
ewi t
harray
sinf uncti
onarguments.
d)Poi
nter
scanbeusedt oretur
nanewar rayf r
om thefunct
ion.

66.Whati
sthepur poseoffuncti
ont emplatesinC++?
a)Torestr
ictfunct
ionusagetospecif i
cdatatypes
b)Tocreategenericfunct
ionsthatcanwor kwithvar
iousdat
aty
pes
c)Todefi
nef uncti
onsthatonlyacceptar r
aysasarguments
d)Toenforcestri
cttypecheckingatruntime
67.Whi
chofthefoll
owingisav al
idwaytodeclareafunct
iont
empl
atei
nC++?
a)`
templat
e<typenameT>v oi
dmy Functi
on(Targ)
;`
b)`
funct
ion<T>v oidmyFuncti
on(
Tar g);
`
c)`
gener
ic<typenameT>v oi
dmy Funct
ion(Tar
g);`
d)`
voidmyFunction(t
emplateTarg);
`

*
*Fi
l
lint
heBl
ank(
Cont
inued)
**

68.Passingargumentsby\_\
_ \
_\_
\_av oi
dscopyi
ngtheentiredat
a,whichcanbebenefi
cialf
or
perf
ormance.
69.\_\
_\_\_\_canbeusedtopassarraystof
uncti
ons,al
lowingmodif
icati
onoft
heori
ginalarr
ay
el
ement s.
70.Function\_\_
\_\
_\_al
l
owt hecreati
onofgeneri
cfuncti
onsthatcanwor kwi
thdi
ff
erentdata
ty
pes.

*
*Mat
chi
ng(
Cont
inued)
**

*
*Mat
cht
hef
oll
owi
ngconcept
srel
atedt
ofunct
ionsi
nC++wi
tht
hei
rexpl
anat
ions:
**

**Concept **|**
Explanati
on* *
--
--
---|--
---
--
-
71.PassbyRef erence|m)A wayt opassarraystofuncti
ons,enabli
ngmodi f
icat
ionoft he
ori
gi nalarr
ay.
72.Poi nters|
n)Amechani sm tocr
eategener
icf
uncti
onsthatcanhandlevar
iousdatatypes.
73.Funct i
onTempl ates|o)A met hodofpassingargumentswheret hefuncti
onr ecei
vesa
referencetotheoriginal
dat a.

*
*Shor
tAnswer(
Cont
inued)
**

74.Expl ai
n how ar rays are passed t of uncti
ons in C++.Discuss potenti
alper f
ormance
i
mpl i
cati
onsandway st oopti
mi zetheprocess.
75.Compar e and contrastpassi ng ar
gument s byv al
ue and byreference,highl
i
ghting the
advantagesanddisadv antagesofeachappr oach.
76.Descr i
betheroleofpoi ntersinpassingar r
aystofuncti
ons.Provi
deanexampl etoil
lust
rate
thei
rusage.
77.Whatar efunct
iont emplates,andhowdot heyenhancecodereusabil
i
tyinC++?

*
*Tr
ueorFal
se(
Cont
inued)
**

78.Recursionisalway sthemostef fi
cientwaytosolveproblemst hatcanbebr okendowni nt
o
small
ersubpr obl
ems.[ Tr
ue/ False]
79.Arecursivefuncti
onmusthav eabasecaset oprev
entinf
init
erecursion.[True/Fal
se]
80.St
ackov er
fl
owcanoccurwhenar ecursi
vefunct
ionhastoomanynest edcal l
s.[True/False]
81.It
erat
ivesol uti
onsusi ngl oopscansomet imesbemor eef fi
cientthanr ecursiv
esol utions.
[Tr
ue/Fal
se]
82.Memoi zati
onisat echniquet hatcanbeusedt oopti
mizerecursi
v efunct
ions.[True/False]

*
*Chooset
heCor
rectAnswer(
Cont
inued)
**

83.Whati
sacr i
ti
calcomponentofar
ecur
siv
efunct
iont
opr
eventi
nfi
nit
eloops?
a)Arecur
sivecase
b)Abasecase
c)Afunct
ioncall
d)Aloop
84.Whatisapotential
issuewi thdeepl
ynest
edr
ecur
siv
ecal
l
s?
a)Improvedcoder eadabili
ty
b)Reducedmemor yconsumption
c)Stackoverf
low
d)Enhancedpr ogram eff
ici
ency

85.Whi
cht echniquecanopti
mizer
ecur
siv
efunct
ionsbyst
ori
ngpr
evi
ousl
ycal
cul
atedr
esul
ts?
a)I
teration
b)Memoi zati
on
c)Tai
l recursi
on
d)Dynami cprogr
amming

86.Whenmightanit
erati
vesoluti
onbepr efer
abletoarecursi
vesol
uti
on?
a)Whentheproblem str
uctur
enat ur
all
ylendsitsel
ftor
ecursi
on
b)Whenmemor yusagei sacrit
icalconcern
c)Whentherecursi
vedepthisli
kelytobev erydeep
d)Whentheproblem canbeeasi l
ysolvedusingaloop

87.Whi
choft hef ol
lowingisNOTacommonappl
i
cat
ionofr
ecur
sion?
a)Treetraversals
b)Sort
ingal gori
thms
c)Factor
ialcalculati
on
d)Simplearithmet i
coperat
ions

*
*Fi
l
lint
heBl
ank(
Cont
inued)
**

88.A\ _
\_\_\
_ \
_caseinar ecur
siv
ef unct
iondefi
nest hecondi
ti
ont hatstopstherecur
siv
ecall
s.
89.\_\_
\_\_\
_ov er
fl
owcanhappeni farecur
siv
ef uncti
ongoest oodeep.
90.\_\_\_
\_\_soluti
ons,oftenusingloops,canbemor eefficientthanrecursi
vesoluti
onsfor
cert
ainproblems.

*
*Mat
chi
ng(
Cont
inued)
**

*
*Mat
cht
hef
oll
owi
ngr
ecur
sion-
rel
atedt
ermswi
tht
hei
rdef
ini
ti
ons:
**

**Ter
m* *|**Defini
ti
on**
--
--
--
-|--
--
--
--
91.BaseCase| p)Apotenti
alerrorcausedbyexcessiverecur
siv
ecall
s.
92.StackOv er
fl
ow| q)Atechni
quet oopti
mizerecursi
onbyst or
ingr
esul
ts.
93.Memoi zati
on| r)Thecondit
ionthathalt
stherecursi
on.

*
*Shor
tAnswer(
Cont
inued)
**

94.Descr i
bet hekeycomponent sofar ecursi
vefuncti
on,includi
ngt hebasecaseandt he
recur
sivecase.
95.Explai
nhowst ackoverf
lowcanoccurint hecont
extofrecursi
vefunctions.
96.Discusstheconceptofmemoi zat
ionandi t
srol
einopti
mi zi
ngrecursiv
ef unct
ions.
97.Compar eandcontrastr
ecur
sionanditerati
onasapproachestosolvingpr obl
ems.
98.Wheni sitappr
opri
atetouserecursi
on,andwhenar eit
erati
vesolut
ionsabet terchoi
ce?

99.Whatist
heout
putoft
hef
oll
owi
ngcodesni
ppet
?
``
`c++
#incl
ude<i
ost
ream>

i
ntf
ibonacci
(i
ntn){
i
f(n<=1){
r
etur
nn;
}el
se{
r
etur
nfi
bonacci
(n-1)+f
ibonacci
(n-2)
;
}
}

i
ntmai n(){
i
ntposi t
ion=6;
std::
cout<<fi
bonacci
(posi
ti
on)<<st
d::
endl
;
retur
n0;
}
`
``

100.Pr
ovi
deaC++codesni ppetdemonst
rat
ingar
ecur
siv
efunct
iont
hatcal
cul
atest
hef
act
ori
al
ofagiv
ennon-
negat
ivei
nteger.

*
*Answer
s**

1.**False* *.Funct ionscanbecal l


edmul tiplet i
mes.
2.**True* *.Passbyv aluemakesacopy ,isolatingchangeswi thinthef unct ion.
3.**True* *.Def aultpar amet er sar eusedi fnocor respondi ngar gumenti sgi ven.
4.**False* *.Recur si oni nvolv esaf unct ioncal li
ngi tself.
5.**True* *.Thebasecasei sessent ialtost opr ecur sionandav oidinf i
nitel oops.
6.**False* *.Iterativeappr oachesar eof tenmor eef ficient ,especi all
ywi thlar gedat asets.
7.**True* *.Memoi zat i
onst oresr esul tstoav oidr edundantcal culati
onsi nr ecur sion.
8.**True* *.Deepr ecur si oncanl eadt ost ackov erflowi fthebasecasei snotr eached.
9.**True* *.Passbyr eferenceal l
owsmodi fi
cat i
onst ot heor i
ginal dat a.
10.**Fal se* *.Passi ngbypoi nteri saf or m ofpassbyr efer ence, asitusest hememor yaddr ess.
11.**c)I ncr easedCompl exity *
* .Funct i
onsact uallyr educecompl exity.
12.**b)Toi nformt hecompi leraboutt hef unction'sname, returnt ype, andpar amet er
s** .
13.**b)Par amet ersar epl acehol der si nt hef unct i
ondef inition, whi l
ear gument sareact ualval
ues
passedt ot hef unction.* *
14.**c)Whent hepr obl em nat ural l
yf it
sar ecursivepat ter n, suchast reet r
av ersals**.
15.**c)St ackov er f
lower r
or s**.Deepr ecur sioncanexhaustt hecal l stack.
16.**block* *
17.**Modul ari
ty**
18.**Reusabi l
it
y* *
19.**base* *
20.**reference* *
21.**c)I nfor mst hecompi leraboutaf unct ion'sname, ret ur nty pe,andpar amet ers**
22.**d)Pr ov i
dest hebodyofaf unct ion, includingt hecodet hatexecut est het ask**
23.**a)Var iablesl i
st edi nthef unct ion'sdef init
ion* *
24.**b)Act ual valuespassedt oaf unct ion* *
25.**e)Thecondi ti
ont hatst opst her ecur sion* *
26.**f)Thepar toft hef unctionwher et hef unct ioncal lsi tsel f
* *
27.* *
h)Acopyoft hedat ai scr eated,andmodi fi
cat ionsi nsi det hef unct iondon' timpactt he
ori
ginal dat a**
28.**g)Changest opar amet erswi thint hef unctionaf fectt heor i
ginal argument s**
29.Funct ionsi nC++ar ebl ocksofcodedesi gnedt oper for m speci fi
ct asks.Theypr omote
modul arity,reusabi li
ty ,
mai ntainabi l
i
ty ,andr eadabi lit
y .

``
`c++
#incl
ude<i
ost
ream>
v
oidgr
eetUser(
){
st
d::
cout<<"Hel
l
o,user
!"<<st
d::
endl
;
}

intmai n(){
greet User();
return0;
}
```
30.Decl ari
ng af uncti
on in C++ i nvolvesspecifyi
ng i t
sname,r eturntype,and parameters,
i
nformi ng t he compi ler about t he f unct
ion'
s exi stence without providi
ng the act ual
i
mpl ement ation.Def i
ningaf unctioninv ol
vesprovidi
ngt hecompl et
ef unct
ionbodywi ththecode
toexecut et het ask.
31.**PassbyVal ue:**Acopyoft hear gumenti spassedt othefuncti
on.Modi f
icat
ionswithi
nthe
functiondon' taf f
ectt heorigi
nal dat
a.
** PassbyRef erence:**Ar eferencet otheor i
ginaldataispassed.Modi fi
cati
onswi thi
nthe
functionchanget heor i
ginaldat
a.

`
``
c++
/
/PassbyValue
v
oidmodify
Value(
intnum){
num =10;
}

/
/PassbyReference
v
oidmodify
Reference(
int
&num){
num =10;
}

i
ntmain(
){
i
nta=5;

modi
fyVal
ue(a);
//ar emai
ns5
modi
fyRef
erence(a)
;//abecomes10

ret
urn0;
}
`
``
32.Recursi
onisatechni
quewhereaf uncti
oncall
sitsel
ftosol
vesmal
l
eri
nst
ancesoft
hesame
probl
em unti
labasecaseisr
eached,
pr ev
entingi
nfi
nitel
oops.

```c++
longl ongf actori
al(
intn){
if(n==0){
r
eturn1;
}else{
r
eturnn*f actori
al(
n-1);
}
}
```
33.** Adv antagesofRecur sion:*
*Elegantsoluti
onsforprobl
emswithrecur
siv
enatur
e(e.
g.,t
ree
trav
er sals),oft
enl eadi
ngt omor ereadablecode.
** DisadvantagesofRecur sion:
**Canbel esseff
ici
entduetorepeatedcal
cul
ati
onsand
functioncal loverhead,potenti
alforstackoverf
lowerror
swithdeepr
ecursi
on.
**Adv ant agesofI terat i
on:**Gener allymor eef f
icient,especial
lywithlargedataset s,av oids
ther iskofst ackov erf
low.
**Di sadv ant agesofI teration:*
*Canl eadt olessr eadablecodef orproblemst hatnat urall
yf i
t
recur sion.
34.* *Tr ue* *.Funct i
onscanr eturnvaluesofv ari
ousty pes.
35.* *True* *.Funct i
onov erl
oadi ngal l
owsmul t
ipl
ef unctionswi ththesamenamebutdi fferent
par amet ers.
36.* *True* *.I nli
nef unctionscanpot entiall
yi mprov eper f
ormancebyel iminati
ngf unctioncal l
over head.
37.* *True* *.Lambdaexpr essionspr ov i
deaconci sewayt odefineanony mousf uncti
ons,of ten
usedf orshor tt asks.
38.* *True* * .Funct i
onpoi ntersenabl edy nami cfunct i
oncal lsatrunti
me,i ncreasingpr ogr am
fl
exi bili
ty.
39.* *b)Pot ent ialperf
or mancei mpr ovementbyr educingf uncti
oncalloverhead**.
40.* *b)Funct ionov erl
oadi ng* *.
41.* *b)Conci seanony mousf unctionsof tenusedf orshor ttasks**.
42.* *c)Theyenabl edy nami cf unctioninvocat i
onatr untime**.
43.* *d)f unct i
on* *
.Whi l
ef unct i
onscanr eturnpoint erstof uncti
ons,"functi
on"itselfisnota
directr etur nt ypeinC++.
44.* *v alues* *
45.* *Ov er l
oaded* *
46.* *Inline* *
47.* *Lambda* *
48.* *Funct i
on* *
49.* *k)Funct ionswi t
ht hesamenamebutdi ff
erentpar ameters**
50.* *l)Suggest edt othecompi l
erforinli
ning* *
51.* *j)Conci seanony mousf unct i
ons* *
52.* *i)Var i
abl esthatst orememor yaddressesoff unct i
ons**
53.Ret ur nv aluesal l
owf unct ionstosendr esultsbackt othecalli
ngcode.

```c++
intsum( inta,intb){
returna+b;
}
```
54.Funct i
onov erl
oadingi nv ol
vesdefini
ngmul ti
plefunctionswi t
ht hesamenamebutdi ff
erent
paramet erl ists.Thisi mprov escodeor ganizat
ionbygr oupingr el
atedf unct
ionsandenhances
readabi l
it
ybyal l
owi ngtheuseofasi nglefuncti
onnamef orv ari
ousinputt y
pes.
55.Inlinef unct i
onsar easuggest iontothecompi l
ertor eplacethef uncti
oncallwit
htheactual
functioncode,pot entiall
yr educingfuncti
oncallover
headandi mprovingperf
ormance.Howev er
,
excessi veuseofi nli
nef unctionscanincreasecodesize.
56.Lambdaexpr essionsof f
eraconci sewayt odefineanony mousf uncti
onsdirect
lywi
thi
nt he
code.

``
`c++
autosum =[ ](
inta,intb){r et
ur na+b;};
i
ntr esult=sum( 5,3);
``
`
57.Funct i
onpoi ntersst oret hememor yaddressesoff uncti
ons.Theyenabl ethepassi
ngof
functi
onsasar gument st ootherf uncti
onsanddy namicfuncti
oninvocati
onatr unt
ime.
58.**False** .Whil
epassbyv alueiscommon, C++al sosupportspassbyr efer
ence.
59.**Fal se**.Whenpassi nganar raytoaf uncti
on,typi
call
yapoi ntert othefirstel
ementi
s
passed,nott heentir
ear ray.
60.**True* *.Passbyr eferenceav oidscopyi
nglargedatastructur
es,improvingperfor
mance.
61.**True* *.Point
erscanbeusedt opassarr
aysandmodi f
ytheorigi
nalar r
ayelements.
62.* *True* *.Funct i
ont emplat esenabl et hecr eati
onofgener icf unct i
onst hatcanwor kwith
di
fferentdat at y
pes.
63.**b)Byr efer ence,passingapoi ntert ot hefirstelement **.
64.**c)I tav oi dscopy i
nglargeamount sofdat a, potential
lyimpr ovi
ngper f
ormance* * .
65.**b)Poi nt erscanbeusedt opasst hememor yaddr essofthef i
rstar r
ayelement **.
66.**b)Tocr eat egener icfunctionst hatcanwor kwi thvari
ousdat at ypes* *.
67.**a)` templ at e<typenameT>v oidmy Function( Targ);`
**.
68.**refer ence* *
69.**Poi nter s* *
70.**templ ates* *
71.**o)Amet hodofpassi ngar gument swher et hef uncti
onr eceivesar eferencet ot heorigi
nal
data**
72.**m)Awayt opassar r
ayst of unct ions, enabl ingmodi fi
cationoft heor igi
nal array**
73.**n)Amechani sm tocreategener i
cf uncti
onst hatcanhandl evar iousdat at ypes* *
74.Whenpassi nganar raytoaf unct ioni nC++,apoi ntertothef ir
stel ementi st ypicall
ypassed.
Thisav oidscopy i
ngt heentirear ray ,
impr ovi
ngef fi
ciency.Howev er
, itallowsmodi ficationstothe
ori
ginal arraywi thi nt
hef uncti
on.
75.**PassbyVal ue:**
* * *Adv ant ages:*
*Pr otectsor igi nal datafr om modi fi
cationswi thinthef unction.
* * *Di sadv ant
ages:**Inef f
icientf orlargedat astructur
esduet ocopy i
ng.

*
* PassbyRef er
ence:**
* * *Advantages:*
*Ef f
icientf
orlar
gedat astructures,all
owsmodi ficati
onstotheori
ginal
data.
* * *
Disadvantages:*
*Ri skofuni
ntent
ionalmodi f
icat i
onsifnothandledcareful
l
y.
76.Pointer
scanhol dt hememor yaddr
essoft hefi
rstelementofanar ray.Passingapoi
ntert
oa
funct
ionallowsthef unct
iontoaccessandmodi f
ytheor iginalar
rayelement s.

``
`c++
voidmodi f
y Array(int *arr,i
ntsize){
for(inti =0; i <si ze;++i){
arr[i
]*=2;
}
}
``
`
77.Funct i
ont empl at esal l
owt hecr eat ionofgener i
cf unctionst hatcanoper at
eonv ari
ousdata
typeswi t
houtt heneedt owr i
tesepar at efunctionsf oreacht ype.Theyuset emplateparamet
ers
torepresentgener i
ct y pes, promot i
ngcoder eusabi l
ity.
78.**False**.Whi ler ecur sioncanbeel egant,it
er ativesol utionsareoftenmor eeff
ici
ent
.
79.**True**.Abasecasei sessent ialtopr eventi nfinit
er ecur si
on.
80.**True**.St ackov erf l
owcanoccurduet oexcessi verecur siv
ecalls.
81.**True**.Iterati
v esol uti
onscanbemor eef ficientinsomecases.
82.**True**.Memoi zat ionopt i
mi zesr ecursi
onbyst oringandr eusingresul
ts.
83.**b)Abasecase* *.
84.**c)Stackov erflow* *.
85.**b)Memoi zat i
on* * .
86.**c)Whent her ecur sivedept hislikelytobev erydeep* *
.
87.**d)Simpl ear it
hmet icoperations* *.
88.**base* *
89.**Stack**
90.**It
erativ
e* *
91.**r)Thecondi ti
ont hathal tsther ecur si
on**
92.**p)Apot entialer r
orcausedbyexcessi verecur sivecal ls**
93.**q)At echni quet oopt i
mizer ecursionbyst or i
ngr esults* *
94.Ar ecursivef unctionhast womai ncomponent s:
* * *BaseCase: **Thecondi tiont hatst opst her ecur sion.
* * *Recur siveCase: **Thepar twher et hef unctioncal l
sitselfwi thamodi fi
edi nputto
approacht hebasecase.
95.Stackov er f
lowoccur swhenar ecur sivef unct ionmakest oomanynest edcal l
s,exceedingthe
avail
ablememor yforthecal lst ack.Eachf unctioncal laddsaf ramet ot hest ack,andi fthe
recur
siongoest oodeep, thest ackov erflows.
96.Memoi zat i
oni sanopt i
mi zationt echni quef orr ecur sivef uncti
ons.I tinv ol
vesst oringthe
resul
tsofexpensi v
efunct i
oncal lsandr eturningt hecachedr esulti
fthesamei nputsoccuragai n.
Thisavoidsr edundantcal culationsandspeedsupt hefunct i
on.
97.**Recur sion:**Elegantf orpr obl emswi thr ecur sivest r
ucture,canbel essef fi
cientduet o
functi
oncal lover headandpot ential stackov erfl
ow.
**I
terati
on:* *Gener all
ymor eef fici
ent ,especi al l
yf orl argedat aset s,avoidsst ackov erf
low
butmayl eadt ol essreadabl ecodef orr ecur sivepr oblems.
98.User ecursionwhen:
* Thepr obl em naturall
yf i
tsar ecur sivest ruct ure(e.g., t
reetraversals).
* Coder eadabil
ityisapr i
ority,andt her ecur siondept hismanageabl e.

Useiterativ
esoluti
onswhen:
* Per formanceiscri
tical
,andthepr
obl
em canbeef fi
cientl
ysolv
edi t
erat
ivel
y.
* Ther ecur
sivedepthisl
ikel
ytobever
ylar
ge, r
iski
ngst ackover
flow.
99.Theout putis`8`.Thecodecalcul
atest
he6thFibonaccinumber ,
whichis8.
100.``
`c++
#i
nclude<i ost
ream>

l
onglongf act
ori
al(
intn){
i
f(n==0){
retur
n1;
}else{
retur
nn*f actor
ial
(n-1)
;
}
}

i
ntmain(){
i
ntnum =5;
std::
cout<<"
Fact
ori
alof"<<num <<"i
s:"<<f
act
ori
al(
num)<<st
d::
endl
;
retur
n0;
}
`
``

You might also like