Sei sulla pagina 1di 24

UNIVERSITI

TEKNOLOGI
PETRONAS

FINAL EXAMINATION
SEPTEMBER 201 4 SEMESTER

COURSE FATOOIS - FUNDAMENTAL OF PROGRAMMING I

DATE 23rd DECEMBER 2014 (TUESDAY)


TIME 9.00 AM - 12.00 NOON (3 hours)

INSTRUCTIONS TO CANDIDATES

SECTION A Answer ALL questions in the OMR sheet.


SECTION B 1. Answer ALL questions from this euestion Booklet.
2. Begin EACH answer on a new page in the Answer
Booklet given.
3. lndicate clearly answers that are cancelled, if any.
4. Where applicable, clearly indicate steps taken in arriving at
the solutions and state ALL assumptions, if any"
5. Do not open this Question Booklet until instructed.

Note : There are TWENTY FouR (24) printed pages including the cover page.

Universti Teknotogi PETRONAS


FATOOlS

SECTION A

[40 Marks]

Answer ALL questions in the OMR sheet.

1. Which of the following drives is a volatile memory?

A. RAM drive.
B. ROM drive.
C. Flash drive.
D. Thumb drive.

2. All the following components are included in problem solving EXGEPT

A. input
B. output
C. process
D, numbers

3' Which of the following expressions is the best to represent 0 < marks < 100
in PASCAL?

