Sei sulla pagina 1di 30

Chapter 6 Arrays, Text Files, Procedures and Functions Question Bank

Chapter 6 Arrays, Text Files, Procedures and Functions


Multiple Choice Questions

(U03C06L01Q001)
What is the output of the following Pascal statements? (A and B are string variables.)
A := 'X';
B := 'Y';
write (‘A’, ‘B’, A, B)
A. ABAB
B. ABXY
C. XYAB
D. XYXY
Answer
B

(U03C06L01Q002)
Consider the following program.
program X;
var n : integer;
procedure p (n : char);
begin
(* Statement S *)
writeln(n)
end;
begin
(* Statement T *)
writeln(n)
end.
Which of the following are valid for statement S and statement T?
Statement S Statement T
A. n := ‘5’; n := 7;
B. n := ‘5’; n := ‘7’;
C. n := 5; n := ‘7’;
D. n := 5; n := 7;
Answer
A

Computer & Information Technology for HKCEE 1 © Pearson Education Asia Limited 2004
(Module A)
Chapter 6 Arrays, Text Files, Procedures and Functions Question Bank

(U03C06L01Q003)
Consider the following procedure heading.
procedure P2 (A : real; var B : integer);
Which of the following statements are INVALID if r is a real variable and n is an integer variable?
(1) P2(n, 5)
(2) P2(n, n)
(3) P2(2.5, r)
A. (1) and (2) only
B. (1) and (3) only
C. (2) and (3) only
D. (1), (2) and (3)
Answer
B

(U03C06L01Q004)
What is the output of the following program?
Program Q3;
var m, n :integer;
procedure p( var x : integer; y : integer);
begin
x := 10; y := 20
end;
begin
m := 1; n := 2;
p(m, n);
writeln(‘m = ’ ,m, ‘, n = ’, n)
end.
A. m = 1, n = 2
B. m = 1, n = 20
C. m = 10, n = 2
D. m = 10, n = 20
Answer
C

Computer & Information Technology for HKCEE 2 © Pearson Education Asia Limited 2004
(Module A)
Chapter 6 Arrays, Text Files, Procedures and Functions Question Bank

(U03C06L01Q005)
What is the output of the following program?
Program Q4;
var m, n :integer;
procedure p (var x : integer; y : integer);
begin
x := x + 1; y := y + 10
end;
begin
m := 100;
n := 200;
P(m, n);
P(n, m);
writeln(m)
end.
A. 100
B. 101
C. 110
D. 111
Answer
B

Computer & Information Technology for HKCEE 3 © Pearson Education Asia Limited 2004
(Module A)
Chapter 6 Arrays, Text Files, Procedures and Functions Question Bank

(U03C06L01Q006)
What is the output of the following program?
Program Q5;
var m, n :integer;
procedure p( var x : integer; y : integer);
begin
x := x +1;
y := y + 10
end;
begin
m := 100;
n := 200;
P(m, n);
P(n, m);
writeln(n)
end.
A. 200
B. 201
C. 210
D. 211
Answer
B

(U03C06L01Q007)
Consider the following procedure heading.
procedure P6 (var A : real; B : integer);
Which of the following statement is valid if r is a real variable and n is an integer variable?
A. P6(n, 9);
B. P6(7.5, n);
C. P6(r, 1.5);
D. P6(r, 2);
Answer
D

Computer & Information Technology for HKCEE 4 © Pearson Education Asia Limited 2004
(Module A)
Chapter 6 Arrays, Text Files, Procedures and Functions Question Bank

(U03C06L01Q008)
Consider the following procedure P7.
procedure P7 (var x : real; y : integer);
var i : integer;
begin
for i := 2 to y do
x := x * 2
end;
What is the value of x after the statements below are executed?
x := 2;
P7(x, 5)
A. 1
B. 2
C. 10
D. 32
Answer
D

(U03C06L01Q009)
What are the contents of the integer variables x and y?
val(‘999.9’, x, y);
A. x = 0 y = 0
B. x = 0 y = 4
C. x = 999.9 y = 0
D. x = 999 y = 0
Answer
B

