Professional Documents
Culture Documents
SULIT
UI{I\18 RSITI MALAYSIA SARAWAK
!-F
i-
21
20L3
01
April2013
2.30-4.30 p.m
Delirna, DeTAR
2 jam (hours)
Dr. Cheah Wai Shiang/
Dr Azrnan b Bujang Masli
Arahan
lrlnstnt.ction)
(An.st',er
ALL
?'.rlis jawapan anda pada kertas jawapan yang disediaka;:i menggunakan daRwat
sahaja.
(Wt'ite yotlt'
4
5
printed page
pq,ge).
Unit Peperiksaan
Bahagian Pengajian Prasiswazah (BPPs)
7'/,.'l
Seciion
C I 1 3 3 iThl C' I 4 3 4 Dd tu
s [t.
Lt
Instruction:
.'( n an assigtrment
a I
will lot
will
/)
( 7. AII of the tbllowing
; a:2+(b:5);
=tr:c:5;
l,la:ll%3
$r'*
,tf
I
I
I
I
False
,,.{t
(i'
;14 Lnl statenrent must currtain a variable prececled by >> operator ./ (',J
b. ci. does not Proccss the inpyt-qrtir user presses RETURN keyx
c. yoLl can usc rnclre than onq(atuu\ input fi.om user by using ciir
\d. all of above
/.
Qr
fu1ctio1such
as
a.Any rnoditications to the variables x & y t}om insicle thc function will ,ot have any
eflbct outside the function.
b'The variables x and y will be upclated 'uvhen any mocliflcatiol is done i1 tl"le fu,ction
*ff\"rc variables x ancl y are passed to trre functicln aclditio, ,
d.None of above are valid.
Page
I of 1l
7'M C I 4 3
@ Ou.rtoucled functions
iTM C I 4 3 4 D u rit,r m c
nr
re,
n n
d,4 lgo r i t h nr
are
/{n**rine
#include <iostream>
using namespace std;
int operate (int a, int b)
1EL
trtum (a * b);
)
float operate (float a, floa{})
5:
return (a/b);
)
int mainQ
t
int x=5, 52;
float n:5.0,m:2.0;
cout << operate(x,y)
cout (<
<<"\t";
to
1fi
operate (n,m);
return 0;
)
a.10.0
b.5.0
c.10.0
d.ta
5.0
2.5
5
2.s
/?ge2olll
TlVl C I 4
3 /Tlvl C I 4 3
4 Du
tcr
s tt.u c tu
re a ntl
A lgo rit h m
Code -l:
switch (x) {
case 1:
cout <("x is 1";
break;
case 2:
e2
If (x::1)
Cout <<"x is
)
if (x:=2){
Cout << "x
Else
above
oo\
)d
.,r,
''
--,-t
y\rL_g
-l
ii[3]
d. Both a and c
above
/F \
^.r B\
/,il'ln,'n'1'Si
lLt--')
gn(\\''
tr\
t_l
G^
v
Page 3
of
ll
TM C I 4 3 i/I'ivl C I
i4
Da tu .t trlt ctu
r(
nd,1 lgori t lr nt
(->
l@.' Wrich of the following is
ar-r.advantage
list?
a.Arrays are always sofied.e
b.Arrays support more data types)
c.Array indices staft at zero.
y'.Accessing an arbitraly element in an aray is taster.
"/
)4.
a.The bottom.
b.The front.
4lhe top.
d.The back.
class template in C+* has the following structure
)6A
i;
What is the meaning of T in the above declaration?
a.It is a placeholder for a pointer value
-4.It is a placeholder for a\r type name
c.It is a string variable
d.lt rnust be an integer constant)
int *r;
xP:
17;
r:q;
*q:42;
p: q; l= hr
deleG r;
Page4al'll
TM C I
(q
4 3 3 /TM
CI4
4 Da to
s tr
uc tlr re
nd
lgo ri
th
nr
Suppose we have the fbllowing class whose underlying data structure is a linked
IISt OI
ListNodes.
class List {
public:
ll other public tbnctions
-List0;
private:
struct ListNode {
int item;
ListNode *next;
);
ListNode *head;
\'t,
I. fbr (ListNode
*n:
head; head !=
n = head->next;
delete head;
NULL; head: n) {
head; n !=
NULL; n->next)
delete n;
IIl, ListNode*
while (head
n;
!: NULL) {
n = head->next;
delete head;
head n;
$2
l{il
r*
//
only
a.Unordered array
,6.Sorted array
c.Linked list
$It
Oolt
ftrkr
''
6V
depends
Page 5
of
11
TM C
L{mlecture we clefined
1 4
3/TM C I 4 3 4 Du ta
.s
nt(
tu t
nd,1 lgorith nt
of integers:
class IntStack {
public:
IntStack( );
bool isErnpty( );
void push(int item);
int pop( );
int top( );
)
What happens if we execute the following staternents?
IntStack s;
int nl, n2,n3'
s.push(17);
s.push(la3); 4
s.push(42); 'r,r'
nl :
);
);
s.pop(
s.top(
s.push(n1);
n3 = s.pop( );
n2:
t LL
\\
\.
rr!'-.q, .
(hL
i?
't
z'
'
ni = s.top( );
ltStack
\dV*u"of
a.One
--Merc
c.-1
d.None of these
-y'An
-{LIFo
b.FIFO
c.Both l and2
d.None of these
.-l i 0
L!\"0
Page6ot'll
TA,l C I 1 3 3 /TM C I 4 -l
4 Do tq
s t,"Lt c
tu
re d nd
A lgo r i th
/pue,aelinked list
Ftf
r"
l, \
a.Node
5'
elernenl
t\)
li\
,#
\{f
,uy;u,
\V^]rJ
b.Linked list
\j-\
d.Constnrctor
.iY
&\
1y
.\*
g,\J" .# A
a^\}'-\-
,d,+ouy
A\
-,.-\
7{fttepop0
a.rernoveback$
-{f.isEmpty$
c.rernovedfiontQ
d.hasNext0
df"r.
l-[*,.
U
,{r,/ay
@.
Vni"h operation
a.Traversing
b.lnseftion"
-gReading
d.Deletion',
Q;
acldress is ?
{0
b.-r
c.1
PageT
ofll
Tt"l C I 4 3 3 iT A,l C I 4 3
D a tu
.s
t n t c tr
u'e
u nd .1
Igo r i t h nt
f,dtro,-," of Above
$lwo
't
fuf.ft
"situation
a.Overflow
-"#Underflow
c.Both of above
d.None of Above
;{lUetenns
a.Arrays
y'.Stacks
c.Linked List
d.None
{ln
'impoftant step.
a.Design
b.Implernentation
,zAnalysis
@r*"or
False: In object-oriented design (OOD), the frrst step in the problemsolving process is to identify the components.
)Y.True
b.False
;t{ru"C++
tq
(period)
b.Two semicolons,
c.The equal sign, :
d.The plus sign, +
,-{Dot,.
a{ l*"or
::
*Trae
b.False
G. f*"
Page ti
of I I
Til'lC I 4 3 -t/Tl,lC
I 4 -l
1 Du tu s tt.u(.ntt.e
nd
lgot.i th nl
a.True
b.False
)8.In
the
of the next
node.
a.Data
b.Url
c. Answer
- diAcldress
39. Deletion of an itern ti'om a linked list requires
--
a,insert
b.isEmpty
c.travelsal
dremove
4O. Deleting an item fior-n a linked
pointer-(s) so
--
-alOne
b.Two
c.Three
d.Four
End of section A
Page9ofll
TM C I 4 3 3 /Tl,l
Section
Instruction:
l.
C' I 4 3
4 D a ta,y t r u c tu re u nd A lgo ri t h rn
class
List{
/* public
*/
private:
struct Node{
ListDataType item; l/ the data of the node
Node* next; llpoints to the next node of the list
\.
t)
Node* head; llpoint to first node in the list
I.
))
Consider the linked list of ints represented by the tbllowing diagrarn:
[TTI----- | r2l-l
---.
[__il]
|NULL
a.Draw a diagram of the above list after the following lines of code have been
(5 marks)
executed:
..
o"tilNode* prev: head->next; "
:NoA.* nodeTolnsert = new Node;
fg:
nodeTolnsert->itern = 4;
nodeTo Insert-@ : prev->next;
prev->next:
nHdTolnsert;
Answer:
b.Assume that the code represented above in part (a) has executed. (3 marks)
What is the value of prev->item?
Answer
Page l0
of I I
TM C I 4 3 3 /7"M C I
4 3
4 Do ta
s tt "u c
at
re o nd
.4
lgori
th
(c) hi addition to the code above, assume the following code executes. (5 marks)
Draw a diagrarn of the list after this code executes as well.
prev = prev->next;
prev = prev->next;
Node* cun' prev->next;
prev->next
curr-)next;
delete curr;
curr: NULL;
Answer:
ww*
l\2el
Node
*nodelolastl,l = nw Nodei
xol,ilo\neo{
ta1+ nu+
fiodolol4guv( +
tul -
rwilolol:taot{
/o
End of section B
Page
ll of ll