A. 0 :{marks :< 100


B. (marks ): 0) On (marks
C. (marks
D. (marks

2
FATOOl 5

4. Which one below is FALSE about flowchart?

A. Flowchart is a visual representation of tasks.


B. To be used only for j_f . . . rhen statement.
c. Represented by symbols and arrow.
D. The flowchart drawn can be varying from one to another even from
the same pseudocode. That is depend on the direction of arrows.

5 The following words are reserved words in pascal EXcEpr

A. begin
B. string
c. integer
D. character

6 which of the following statements is FALSE about case...of statement?

A. A{ternative fo r-if seleetio n:

B. Usually applied in menu-driven.


C. Valid for integer, character and boolean data types only.
D. This case. . .of code fragment is valid. Consider s is string,
Case s of
's, : writeln{'String, );
end;

7 During code compilation, compiler will ignore written by


programmer.

A. constants
B. identifiers
C. operators
D. comments
FATOOl 5

8. ln a syntactically correct program, error will cause abnormal


program termination during run time.

A. fatal
B. logic
C. run time
D. warning

9. The use of Break in loop will

A. delay the loop


B. stop the program
C. stop the loop and end the program
D. stop the loop and continue to the next statement after the loop

10. Rearrange the steps of preparing a program for execution.

l. Use a compiler to translate the source program into machine language.


ll. Any syntax errors must be edited before recompiled.
lll. Write a program by using by editor application.
lV. Object program and additional object files will be linked by linker/loader
and ready for execution.
V' When the source program is error free, the translated language is saved
as an object program.

A. l, ll, lll, lv, v


B. ll, lll, lv, l, v
c. lll, ll, l, v, lv
D. lll, l, ll, lv, v
FATOOlS

11. which one of the following is TRUE about procedure and function?

A. Both must receive parameters.


B. Procedure can be called orrce.
C. A procedure must return string value.
D. Procedure can be called in the function module.

Questions 12,13 and 14 refer to the code segment in FIGURE el2.

1. Procedure StudName(First, Middl_e, last.: string) ;


2. Begin
3. writeln('Name:' ,First+ , ' + Middle +, ,+ Last);
4. End;
5. Beg-n
6. N1:: ,Andrew,;
7. Name2:: ,Adam,;
8. StudName (Name2, N1, , John, );
9. End;

FIGURE Q12: Code Segment

12. What are the formal parameters?

A. Name2, N1, , John'


B. First, Middl_e, Last
C. tAdamr,tAndrewr,, John,

D. Name2, First, Mj-ddle, Last, N1


FATOOl5

13. lf the formal parameter is declared with var, which of the following will
return error?

A. Line 1

B. Line 3
c. Line 8
D. All lines

14. What is the reason for the error in Question 13?

A. The declared variables are exceeding the limit.


B. Actual parameter must be variable and not value.
c. Formal parameter must be value and not variable.
D. Data type for formal parameter is not compatible with actual
parameter.

15. The following functions are built-in functions, EXCEpT

A. Sqrt (x)
B. Pow (x, y)
c. Round (x)
D. upCase (x)

16. What would be the heading for a function called High, if it has two real
parameters, and returns the larger of these two real numbers?

A. Function High (rea1_1 , real_2: Real) ;


B. Function High (1real , 2real: Real) : Real;
c. Eunction High (rea11-, real2 : Real) : Reaf ;
D. Function High(real1, real2 : Real, var Larger);
FATOOlS

17 - which of the following are TRUE about actual parameters?

t. Actual parameters appear in procedure call.


il. Actual parameters appear in function heading.
ilr. The value of actual parameters will be used inside the procedure.
tv. Actual parameters pass the actual variables to formal parameters of
function.

A. I , ll and lll
B. l,ll andlV
c. l, lll and lV
D. All of the above

18. Before data values can be read from a file, the file must be prepared by
executing a(n) statement referencing the file variable.

A. read
B. write
C. reset
D. append

19. Which of following data types can be the index of array?

t. Real
il. lnteger
il1. Character
tv. Enumerated

A. I , ll and lll
B. l,ll andlV
c. ll, lll and lV
D. All of the above
FATOOl s

20' Rearrange these statements of operation on how to use the Text file.

l. Close the file.


ll. Declare a file variable.
lll. Associate a file variabre with a physicalfile name on a disk.
lV. Use the file variable as the first parameter in read or write operation.
V. Open a file using the procedure RESET that contains parameter of a
disk file.

A. ll, lll, v, lv, I

B. ll, lv, lll, v, I

c. ll, v, lv, lll, I


D. ll, v, lll, lv, I

21. function is useful to evaluate a condition and return True or


False result to the function caller.

A. Var
B. Nested
C. Boolean
D. Recursion

22. which of the following expressions will give syntax error?

A. 5+3*2-6DlV2
B. 5+3MOD2-6DlV2
C. 5*3+12l6DlV(2+2)
D. NOT (S +3* 2-6 DtV 2, O)AND (S + 3 MOD 2= 10)
FATOOlS

23. Given these values for boolean variables A, B and C:


A:: Fal-se; B:: True,. C:: False;
Allthe following expressions will return true EXCEPT?

L eoRAANDc
II. NOT(BORC)ORA
lll. Nor B oR (c oR NoT A)
IV. C OR ( A AND (B OR C))

A. l, ll and lll
B. l, ll and lV
C. l, lll and lV
D. ll, lll and lV

Question 24 and 25 refer to the code segment n FIGURE e24.

1. Program Find TheError;


2. var
3. x,y, z: integer,.
4.
5. begin
6. writeln(please Enter x, y: l;
1. z.: x + y
B. write]n ( 'The total:' z)
9. readln;
10. end.
FIGURE Q24

24. Which one below will return syntax errors?

A. Line: 1,3 and 6


B. Line: 1,6,7 and I
C. Line: 1,3, 6 and 8
D. Line: 1,3,6,7 and 8
FATOOls

25. Choose the CORRECT statements to fix the syntax error in Question 24.

L Line 1: No space is allowed for identifier


ll. Line 3: One of the identifiers should be declared as real
lll. Line 6: Missing the single quote "
lV. Line 7: Missing the semicolon ;

V. Line 8: Missing the comma ,

A. l, ll and lll
B. l, ll, lll and lV
C. l, lll, lV and V
D. l, ll, lll, lV and V

26. Which statement will return logic error and not related to the given TABLE Q26?
TABLE Q26
Symbol Points
$ 90-1 00
# 80-89
@ 60-79
& 50-59
*
40-49

A. If (Points >=90) and (points<:100) then


writeln ('$') ;

B. If (Points >:80) and (points<90)then


writ.eln('#');

C. If (Points >=60) or (points<:79) then


writeln ('G') ;

D, If (poinrs <40) then


writeln ('$#Gc') ;

10
FATOOlS

27 consist(s) of executable statements and assignment


statements that perform computations in one program.

A. Head section
B. Body section
C. Declaration section
D. Head, declaration and body section

28. The following formula must be written in pascal as

sin(a + b) : sin cos b + cos asinb

A. sin(A *B) ::sinA*cosB + cosA*sinB,.


B. sin (A+S) : =sin (A) *cos (B) + cos (A) *sin (B) ,.

C. slnA+B:: sin (A) *cos (B) + cos (A) * sin (B) ;


D. sin_AB:: sin (A) *cos (B) + cos (A) *sin (B) ;

29. Choose the INCORRECT statement.

A. A reserved word cannot be used as identifiers.


B. ldentifier could be constant, which value cannot be changed.
C. ldentifiers begin with a letter only and consist of letters, underscores
and digits.
D. Within declaration part in Pascal program, each identifier must be
declared.

11
FATOOl 5

30' Randomization is used to get the random number. lts parameter is the
highest possible number that can be generated. suppose:
num::random(30); it means that num will contain any integer number
between

A. 0to29
B. 0to30
C. 1to29
D. 1to30

For Question 31 to 35, refer to the declaration in FIGURE


e3l.
var
xt y : array[1..5] of integer,.
z, i : lnteger;
where a procedure or function is involved, the declaration
is shown in the accompanying comment.

FIGURE Q31: Declaration Code

Based on the declaration in FIGURE e31, what is the output when the following
program is executed?

12
31. for i::1 to 3 do
begin
xlil :: i;
ytil :: xlil * xlil;
end;
z .: (1 .
v,

for i::1 to 3 do if (ytil >:2) and (ylil < 4) rhen


z i: z + x[i];
writeln(y[1], ' and ,, z)i

A.
B.

c.
D.

x :: {1, 5, 3};
ifx[0]=Lthen
x:--x-3;
else
v .:
.
QO.
JJ
'

writeln (x [0] , , and ' , x[1] );

A.
B.

c.
D.

13
FATOOlS

33. x[0] := 100,' x[1] := 130t x[2] := 1;


shuffle (x) ;
writefn (x [0] , ' and ', x[1]);

where shuffle has been defined as foll_ows:


procedure shuffJ-e (y : array of integer)
begin
ytol :: y[1];
end;

A. 100 and 100


B. 130 and 100
C. 100 and 130
D. Compilation error

34. x [1] :: ceil (0. 5) ;


x[2] :: cei]_ (I.2I) ;
x [3] :: ceil- (9.99) ;
case x[2] of
3 : writeln (xL2l ) ;
4: writeln (x [ 1] ) ;
I, 2 z writel-n (x[3] );
el_se
writeln(twhatever,);
end;

4.2
8.9
c. 10

D. whatever

14
FATOOlS

35. x[0] ::2; x[1] :2;


case x [0] /x t1 I of
1 : Writeln(.10,);
2 : Writeln('30,);
e1 se

Writ.eln(,el-se, );
end;

A. 10

B. 20
C. else
D. Syntax error

15
For Question 36 to 40, refer to the declaration in FTGURE e36.
var
x, y : arrayt1..5l of integer;
z, i : integer;
where a procedure or function is involved, the declaration
is shown in the accompanying comment.

FIGURE Q36: Declaration Code

Based on the declaration in FIGURE e36, what is the output when the following
program is executed?

36. x[0] :: 1; x[1] :: 4; xL2l .- E.

whil-e x[1] < (xt0l * x[2]) do


begin
write (x t0l ,, #, )
write(xt1l ,,#,)i
wrj-teln (xl2l , '#, ) ;
x[1] := x[1] * 2;
x[0] :: x[0] + 1;
end;

A. 1#4#5
B. 2#8#5
c. 1#4#5
2#8#5
D. lnfinite loop

16
FATOOl 5

37. for i:= 1 to 5 do


xlil :: i * i;
z := count(x, false);
writel-n (z ) ;

{ where count has been declared as fol-lows:

function count (arr : array of integer; even : boor-ean) : integer;


var
i, c : integer;
begin
c := 0;
for i::1 to 5 do
begin
if (arrlil mod 2 : 0) and even then
begin
c :: c + 1;
end
el-se if (arr[i] mod 2 : 7) and (NOT even) then
beqin
c :: c + 1;
end;
end;
count := c;
end;
)

A. I
8.2
c.3
D. Syntax error

17
39. for i:: 1 to 5 do
x[i] :: i * i;
z :: count (x) ;
writeln (z) ;
{ where count has been declared as folows:
procedure count (arr : array of integer)
var
i, c : integer;

.: n.
v,

for i:=1 to 5 do
c :: c + arr[i];
count i: c;

4.0
B. 50
c. 54
D. Syntax error

19
FATOOl 5

40. i := 0;
repeat
writel-n(i:i);
i:=t+2;
ifi>10then break;
until fal-se;

A. 0246810
8.0
2

4
6

I
10

c.0

I
10
D. lnfinite loop

20
FATOOl 5

SECTION B
[60 Marks]

Answer ALL the questions in the Answer Booklet.

1. Permutation refers to the act of rearranging or permuting some or all the


members of a set into some sequence or order. A program is needed to compute
the number of possible permutation for a given set. The formula for the
calculation involved is as follows:
N!
(^/ - /r) !
where N is the total number of members of the set, k is the number of members
to be rearranged, N! denotes the factorial of N, and (N-k)! denotes the factorial of
N-k.

a. Write a function calcu-LateFactorial- that will take an integer A as


parameter. This function will calculate the factorial of A, denoted A!, using the
formula below:
Al='J.x2x...xA
and return the result.
(NOTE: Apply for-do loop to calculate factorial)
[10 marks]

b. By using the above formula for permutation, write a function


calculatePermutation that will take integer N and integer R as the
parameters. This function will call function cal-culateFactori-a1 answered
in Part 1(a) and calculate the value for permutation,

[8 marks]

c. Explain why it is better to declare calculateFactorial as a function


rather than a procedure.

[2 marks]

21
FATOOl 5

2. A data file contains the list of measurements recorded at a number of machines


in a petroleum plant. The content of an example data file is shown in FIGURE
Q2:

21 11 2014
6
4141 60"5
cD42 80.5
F0153 60
4404 55,5
4481 75
83616 85

FIGURE Q2

The first non-blank line in the file contains the day, month and year for the
measurement. The second non-blank line in the file contains an integer that
indicates the number of lines of data in the file. Each line of data contains the
following sequence of data: machine id and temperature.

a' Draw a flowchart for a program that reads the name of a data file from the
user, reads the file, computes the minimum, maximum and average
temperature for the machines listed in the file, and write out to a report file
that has a format as shown in FIGURE e2(a):

Repart *te: 21 11 I t2t14


Minirnum Ternperature 6t {Maehine F1SS}
Maxirp* Temper*ture: 85 {dacine Bg6t6}
Avarage Temperetur*: fiS.4

FIcURE a2(a)

[10 marks]

22
FATOOls

b. Discuss why, in this particular case, using a file would be more appropriate
as compared to performing input and output interactively.

[2 marks]

c. Translate the flowchart answered in Part 2(a)to a PASCAL program.

[8 marks]
FATOOl 5

3. A class of 200 students sits for a Programming examination. A program will be


written to analyze the student's performance in that examination. FIGURE e3
shows the declaration of an array that willstore the marks of the students.

var mark: Array t1..2001 of real_,.

FIGURE Q3

a. Write a procedure enterMark that prompts and stores examination marks


of 200 students into array mark.

[4 marks]

b. Write aprocedure dispJ-ayAverage that calculates and displays the


average mark in array mark.

[7 marks]

c. Write a procedure displayHighest that determines and displays the


highest mark in arcay mark.

[9 marks]

- END OF PAPER -

24

Potrebbero piacerti anche