(U03C06L01Q010)
What is the content of the string variable s?
str(-999.0, s);
A. ‘-999’
B. ‘-999.0’
C. ‘-9.99E+02’
D. ‘-9.9900000000E+02’
Answer
D

Computer & Information Technology for HKCEE 5 © Pearson Education Asia Limited 2004
(Module A)
Chapter 6 Arrays, Text Files, Procedures and Functions Question Bank

(U03C06L01Q011)
What is the content of variable m after executing the following statements? (Where m is a string
variable, r is a real variable and n is an integer variable.)
......
m := ‘-35’;
val(m, r, n);
str(r, m);
......
A. ‘-35’
B. ‘-3.5000000000E+01’
C. ‘0’
D. ‘0.0000000000E+00’
Answer
B

(U03C06L01Q012)
Data, when put in ascending order of size, can be arranged into which order?
A. character, file, record, field
B. character, field, record, file
C. record, file, character, field
D. field, file, character, record
Answer
B

(U03C06L01Q013)
In Pascal, data files are stored in the format of
A. text files.
B. numeric files.
C. random files.
D. sequential files.
Answer
A

Computer & Information Technology for HKCEE 6 © Pearson Education Asia Limited 2004
(Module A)
Chapter 6 Arrays, Text Files, Procedures and Functions Question Bank

(U03C06L01Q014)
In Pascal, text files are
A. sequential files.
B. random files.
C. index sequential files.
D. index random files.
Answer
A

(U03C06L01Q015)
Which of the following is a correct declaration of a file variable?
A. var XFILE : test;
B. var XFILE : text;
C. variable XFILE : test;
D. variable XFILE : text;
Answer
B

(U03C06L01Q016)
Which of the following statements of using procedure assign is correct?
A. assign(filename, ‘a:\master.txt’);
B. assign(filename, a:\master.txt);
C. assign(‘a:\master.txt’, filename);
D. assign(a:\master.txt, filename);
Answer
A

(U03C06L01Q017)
Which of the following Pascal procedures is used to prepare a file for writing data to it?
A. reset
B. restart
C. rewrite
D. reread
Answer
C

Computer & Information Technology for HKCEE 7 © Pearson Education Asia Limited 2004
(Module A)
Chapter 6 Arrays, Text Files, Procedures and Functions Question Bank

(U03C06L01Q018)
A student writes a rewrite statement in the following form:
Rewrite(filea)
If the file associated with the file filea does not exist on the secondary storage medium, what would
happen?
A. An error message will appear.
B. Nothing happens.
C. A file on the secondary storage is renamed as filea.
D. A file on the secondary storage with the name filea is created.
Answer
D

(U03C06L01Q019)
Which of the following statements about the Reset statement are true?
(1) Reset is used to prepare a file for reading data from it.
(2) Data cannot be written to the file opened by reset.
(3) If the file does not exist, the computer system will create a new one.
A. (1) and (2) only
B. (1) and (3) only
C. (2) and (3) only
D. (1), (2) and (3)
Answer
A

(U03C06L01Q020)
Refer to the program segment below. M and N are integer variables and A is an array of integers.
for M := 0 to 100 do
A[M] := M;
for N := 0 to 100 do
A[N] := A[100 - N];
What is the value of A[20] and A[80] after the execution of the program segment?
A. 20 and 80 respectively
B. 80 and 20 respectively
C. 20 and 20 respectively
D. 80 and 80 respectively
Answer
B

Computer & Information Technology for HKCEE 8 © Pearson Education Asia Limited 2004
(Module A)
Chapter 6 Arrays, Text Files, Procedures and Functions Question Bank

Conventional Questions
(U03C06L02Q001)
Write down the output of the following Pascal statements. (Use a ‘ ’ to indicate a blank.)
(a) write('Enter a number: ')
(b) write(678.267 : 10)
(c) write(1200.00)
(d) write(57 : 4, 78 : 4, 100 : 4)
(e) write('654,321')
(f) write('A', 'B');
write('C');
writeln('D', 'E');
writeln('F');
write('G')
(g) X := 1; Y := 2; write('X', '=', X, Y, '=', 'Y');
(7 marks)
Answers
(a) Enter a number:
(b) 6.783E+02
(c) 1.2000000000E+03
(d) 57 78 100
(e) 654,321
(f) ABCDE
F
G
(g) X=12=Y

