Vous êtes sur la page 1sur 18

COMP102: Computer and Programming Fundamentals

FINAL EXAMINATION

T!"ENT NAME: ##########################
T!"ENT N!M$E%: ##########################
"EPA%TMENT: ##########################
LA$& ECTION: ##########################
NAME OF TA 'i( )no*n+: ##########################
Instru,tions to tudents
1& C-e,) t-at .ou -a/e all 10 pages&
2& 1rite .our name2 student num3er2 department2 la3& session
num3er2 and TA4s name on t-is page&
0& Ans*er ALL 5uestions in t-e spa,e pro/ided&
For !se 3. 6rader
7uestion 1 ######################82
7uestion 2 ######################82
7uestion 0 ######################82
7uestion 9 ######################82
7uestion : ######################82
7uestion ; ######################810
7uestion < ######################89
7uestion = ######################82
7uestion > ######################8:
7uestion 10 ######################8;
7uestion 11 ######################8:
7uestion 12 ######################8=
7uestion 10 ######################8:
7uestion 19 ######################89
7uestion 1: ######################8:
7uestion 1; ######################8=
7uestion 1< ######################8>
7uestion 1= ######################8;
7uestion 1> ######################810
7uestion 20 ######################89
TOTAL: ######################8100
1+ '2 mar)s+ 1-at is t-e /alue o( n?
int n @ 4>4 A 4A4 B 4F4 A 4<4 B ;C
AN1E%:
10
2+ '2 mar)s+ 1-at is *rong *it- t-e (ollo*ing ,ode?
int AD0ED2E @ FF220G2 F921G2 F120GGC
int $D2ED0E @ FF920G2 F122G2 F:22GGC
AN1E%:
0+ '2 mar)s+ 1-at is t-e output to t-e s,reen?
int n @ 0C
i( 'n @ 0+
n @ 0C
else n @ 9C
,out HH nC
AN1E%:
9
9+ '2 mar)s+ 1-at is t-e output to t-e s,reen?
int i@9C
int n@:C
int main'+
F
i( 'iI0+
F
int i@0C
n @ iC
G
,out HH nC
return 0C G
AN1E%:
0
:+ '2 mar)s+ 1-at is t-e output to t-e s,reen?
int i@<C
int ('int JK+
F
K @ iC
return KC
G
int main'+
F
int i@22nC
n @ ('n+BiC
,out HH nC
return 0C G
AN1E%:
>
;+ '10 mar)s+ T-e (ollo*ing program ,an 3e ,ompiled and eLe,uted
,orre,tl.&
Line no& our,e ,ode
1 Min,lude Hiostream&-I
2 int )C
0 int sear,-'int dataDE2 int lengt-+ F
9 int maL @ A1C
: (or 'int K@0 C KH lengt-C KBB+
; i( ' dataDKE I maL+
< maL @ dataDKEC
= return maLC G
> int main'+F
10 int arra.D10E @
F022292:2:2;2<2<2=2>0GC
11 ,out HH sear,-'arra.210+ HH endlC
12 return 0C G
For ea,- o( t-e (ollo*ing : /aria3les2 *rite do*n its s,ope 3.
stating t-e 3eginning and t-e ending line num3ers
ELample: T-e s,ope o( /aria3le NmaLN is (rom line 9A=&
i+ )
2 A 12
ii+ K
: A =
iii+ lengt-
0 A =
i/+ arra.
10 A 12
/+ sear,-
0 A 12
<+ '9 mar)s+ omeone t.pes in t-e (ollo*ing s-ort program&
Min,lude Hiostream&-I
int main'+ F
return 0C
G
T-e ,omplete ,ode is s-o*n a3o/e 'Kust (our lines+& T-e person does
not t.pe an.more ,ode&
7uestion <'a+: 1ill t-e program ,ompile *it- no *arnings8errors o(
an. )ind?
Ans*er :
Oes
No
7uestion <'3+ : 1ill t-e program eLe,ute *it- no *arnings8errors o(
an. )ind?
Ans*er :
Oes
No
=+ '2 mar)s+ !sing "eMorgans rule2 repla,e t-e (ollo*ing line o( ,ode
*it- a single line o( ,ode *-i,- is e5ui/alent& "o not use an. 44i(44
statement&
ans*er@P' s*it,-1 QQ s*it,-2 +C
Oou ,annot use QQ an.*-ere in .our ans*er&
AN1E%:
>+': mar)s+ Assume n is a positi/e integer& T-e (ollo*ing (un,tion
prints t-e num3er 1 2 0 9 :&&&&n&
/oid printnum'int n+
F
(or 'int i@1C iH@nC iBB+
,out HH i HH endlC
G
1rite a %EC!%IRE (un,tion *-i,- does t-e same Ko3& Oou are NOT
allo*ed to de(ine ANO LOCAL O% 6LO$AL RA%IA$LE& NO N*-ileN2 NdoA
*-ileN or N(orN statements are allo*ed& Onl. one Ni(N statement is
allo*ed in .our (un,tion& No mar) *ill 3e gi/en i( .our (un,tion
,ontains an. /aria3le8parameter ot-er t-an a single
/aria3le8parameter *-i,- must 3e named as NnN&
Sere is t-e s-ell o( .our (un,tion:
/oid printnum'int n+ F88re,ursi/e approa,-
G
10+ '; mar)s+ 1-at is t-e output to t-e s,reen i(
"fl(4)"
is
,alled?
#include <iostream.h>
void f1(int n);
void f2(int n);
int main()
{
f1(4);
return 0;
}
void f1(int n)
{
cout << n << #;
if (n>0)
f2(n!2);
}
void f2(int n)
{
cout << n << #;
if (n>0)
f1(n"1);
}
1rite do*n t-e output in t-e (ollo*ing 3oLes:
11 + ': mar)s+1-at is t-e output?
Min,lude Hiostream&-I
/oid s*ap'int JL2 int J.+
F
int tempC
temp @ LC
L @ .C
. @ tempC
G
int main'+
F
int iC
int AD10E @ F0212220292:2;2<2=2>GC
(or 'i@0 C iH2 C iBB+
s*ap'AD'2Ti+U10E2 AD'0Ti+U10E+C
(or 'i@0 C iH10 C iBB+
,out HH ADiE HH 4M4C
return 0C
G
1rite do*n t-e output in t-e (ollo*ing 3oLes:
AN1E%:
12 + '= mar)s+ For ea,- o( t-e (ollo*ing programs '10'a+ to 10'd++
*rite OE neLt to it i(
1& t-e program su,,ess(ull. ,ompiles *it-out an. *arnings or errors2
AN"
2 & *-en eLe,uted2 t-e program results in t-e message N.esN 3eing
s-o*n on t-e s,reen&
I( one or 3ot- o( t-e a3o/e t*o ,onditions are not true (or t-at
program2 *rite NO neLt to it& T-ere is NO need to *rite do*n an.
eLplanation& Vust *rite do*n 44.es44 or 44no44 neLt to ea,- program&
7uestion 12'a+
Min,lude Hiostream&-I
int main'+
F
int t-e#num3er@:0C
i( ':0@@t-e#num3er+ ,out HH N.esNC
return 0C
G
ans*er:
.es

7uestion 12'3+
Min,lude Hiostream&-I
Min,lude Hstring&-I
Mde(ine aut-or#name N"a/eN
int main'+
F
,-ar m.#nameDE@N"a/eNC
i( 'str,mp'm.#name2aut-or#name+@@0+
,out HH N.esNC
return 0C
G
ans*er:
.es
7uestion 12',+

Min,lude Hiostream&-I
Min,lude Hstring&-I
int main'+
F
,-ar m.#nameDE@N"a/eNC
i( 'strlen'm.#name+@@9+
i( 'm.#nameD0E@@4"4+
i( 'm.#nameD1E@@4a4+
i( 'm.#nameD2E@@4/4+
i( 'm.#nameD0E@@4e4+
,out HH N.esNC
return 0C
G
ans*er:
.es
7uestion 12'd+

Min,lude Hiostream&-I
int main'+
F
int num@int'404+C
int num2@int'4>4+AnumC
i( ',-ar'num2+@@4>4+
,out HH N.esNC
return 0C
G
ans*er:
No

13) (5 marks) What is the output?
#include <iostream.h>
void f2();
void f1(int a)
{

f2(a"1);
}
void f2(int #)
{

f1(#!1);
}
int main()
{
int n $ %;
f1(n);
cout << n;
return 0;
}
1rite do*n t-e output in t-e (ollo*ing 3oLes:


19+ '0 mar)s+ T-e (ollo*ing ,ode prints a set o( integers in t*o
,olumns& Modi(. t-e (ollo*ing ,ode so t-at it prints t-e integers in
t-ree ,olumns& Oou are onl. allo*ed to modi(. a single line o( ,ode&
Min,lude Hiostream&-I
int main'+
F
int numD12E @ F02 12 22 02 92 :2 ;2 <2 =2 >2 102 11GC
int iC
(or 'i@0 C iH12 C iBB+ F
,out HH numDiEC
i(''1Bi+U2+ ,out HH N N
else ,out HH endlC
G
return 0CG
AN1E%:
1:+ ': mar)s+1rite a (un,tion Ntranspose'+N to transpose a 0 W 0
matriL 3. inserting a single statement in t-e nested (orAloops& T-e
(un,tion ,al,ulates t-e transpose o( t-e matriL m1 and stores t-e
/alues into t-e ne* matriL m2&MatriL m1 is unaltered a(ter t-e
eLe,ution&
MatriL transpose:
0 1 2 0 0 ;
0 9 : AI 1 9 <
; < = 2 : =
/oid transpose'int m1D0ED0E2 int m2D0ED0E+
F
(or 'i@0 C iH0 C iBB+
(or 'K@0 C KH0C KBB+
88 Insert one statement -ere
G
AN1E%:
1;+ '= mar)s+ Loo) ,are(ull. at t-is program&
Min,lude Hiostream&-I
Min,lude Hmat-&-I
Min,lude Hstring&-I
int (un,tion1'+
F
int ans*er@;C
return''int+'s5rt'1&:T',eil'(loor'ans*erB0&:++++++C
G
int (un,tion2'+
F
int XXXXXYYY@0C
int YYYYYXXX@0C
3ool (inis-ed@(alseC
do
F
i( 'YYYYYXXX@@XXXXXYYY+
(inis-ed@trueC
else
YYYYYXXXBBC
G
*-ile 'P(inis-ed+C
return''int+YYYYYXXX+C
G
int (un,tion0'+
F
int L#LL#XXX@0C
(or 'int L#L#XXX@2CL#L#XXXIA2CL#L#XXXAA+
L#LL#XXXB@L#L#XXXC
return'L#LL#XXX+C
G
int (un,tion9'+
F
,-ar (unDE@N1o***eePNC
int le@2C
int ,ount@0C
,-ar test@4e4C
i( 'testH@4e4+ test@4*4 C
le@ strlen'(un+C
(or 'int -#i@leA1C-#iI@0C-#iAA+
i( '(unD-#iE@@test+ ,ountBBC
return ''int+ ,ount+C
G
int main'+
F
,out HH (un,tion1'+ HH 4M4C
,out HH (un,tion2'+ HH 4M4C
,out HH (un,tion0'+ HH 4M4C
,out HH (un,tion9'+ HH 4M4C
return 0C
G
1-en it is ,ompiled2 no errors or *arnings are gi/en& 1-at is t-e
output to t-e s,reen *-en t-e a3o/e program is eLe,uted? 1rite do*n
t-e output in t-e (ollo*ing 3oLes:
AN1E%:
1<+ '> mar)s+ A NpalindromeN is t-e name gi/en to a *ord *-i,- -as
t-e same spelling *-en t-e letters are re/ersed& Sere are eLamples
o( *ords *-i,- are palindromes 'some o( t-ese are not a,tuall. proper
Englis- *ords2 3ut t-at is not important+:
a e ee eee a33a oLo ooLLoo lido((ada((odil
Sere is an eLample o( *ord *-i,- is not a palindrome:
-i 't-is *ord spelt 3a,)*ards is Ni-N2 *-i,- is not t-e same as N-iN+
omeone -as made a program *-i,- *ill let t-e user t.pe in a *ord&
T-e ,omputer program *ill t-en tell t-e user *-et-er or not t-e *ord
is a palindrome& T-e program is s-o*n 3elo*& It uses a re,ursi/e
(un,tion& T-e program ,ompiles *it- no errors and no *arnings& T-e
main part o( t-e program uses an in(inite loop so t-at t-e user ,an
repeatedl. t.pe *ords& Ea,- time a *ord is entered2 t-e program sa.s
*-et-er or not it is a palindrome& T-ere are t-ree pla,es *-ere t-e
teLt is missing& Fill in t-e 3lan)s to ,omplete t-e (ollo*ing
program& Sere is t-e program listing:
Min,lude Hiostream&-I
Min,lude Hstring&-I
3ool palindrome'
,-ar teLtDE
2
int start
2
int end
+
F
i( ''endAstart+ H@0+
return'true+C
else
i( 'teLtDstartE P@ teLtDendE + return (alseC
return 'palindrome'teLt2 startB12 end A1+C
G
int main'+
F
,-ar testD200EC
int (irst#pos2 last#posC
*-ile 'true+
F
,in II testC (irst#pos @ 0C

last#pos@strlen'test+A1
,out HH NT-e *ord N HH test HH N is NC
i( 'palindrome'test2 (irst#pos2 last#pos+ @@ (alse+
,out HH NnotNC
,out HH N a palindromeN HH endlC
G
return 0C
G
1=+ '; mar)s+ T-e (ollo*ing program reads a (ile t-at ,ontains
,-ara,ters& T-e program ,opies e/er. ,-ara,ter (rom t-e input (ile
'Ninput&datN+ to t-e output (ile 'Noutput&datN+ eL,ept t-e ,ommas& It
remo/e an. ,ommas& Parts o( t-e ,ode 3elo* is le(t 3lan)& Fill in t-e
3lan)s in order to ,omplete t-e program&
Min,lude H(stream&-I
/oid main'+
F
,-ar ,C 88 Open t-e (iles (or reading and *riting
respe,ti/el.
88 FILL IN TSE CO"E SE%E 'at most 9 statements+
88 Loo) (or ,ommas and *rite t-e data to t-e ne* (ile
88 "ON4T CSAN6E TSE CO"E INI"E doA*-ile LOOP
do
F
old(ile&get',+C
i( ', P@ 424+
ne*(ile&put',+C
G *-ile 'old(ile&eo('++C
88 Close (iles
88 FILL IN TSE CO"E SE%E 'at most 2 statements+
G
1>+ '10 mar)s+ 1e -a/e de,ided to modi(. t-e de,larations o(
Assignment 0 to use a stru,ture (or t-e student re,ords& T-e -eader
(ile ,ontains t-e (ollo*ing:
,onst int ,lass#siYe@:0C 88 maLimum ,lass siYe
,onst int name#siYe@21C 88 maLimum lengt- o( nameC s-ould -a/e 3een
20
,onst int id#siYe@>C 88 maLimum lengt- o( id
,onst int no#grades@19C 88 maLimum num3er o( grade ,olumns
stru,t tudent%e,ord F 88 student re,ord
dou3le totalgradeC
,-ar nameDname#siYeE2 idDid#siYeEC
int gradeDno#gradesEC
GC
tudent%e,ord studentD,lass#siYeEC
dou3le *eig-tDno#gradesEC
int no#lines2 ,olumn2 sa/e@1C
Assuming studentDE -as 3een sorted a,,ording to student&idDE is
as,ending order2 *e *ould li)e to use t-e 3inar. sear,- met-od to
sear,- t-e data3ase& 1e t-ere(ore need to *rite t-e (un,tion to 3e
used 3. ,-angeg'+C
int 3sear,-str'tudent%e,ord studentDE2 int siYe2 ,-ar sear,-idDE+C
*-ere siYe is t-e num3er o( /alid elements in student2 and sear,-id
is t-e ,-ara,ter string representing t-e id to 3e sear,-ed& T-e
3inar. sear,- (un,tion (or an arra. o( integers *as dis,ussed in
Le,ture 2;C T-at is:
Line No our,e Code
1 88ear,-es an ordered arra. o( integers
2 int 3sear,-'int dataDE2 88 input: arra.
0 int siYe2 88 input: arra. siYe
9 int /alue 88 input: /alue to (ind
: + 88 output: indeL i( (ound
; 88 ot-er*ise return A1
< F
= int lo*er2 middle2 upperC
> lo*er @ 0C
10 upper @ siYe A 1C
11 *-ile 'true+ F
12 middle @ 'lo*er B upper+ 8 2C
10 i( 'dataDmiddleE @@ /alue+
19 return middleC
1: else i( 'lo*er I@ upper+
1; return A1C
1< else i( '/alue H dataDmiddleE+
1= upper @ middle A 1C
1> else
20 lo*er @ middle B 1C
21 G
22 G
T-e (un,tion is no* modi(ied so t-at it *ould sear,- t-e data3ase (or
t-e sear,-id& Fill in t-e 3lan)s in line 10 and 1< in t-e (ollo*ing
(un,tion 3sear,-str'+&
Line No our,e Code
1 88ear,-es an ordered arra. o( integers
2 int 3sear,-'tudent%e,ord studentDE2 88 arra.
o( tudent%e,ord
0 int siYe2 88 input: arra. siYe
9 ,-ar sear,-idDE 88 input: /alue to (ind
: + 88 output: indeL i( (ound
; 88 ot-er*ise return A1
< F
= int lo*er2 middle2 upperC
> lo*er @ 0C
10 upper @ siYe A 1C
11 *-ile 'true+ F
12 middle @ 'lo*er B upper+ 8 2C
10 i( '
+
19 return middleC
1: else i( 'lo*er I@ upper+
1; return A1C
1< else i( '
+
1= upper @ middle A 1C
1> else
20 lo*er @ middle B 1C
21 G
22 G
Ans*er:
Line10: i( 'str,mp'studentDmiddleE&id2 sear,-id+ @@ 0+
or i( 'Pstr,mp'studentDmiddleE&id2 sear,-id++
Line1<: i( 'str,mp'studentDmiddleE&id2 sear,-id+ I 0+
or i( 'str,mp'sear,-id2 studentDmiddleE&id+H0+
20+ '9 mar)s+ 6i/en t-e (ollo*ing de(initions2 initialiYe Nline1N so
t-at it -as t-e end points '021+ and '220+ using assignment
statements onl.:
stru,t Point F
int L2 .C
GC
stru,t Line F
point p12 p2C
GC
/oid main'+F
Line line1C 88 Assignment statements to initialiYe line1 *it-
t-e end point gi/en
88P!T OO!% AN1E% SE%E '9 statements+

Vous aimerez peut-être aussi