You are on page 1of 12

-

SULIT
UI{I\18 RSITI MALAYSIA SARAWAK

!-F

94300 Kota Samal'ahan


Sarawak

i-

Fakulti Sains Komputer & Teknologi Maklumat


TMC 1433/TMC 1434
TMC 1433/ TMC 1434 Data Structure and Algorithm

Peperiksaan Pertengahan I (MIDTERM EXAM I)


No. Matrik (Ma,tric No.)
Semester/Sesi(Senzesl er / Se s si,or)
Tarikh (Date)
Masa (Ti,nt.e)
Ternpat (Ventte)
Jangka masa (Du'ati,on)
Pensyarah (Lecil tl'er)

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)

Jawab SEiVIUA soalan.

Baca soalan dengan teliti sebelum menjawab.


(Read tl'te qu,estions co,refu,lly before o.nstuering th,ent).

(An.st',er

ALL

qtr est ion s).

?'.rlis jawapan anda pada kertas jawapan yang disediaka;:i menggunakan daRwat
sahaja.

(Wt'ite yotlt'

4
5

e.rLstuet's irr, truh only).

Dil"lrang bercakap atan mengganggu calon-calon dalam janglia masa pel,leriksaan.


(Do not ,,o.lh or d,istu,rb ath.et' cancl,id,o,tes clu,ring tl-te exo,ntiruation).
Calon tidal< dibenarlian meninggalkan biliir peperiksaan dalam ternpoh 30 mimt
pertama dan 15 minit teralihir peperihsaau.
(Ca,n,ci.i.cla.tes ate not o.ll,owet! to l.ea,ue th,e exq,nirLa.tion. lwLl d.u;'in,g th,e
first 30 ninutes
o.rud tlrc la.st 15 nr.intt.tes of th,e exanti,nct.tion).
Calon dil<ehendaiii ni.'matuhi ETIKA AKADEMiI{ seperti yang tertera dalam
perkara 12 Peraturan Akadernik ljazah Sarjana Muda, UNIMAS.
Candidates are required to contply with ihe Etika Ahadenik as stated in Clause 12 of
Pe ra t u ra n,4li a d e ntik Ij a za h Sa tj a n a l[u da, L\NIMAS.

I(ertas peperiksaan ini mengandungi sebelas (11) halarnau bercetak (tidak


terrnastili halauran irr-rlit).
Tlt"is exa,nt.in.o,ti.on pa.per coruslsls of eleuen (11)

printed page

pq,ge).

Unit Peperiksaan
Bahagian Pengajian Prasiswazah (BPPs)

(exch,d,ing the couer

7'/,.'l

Seciion

C I 1 3 3 iThl C' I 4 3 4 Dd tu

s [t.

Lt

Lte ntl A lgor i th nr


u

MCQ Question [80 marks]


Answer ALL Questions

Instruction:

.'( n an assigtrment

statelneut a:tr; Which of'the tbllowing statement is TRUE?

a. The variable a arrcl the variable b are equal. '-

Etl'r" value of b is assigned to variable aburt the later ch4nges on variable


effbct the value of var.iable a
',,/

c. The value of b is assigrred to variable a ancl the later cl-ranges on


variable b
etl-ect the value of variable

a I

will lot
will

d. The value of variable a is assigr-recl to variable b ancl the value of variable


b is
assigtred to variable a.
X

/)
( 7. AII of the tbllowing
; a:2+(b:5);

=tr:c:5;
l,la:ll%3

$r'*
,tf

are valicl expressions in C++

I
I

I
I

False

wnicn of the fbllowing statenrcnt is true regarding cin statement?

,,.{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

/.

st"ay the tbllowi,g piece of cocle

ancr crroose the BEST answer.

int x:5, y:-1 , z;


a:addition(x,y)
.--a:The tunction addition is called by passing the values
b. The fr-urction addition is callecl by passing ret-erence
lrr casrr of arguments passed by values when calling
z:addidion(x,y),

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

a.Very long functions that can hardly run


b. One function containing another one or more functions inside it.
gfwo or more functions with the same name but different number of parameters or type,
d. None of above

/{n**rine

the following program ancl detennine the output.

#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

,ffiind out the error in following block of code.


lrqx$rooi
Cout << "x is 100";
a.100 should be enclosed in quotations -p
b.There is no semicolon at the end of first line y
to operator mistake
-e.:Equals
d.Variable x should not be inside quotation y

/?ge2olll

TlVl C I 4

3 /Tlvl C I 4 3

fiConsider the fbllowing two pieces of

4 Du

tcr

s tt.u c tu

re a ntl

A lgo rit h m

codes and choose the best answer.

Code -l:
switch (x) {
case 1:
cout <("x is 1";
break;
case 2:

cout <<"x is 2";


break;
default:
cout (("value of x unknown',;
)

e2

If (x::1)
Cout <<"x is
)

if (x:=2){
Cout << "x

Else

r*Eoth of the above code

fi'agrnents have tlie same behaviour


b.Both of the above code fragrnents produce different effects,,
c.The first code produces more results than second *
d.The second code produces more results than first. p
10. Tlie

"retum 0;" statement in main function indicates

program did nothing; cornpleted 0 tasks !


B"'fhe program worked as expected without any erroe during its execution r
',*'\
gNot to end the program yet.
oinYt
,'d.None of
,.J
,_,^Lorkof,*1t''

above

oo\

)d

.,r,

''

--,-t

u{ ,t On which principle doesFac[-lwork?


. r.,.-l\-,*1,:',"
zl.
g*r"\";;;'" -^?
d

y\rL_g
-l

ii[3]

d. Both a and c

above

@V/nia'tof tlie tbllowing


list?

/F \

^.r B\
/,il'ln,'n'1'Si
lLt--')
gn(\\''

tr\

t_l

G^
v

is a dEgd:rul&& of using an qSQr versus using a linked

A.rnserting into an anay may require shifting many other elements.


b.Anays cannot be sortedsp
c.Not all elements in an affay can be accessed.*

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

.d.Declaring an affay is a cumbersome programming task.

(->
l@.' Wrich of the following is

ar-r.advantage

otusing an_qlray versus using a linkeci

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.

push and pop are operations aff-ecting whicli end of a stack?

a.The bottom.
b.The front.
4lhe top.
d.The back.
class template in C+* has the following structure

)6A

ternplate <class T> class TerrplatedClass {

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)

*"there any dvnamic


6d
\-/
int *p: new int;
int *q: new int;

lnemory management en'ors in tlre fbllowing cocle?

int *r;

xP:

17;

r:q;
*q:42;
p: q; l= hr

deleG r;

a.No, there are no emors


b.Yes, a memory leak
c.Yes, misuse of a dagglinggointer
,{nes, both a memory leak and misuse of a dangling pointer

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,

Mrich of the fbllowing

sequences of code could be used in the destructor -ListQ to


conectly delete all of the nodes in the list?

I. fbr (ListNode

*n:

head; head !=

n = head->next;
delete head;

NULL; head: n) {

II. for (ListNode *n

head; n !=

NULL; n->next)

delete n;

IIl, ListNode*
while (head

n;

!: NULL) {

n = head->next;
delete head;
head n;

$2

a.I and II only


b.ll and III only
$ and III only

l{il

r*

//

only

\LE. Suppose you were implernenting

a data structure to store rn&uaatrou about the


paintings on displM an art dealer's showroom. Of the following data structures,
which one is the right one to use?

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

a class IntStack to implernent a stack

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?

' 5\.,,1 '. '


-a'f
t'\ --q

't

z'

'

ni = s.top( );

a.Stack contains 143 (top), I 7 (bottorn) ; n7:42, n2:42, tt3=42


b.Stack contains 42 (top),42, 143,17 (bottorn); n1=42, n2:42; n3:42
c.Stack is empty; n7:17, n2:143, n3:42
contains 143 (top), l7 (bottom); nl=[43, n2:143,n3:42

ltStack

\dV*u"of

the first linked list index is

a.One

--Merc
c.-1

d.None of these

@Olinked list index is


Integer
b.A variable
c.A character
d.A boolean

-y'An

that represents the position of a node in'a linkecl list.

fonn of access is used to add-and rerrove nodes fiom a stack.

-{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

is a data structure that organizes data similar to a line in the


7/e
'supennarket,
where the tlrst one in line is the first
one out.

/pue,aelinked list

Ftf

b.Stacks linked list


c.Both of thern
d.Neither of thern

r"

an anay queue, data is stored in an

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

mernber function detennines if the stack is empty by callilg the


member function.

a.rernoveback$

-{f.isEmpty$
c.rernovedfiontQ
d.hasNext0

Wtrat happens when you push a new node onto a stack?

df"r.

new node is placed at the ti-ont of the linked list.


affihe new node is placed at the back of the linked list.
c.The new node is placed at the rniddle of tlie linked list.
d.No Changes happens

l-[*,.
U

@.What is Data Structure ?


to qrganize data
b.Accessing of data elements in specitied maru1er
c. Orgarrization of rnathernati cal and logical concepts
S[11 of Above

,{r,/ay

@.

Vni"h operation

is not possible on Data Structure ?

a.Traversing
b.lnseftion"

-gReading
d.Deletion',

Q;

fne value of flrst linkecl list

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

dimensional arrays are also called

't

a.Matrix Array [ ]r:


b,Table Array
-rBoth a and b
d.None of the Above

fuf.ft

"situation

in linked list START:NULL is callecl ?

a.Overflow
-"#Underflow
c.Both of above
d.None of Above

;{lUetenns

PUSH and POP are related to

a.Arrays

y'.Stacks
c.Linked List
d.None

{ln

the software development process, the

phase is the firqt and most

'impoftant step.
a.Design

b.Implernentation

,zAnalysis

d.Testing and debugging

@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

member access operator is the

(period)
b.Two semicolons,
c.The equal sign, :
d.The plus sign, +

,-{Dot,.

a{ l*"or

::

False: A pointer variable is a variable whose content is a memory adclress.

*Trae
b.False

G. f*"

or False: An asterisk (x) is the unary clirection or ret'erencing operator.

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

linked list, every rrode (except the last node) contains

the

of the next

node.

a.Data

b.Url
c. Answer
- diAcldress
39. Deletion of an itern ti'om a linked list requires

of the linkecl list.

--

a,insert

b.isEmpty
c.travelsal

dremove
4O. Deleting an item fior-n a linked

list is best when pelfbnlecl with


that the deleted node can be fieed from memory.

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

Long Question [20 marks]


Answer ALL Questions

Consider the List class with the following private members:

class

List{

/* public

members here ...

*/

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

d) Write a code to insert a node at the end of a linked list. (7%)


Notes: You should use the node declaration at part a.
Answer:

Node

*nodelolastl,l = nw Nodei

xol,ilo\neo{

ta1+ nu+

fiodolol4guv( +

tul -

rwilolol:taot{

/o
End of section B

Page

ll of ll