Computer & Information Technology for HKCEE 9 © Pearson Education Asia Limited 2004
(Module A)
Chapter 6 Arrays, Text Files, Procedures and Functions Question Bank

(U03C06L02Q002)
Write statement(s) for each of the following cases.
(a) Display your name.
(b) Prompt a student to input his/her name. Store the name in a string variable StudentName.
Then display the word ‘Hello!’ and the name of the student. For example, if the student
inputs Paul, the program displays ‘Hello! Paul’.
(c) Prompt users to input a number and then display the square root of the number with 1 decimal
place.
(8 marks)
Answers
(a) write(‘Chan Siu Ming’)
(b) write(‘Input your name: ’);
readln(StudentName);
writeln(‘Hello! ’, StudentName);
(c) write(‘Input a number: ’);
readln(Num);
writeln(sqrt(Num) : 0 : 1);

Computer & Information Technology for HKCEE 10 © Pearson Education Asia Limited 2004
(Module A)
Chapter 6 Arrays, Text Files, Procedures and Functions Question Bank

(U03C06L02Q003)
Write a program BodyMassIndex which receives the name, the weight in kg and the height in m
of a student. Then the program calculates and displays the Body Mass Index (BMI) of the student
according to the formula below.
Weight
BMI =
Height 2
A sample output is given below.

Sample output:
Enter your name: Peter Chan
Enter your weight in kg: 50.25
Enter your height in m: 1.65
Peter Chan’s BMI is 18.5 (6 marks)
Answers
program BodyMassIndex;
var
Name : string;
Weight, Height, BMI : real;
begin
write(‘Enter your name: ’);
readln(Name);
write(‘Enter your weight in kg: ’);
readln(Weight);
write(‘Enter your height in m: ’);
readln(Height);
BMI := Weight /(Height * Height);
writeln(Name, ‘’’s BMI is ’, BMI : 0 : 1)
end.

Computer & Information Technology for HKCEE 11 © Pearson Education Asia Limited 2004
(Module A)
Chapter 6 Arrays, Text Files, Procedures and Functions Question Bank

(U03C06L02Q004)
Write a program DecimalPlaces which receives a number and the required decimal places from
keyboard. Then the program displays the number with the required decimal places. A sample output
is given below.

Sample output:
Enter a number: 3.1415926
How many decimal places are required? 4
The input number is 3.1416 (corrected to 4 decimal places). (6 marks)
Answers
program DecimalPlaces;
var
Num : real;
Dec : integer;
begin
write(‘Enter a number: ’);
readln(Num);
write(‘How many decimal places are required? ’);
readln(Dec);
write(‘The input number is ’,Num:0:Dec);
writeln(‘ (corrected to ‘,Dec,’ decimal places).’);
end.

Computer & Information Technology for HKCEE 12 © Pearson Education Asia Limited 2004
(Module A)
Chapter 6 Arrays, Text Files, Procedures and Functions Question Bank

(U03C06L02Q005)
Write a Pascal program NameAndAge which prompts a user to input his/her name and age. Then
the program displays the user’s name and age in complete sentence.

Sample output:
Input your name: Peter Wong
Input your age: 16
Peter Wong is 16 years old. (5 marks)
Answers
program NameAndAge;
var
Name : string;
Age : integer;
begin
write(‘Input your name: ’);
readln(Name);
write(‘Input your age: ’);
readln(Age);
writeln(Name, ‘ is ’, Age, ‘ years old.’);
end.

Computer & Information Technology for HKCEE 13 © Pearson Education Asia Limited 2004
(Module A)
Chapter 6 Arrays, Text Files, Procedures and Functions Question Bank

(U03C06L02Q006)
(a) Write a program ConvertTime which receives a time in ‘hour : minute : second’ format and
then converts the input time to number of seconds. A sample output is given below.

Sample output:
Enter a time in hour, minute, second : 13 24 5
13 hours 24 minutes 5 seconds have 48245s

(b) Write another program ConvertTime2 which receives a time in second and then converts
the time to ‘hour: minute: second’ format. A sample output is given below.

Sample output
Enter a time in seconds (0-86400): 48245
13 : 24 : 5
(12 marks)
Answers
(a) program ConvertTime;
var
h, m, s : real;
begin
write(‘Enter a time in hour, minute, second: ’);
readln(h, m, s);
write(h:0:0, ‘ hours ’, m:0:0, ‘ minutes ’, s:0:0);
writeln(‘ seconds have ’, ((h * 60) + m) * 60 + s:0:0, ‘s’)
end.
(b) program ConvertTime2;
var
s : real;
begin
write(‘Enter a number in seconds (0-86400): ’);
readln(s);
write(trunc(s) div 3600:2,‘:’);
write((trunc(s) mod 3600) div 60:2,‘:’);
writeln((trunc(s) mod 3600) mod 60:2)
end.

Computer & Information Technology for HKCEE 14 © Pearson Education Asia Limited 2004
(Module A)
Chapter 6 Arrays, Text Files, Procedures and Functions Question Bank

(U03C06L02Q007)
Write a procedure PowerOf which gets two integer parameters M and N. Then it displays the
value of MN on screen. (6 marks)
Answers
procedure PowerOf (M, N : integer);
var I, X : integer;
begin
X := 1;
for I := 1 to N do
X := X * M;
writeln(X)
end;

Computer & Information Technology for HKCEE 15 © Pearson Education Asia Limited 2004
(Module A)
Chapter 6 Arrays, Text Files, Procedures and Functions Question Bank

(U03C06L02Q008)
Consider the following procedure heading CheckDate.

procedure CheckDate(Date: string; var dd, mm, yyyy: integer);

Value parameter Date stores a date in the format of "dd/mm/yyyy", such as, "01/02/2003". The
procedure separates the date into 3 parts, the day, the month and the year and stores them in variable
parameters dd, mm and yyyy correspondingly. If there is an error in the date, all the variable
parameters will have values 0. Complete the procedure. (11 marks)

Computer & Information Technology for HKCEE 16 © Pearson Education Asia Limited 2004
(Module A)
Chapter 6 Arrays, Text Files, Procedures and Functions Question Bank

Answers
procedure CheckDate(Date: string; var dd, mm, yyyy: integer);
var e1, e2, e3 : integer;
OK : Boolean;
begin
val(copy(Date, 1, 2), dd, e1);
val(copy(Date, 4, 2), mm, e2);
val(copy(Date, 7, 4), yyyy, e3);
OK := false;
if (e1 = 0) and (e2 = 0) and (e3 = 0)
then
begin
case mm of
1, 3, 5, 7, 8, 10, 12 : if dd <= 31
then OK := true;
2 : if dd <= 28
then OK := true;
4, 6, 9, 11: if dd <= 30
then OK := true
end
end;
if not OK
then begin
dd := 0;
mm := 0;
yyyy := 0;
end
end;

Computer & Information Technology for HKCEE 17 © Pearson Education Asia Limited 2004
(Module A)
Chapter 6 Arrays, Text Files, Procedures and Functions Question Bank

(U03C06L02Q009)
Program Quadratic receives the coefficients of a quadratic equation and displays the roots of the
equation.
program Quadratic;
var A, B, C, D, R1, R2 : real;
begin
writeln(‘The general format of a quadratic equation is Ax^2 +
Bx + C = 0.’);
write(‘Enter coefficients A, B and C : ’);
readln(A, B, C);
D := B * B – 4 * A * C;
if D = 0
then
begin
R1 := (-B + sqrt(D)) / (2 * A);
write(‘The equation has two real and equal roots: ’ ,
R1:0:2)
end
else if D > 0
then
begin
R1 := (-B + sqrt(D)) / (2 * A);
R2 := (-B - sqrt(D)) / (2 * A);
writeln(‘The equation has two real and unequal
roots:’ ,
R1:0:2,‘ and ’,R2:0:2)
end
else writeln(‘The equation has no real root.’)
end.

Program Quadratic is rewritten by using modular approach. The main program is given to you
as follows:

Computer & Information Technology for HKCEE 18 © Pearson Education Asia Limited 2004
(Module A)
Chapter 6 Arrays, Text Files, Procedures and Functions Question Bank

program QuadraticEquation;
var A, B, C, D, R1, R2 : real;
(* procedures declaration *)
begin
GetCoefficient(A, B, C);
FindDiscriminant(A, B, C, D);
if D >= 0
then FindRoot(A, B, C, D, R1, R2);
DisplayResult(D, R1, R2);
end.

The details of each procedure are as follows:


(a) procedure GetCoefficient prompts users to key in the coefficients.
(b) procedure FindDiscriminant receives coefficients A, B and C as input and finds out the
discriminant D of the equation as output.
(c) procedure FindRoot receives coefficients A, B, C and discriminant D as input and then find
out the roots R1 and R2 as output.
(d) procedure DisplayResult displays the roots of the equation.
Complete the procedures. (15 marks)

Computer & Information Technology for HKCEE 19 © Pearson Education Asia Limited 2004
(Module A)
Chapter 6 Arrays, Text Files, Procedures and Functions Question Bank

Answers
(a) procedure GetCoefficient (var A, B, C : real);
begin
writeln(‘The general format of a quadratic equation is Ax^2
+ Bx + C = 0’);
write(‘Enter coefficients A, B and C : ’);
readln(A, B, C)
end;
(b) procedure FindDiscriminant (A, B, C : real; var D : real);
begin
D := B * B – 4 * A * C
end;
(c) procedure FindRoot (A, B, C , D : real; var R1, R2 : real);
begin
if D = 0
then R1 := (-B + sqrt(D)) / (2 * A)
else begin
R1 := (-B + sqrt(D)) / (2 * A);
R2 := (-B - sqrt(D)) / (2 * A);
end
end;
(d) procedure DisplayResult(D, R1, R2: real);
begin
if D > 0
then writeln(‘The equation has two real and unequal
roots: ’, R1:0:2,‘ and ’, R2:0:2)
else if D = 0
then write(‘The equation has two real and equal
roots: ’,
R1:0:2)
else writeln(‘The equation has no real root.’)
end;

Computer & Information Technology for HKCEE 20 © Pearson Education Asia Limited 2004
(Module A)
Chapter 6 Arrays, Text Files, Procedures and Functions Question Bank

(U03C06L02Q010)
Program Triangle receives the lengths of the two shorter sides of a right-angled triangle and then
finds out the length of the hypotenuse.

Sample Output 1:
Enter the lengths of the two shorter sides of a right-angled
triangle: 3.1 6.83
The length of the hypotenuse = 7.5

Sample output 2:
Enter the lengths of the two shorter sides of a right-angled
triangle: 4.5 7.2
The length of the hypotenuse = 8.49

program Triangle;
var Side1, Side2, Ans : real;

procedure GetInput (var Side1, Side2 : real);


begin
……
end;

procedure FindSide(side1, side2 : real; var Ans : real);


begin
……
end;

begin
……
end.

(a) Procedure GetInput asks users to enter the lengths of the two shorter sides of a right-angled
triangle. Then it outputs them to the call statement through variable parameters Side1 and
Side2. Consider the sample outputs above. Complete the procedure GetInput.
(b) Procedure FindSide receives the lengths of the two shorter sides from the call statement
through the parameter SideToFind, Side1 and Side2. Then it returns the length of the
hypotenuse to the procedure statement as output through the variable parameter Ans.
Complete the procedure FindSide.
(c) Complete the program body with the use of the procedures. (6 marks)

Computer & Information Technology for HKCEE 21 © Pearson Education Asia Limited 2004
(Module A)
Chapter 6 Arrays, Text Files, Procedures and Functions Question Bank

Answers
(a) procedure GetInput (var Side1, Side2 : real);
begin

write(‘Enter the lengths of the two shorter sides of a right-


angled triangle: ’);
readln(Side1, Side2)
end;
(b) procedure FindSide(side1, side2 : real; var Ans : real);
begin
Ans := sqrt(Side1 * Side1 + Side2 * Side2)
end;
(c) begin
GetInput(Side1, Side2);
FindSide(Side1, Side2, Ans);
writeln(‘The length of the hypotenuse = ’, Ans:0:2)
end.

Computer & Information Technology for HKCEE 22 © Pearson Education Asia Limited 2004
(Module A)
Chapter 6 Arrays, Text Files, Procedures and Functions Question Bank

(U03C06L02Q011)
Program Calculator receives 2 integers and an operator from users and then displays the result
on screen. If the operator is invalid or the divisor for a division is zero, an error message will be
displayed.

Sample Output 1:
Enter the first number: 4
Enter the second number: 5
Enter operator (+, -, *, /): *
4 * 5 = 20.00

Sample Output 2:
Enter the first number: 10
Enter the second number: 2
Enter operator (+, -, *, /): #
Error! Invalid operator ‘#’

Sample Output 3:
Enter the first number: 4
Enter the second number: 0
Enter operator (+, -, *, /): /
Error! Divisor cannot be ZERO!

The program heading, variables declaration, procedure headings and part of the program body are
given as follows.

program calculator;
var N1, N2 : integer; (*integer variables for expression*)
Ans : real; (*variable for storing answer*)
Operator : char; (*variable for storing operator*)
ErrorCode: integer; (*Code for indicating error*)

procedure Validation(Y: integer; Op: Char; var ECode: integer);


begin
……
end;

Computer & Information Technology for HKCEE 23 © Pearson Education Asia Limited 2004
(Module A)
Chapter 6 Arrays, Text Files, Procedures and Functions Question Bank

procedure Calculation(X, Y: integer; Op: char; var Ans: real);


begin
……
end;

begin
write(‘Enter the first number: ’);
readln(N1);
write(‘Enter the second number: ’);
readln(N2);
write(‘Enter operator (+, -, *, /): ’);
readln(Operator);
……
end.

(a) Procedure Validation checks the cases of 'invalid operator' or 'divided by zero error'. If
there is no error, then ECode will store value 0. If the operator is invalid, then ECode will
store value 1. If the operator is '/' and the divisor is 0, then ECode will store value 2.
Complete the procedure Validation.
(b) Procedure Calculation calculates the result. The result will be stored in variable Ans. It is
assumed that there is no error in the input. Complete the procedure Calculation.
(c) Complete the program body.
(13 marks)

Computer & Information Technology for HKCEE 24 © Pearson Education Asia Limited 2004
(Module A)
Chapter 6 Arrays, Text Files, Procedures and Functions Question Bank

Answers
(a) procedure Validation(Y: integer; Op: Char; var ECode: integer);
begin
ECode := 1;
if (Op = ‘+’) or (Op = ‘-’) or (Op = ‘*’)
then ECode := 0;
if Op = ‘/’
then if Y <> 0
then ECode := 0
else ECode := 2
end;

(b) procedure Calculation(X, Y: integer; Op: char; var Ans: real);


begin
case Op of
‘+’ : Ans := X + Y;
‘-’ : Ans := X - Y;
‘*’ : Ans := X * Y;
‘/’ : Ans := X / Y
end;
end;

(c) begin
write(‘Enter the first number: ’);
readln(N1);
write(‘Enter the second number: ’);
readln(N2);
write(‘Enter operator (+, -, *, /): ’);
readln(Operator);
Validation(N2, Operator, ErrorCode);
if ErrorCode = 0
then begin
Calculation(N1, N2, Operator, Ans);
writeln(N1, Operator, N2, ‘=’, Ans:0:2)
end
else if ErrorCode = 1
then write(‘Error! Invalid operator ''',Operator,
'''’)
else write(‘Error! Divisor cannot be ZERO!’)

Computer & Information Technology for HKCEE 25 © Pearson Education Asia Limited 2004
(Module A)
Chapter 6 Arrays, Text Files, Procedures and Functions Question Bank

end.

Computer & Information Technology for HKCEE 26 © Pearson Education Asia Limited 2004
(Module A)
Chapter 6 Arrays, Text Files, Procedures and Functions Question Bank

(U03C06L02Q012)
(a) Explain briefly the relationship between the terms character, field, record and file.
(b) In the following text file which stores the names and sex of 3 persons, state which are
fields, records and files.

Name Sex
Alex M
Joyce F
Simon M

(6 marks)
Answers
(a) A file usually contains records separated into lines. Each record consists of different data
items called fields, which are constructed by characters.
(b) The name and sex are 2 fields. Every line consists of a record and all the records form a
file.

(U03C06L02Q013)
Explain briefly the characteristics of text files. (2 marks)
Answers
Text files are sequential files that save data sequentially. All the data are stored as a sequence of
characters even though they belong to different lines.

(U03C06L02Q014)
What is the use of an end-of-line marker in a text file? (1 mark)
Answers
An end-of-line marker is an invisible mark which will not be displayed on the screen. It will only
cause the screen to start a new line to display data.

(U03C06L02Q015)
What is the use of an end-of-file marker in a text file? (1 mark)
Answers
An end-of-file marker is an invisible mark located at the end of a text file. It is used to denote the
end of a text file.

Computer & Information Technology for HKCEE 27 © Pearson Education Asia Limited 2004
(Module A)
Chapter 6 Arrays, Text Files, Procedures and Functions Question Bank

(U03C06L02Q016)
What is the difference between procedures writeln and write in outputting data to a text file?
(2 marks)
Answers
Both procedures writeln and write can save data to a text file. The only difference between
them is that each writeln statement will add an end-of-line marker after the output items listed in
the statement. However, write statement will not.

(U03C06L02Q017)
What is the difference between readln and read in reading data from a text file? (2 marks)
Answers
Both procedures readln and read can read data from a text file and store them in variables in the
variable list. After the execution of the readln statement, the pointer which points to the next
character read will be advanced to position of the character just after the next end-of-line marker,
even there are some data remained unread. However, the read statement only advances the pointer
to the character just after the read data.

(U03C06L02Q018)
In Pascal, several steps are required to create and manipulate a text file in the secondary storage.
(a) What is the first step of creating a text file?
(b) Write a Pascal statement to associate the file variable abc with a file in secondary storage with
the file name data.txt.
(c) Write a Pascal statement which prepares the file abc for writing.
(d) Write a Pascal statement which prepares the file abc for reading.
(e) Which Pascal statements are used to write data to the file abc?
(f) Which Pascal statements are used to read data from the file abc?
(6 marks)
Answers
(a) Declare a variable of type text as a file variable to identify the file.
(b) assign(abc, ‘data.txt’)
(c) rewrite(abc)
(d) reset(abc)
(e) writeln(abc,…) or write(abc, ….)
(f) readln(abc, … ) or read(abc, …. )

Computer & Information Technology for HKCEE 28 © Pearson Education Asia Limited 2004
(Module A)
Chapter 6 Arrays, Text Files, Procedures and Functions Question Bank

(U03C06L02Q019)
Consider the following variable declaration.
var Test : array [0..40] of integer;
(a) What is the name of the array?
(b) What is the lower bound index?
(c) What is the upper bound index?
(d) What is the data type of the array?
(e) How many elements are there in the array?
(f) Write down the identifier of the first element.
(g) Write down the identifier of the 5th element.
(h) Write down the identifier of the last element.
(8 marks)
Answers
(a) Test
(b) 0
(c) 40
(d) integer
(e) 41
(f) Test[0]
(g) Test[4]
(h) Test[40]

Computer & Information Technology for HKCEE 29 © Pearson Education Asia Limited 2004
(Module A)
Chapter 6 Arrays, Text Files, Procedures and Functions Question Bank

(U03C06L02Q020)
Refer to the program declaration below.
var
List: array [1..40] of real;
Total : real; (* Variable for storing the total value *)
Average : real; (* Variable for storing the average value *)
Maximum : real; (* Variable for storing the maximum value *)
I : integer; (* Variable for using as iteration index *)
(a) Write a program segment to find out and display the total value and average value of all the
elements in the array List.
(b) Write a program segment to find out and display the element with the maximum value in the
array List.
(10 marks)
Answers
(a) Total := 0;
for I := 1 to 40 do
Total := Total + List[I];
Average := Total / 40;
writeln(‘Totall: ’, Total);
writeln(‘Average: ’, Average);
(b) Maximum := List[1];
for I := 2 to 40 do
if List[I] > Maximum
then Maximum := List[I];
writeln(‘Maximum: ’, Maximum);

Computer & Information Technology for HKCEE 30 © Pearson Education Asia Limited 2004
(Module A)

Potrebbero piacerti anche