2004-CE COMP STUD
Candidate Number
PAPER I (SECT B & C)
Centre Number HONG KONG EXAMINATION AND ASSESSMENT AUTHORITY HONG KONG CERTIFICATE OF EDUCATION EXAMINATION 2004
Seat Number
COMPUTER STUDIES PAPER 1 (SECTIONS B & C) Question-Answer Book 8:30 am – 10:30 am (2 hours) This paper must be answered in English
Marker’s Use Only Marker No.
Examiner’s Use Only Examiner No.
Q.6 Q.7 Instructions: 1.
Write your Candidate Number, Centre Number and Seat Number in the spaces provided.
2.
Answer all questions.
Q.8 Q.9 Q.10 Q.10
0
1
2
3
4
0
1
Total 3.
Write your answers in the spaces provided in this question-answer book.
4.
Supplementary answer sheets will be supplied upon request. Write your Candidate Number on each sheet and fasten them with string inside this book.
Checker’s Use Only Checker No. Total
2004-CE-COMP STUD 1B & C-1 (C Version)
2
3
4
Section B (40 marks) Answer ALL questions in this section. 6.
A school stores the telephone numbers of students in a text file named ‘telist.txt’. In the text file, each line consists of 14 characters representing the classname, class number and telephone number of a student, which are separated by space characters as follows: Class name
Class number
Space character
Telephone
Space character
Part of the text file is shown below: 1B 1C 1C 1D 1E
12 23 12 04 23
23516718 24567171 25167717 24516818 21346765 . . . Mr. Chan writes a program to modify the telephone number of a certain student. The program should update the text file and produce the output below on the VDU. (In this output, all the data following a colon is entered by the user through the keyboard. All other items are output from the program.) Sample output 1 Enter class name : 1D Enter class no.: 04 Enter new telephone no.: 26743219 Record has been modified successfully. Sample output 2 Enter class name : 1E Enter class no.: 16 No such record! Mr. Chan’s program is shown below: Line Number 100 110 120 130 140 150 160 170 180 190 200 210 220
Program Statement #include <stdio.h> int main() { FILE* datafile; char data[200][80]; char inpno[80], inpclass[80], oneline[80], newno[80]; char class[3], classno[3]; int position, i, count; datafile = fopen("telist.txt", "r"); printf("Enter class name : "); scanf("%s", inpclass); printf("Enter class no.: "); scanf("%s", inpno);
2004-CE-COMP STUD 1B & C-2 (C Version)
230 240 250 260 270 280 290 300 310 320 330 340 350 360 370 380 390 400 410 420 430 440 450 460 470 480 490 500
count = 0; position = -1; while ( !feof(datafile) ) { fgets(oneline, 80, datafile); strcpy(data[count], oneline); strncpy(class, data[count], 2); class[2] = '\0'; strncpy(classno, data[count] + 3, 5); classno[2] = '\0'; if ( strcmp(class, inpclass) == 0 || strcmp(classno, inpno) == 0 ) position = count; count++; } fclose(datafile); if ( position == -1 ) { printf("Enter new telephone no.: "); scanf("%s", newno); sprintf(data[position], "%s\n", inpclass, inpno, newno); datafile = fopen("telist.txt", "r"); for ( i = 1; i < count; i++ ) fprintf(datafile, "%s", data[count]); fclose(datafile); } printf("No such record!\n"); printf("Record has been modified successfully.\n"); }
However, there are several mistakes in the program after line 170. Fill in the following table to show the location of each mistake and the related corrected statement. Line Number
Corrected Statement
(10 marks)
2004-CE-COMP STUD 1B & C-3 (C Version)
7.
Tom writes a program to encrypt a text. This program handles capital letters only, as shown below: #include <stdio.h> int main() { const int shift = 13; char InStr[80]; int i, tmp;
} (a)
printf("Please input the text : "); scanf("%s", InStr); printf("The coded message is : "); for ( i = 0; i < strlen(InStr); i++ ) if ( InStr[i] >= 'A' && InStr[i] <= 'Z' ) { tmp = (int) InStr[i] + shift; if ( tmp > 90 ) tmp = tmp - 26; printf("%c", (char) tmp); } else printf("%c", InStr[i]); printf("\n"); Write down the output of the program if the input is: (i)
BAR
(ii)
ONE (2 marks)
(b)
Describe briefly how to decode the encrypted text by using the above program.
(1 mark) Mary has Tom’s encrypted text. She is told that Tom’s encrypted text can be cracked by using the following steps: Step 1:
Count the number of occurrences of each letter in the encrypted text.
Step 2:
In general, statistics show that ‘E’ is the most commonly used letter in a passage. Thus, Mary replaces the most frequent letter in the encrypted text by ‘E’ and then the next letter in the alphabet with ‘F’ and so on. For example, if the most frequent letter in the encrypted text is ‘Y’, then ‘Y’will be replaced by ‘E’, then ‘Z’ by ‘F’, ‘A’ by ‘G’, ‘B’ by ‘H’, …, etc.
2004-CE-COMP STUD 1B & C-4 (C Version)
(c)
Mary writes a program to count the number of occurrences of each letter in a text file specified by the user and also to find the most frequent letter. Write the contents of the boxes to complete the program below. int main() { char filename[80], InStr[80]; A Infile; int Freq[26]; int c, max, i; for ( c = 0; c < 26; c++ ) Freq[c] = 0; printf("Please input the name of the file to be processed : "); scanf("%s", filename); Infile = fopen( B , "r"); while ( !feof(Infile) ) { fgets(InStr, 80, Infile); for ( i = 0; i < C ; i++ ) if ( InStr[i] >= 'A' && InStr[i] <= 'Z' ) Freq[InStr[i] - 'A'] = D ; } fclose(Infile); max = 0; i = Freq[max]; for ( c = 1; c < 26; c++ ) if ( E ) max = c; printf("The most frequent letter is %c", (char) max + 'A'); } A
B
D
E
C (5 marks)
(d)
State two weaknesses of using this cracking method on a passage that contains capital letters only.
(2 marks)
2004-CE-COMP STUD 1B & C-5 (C Version)
8.
The algorithm below is to calculate the expression:
1 ( f 1 + 2 f 2 + 2 f 3 + ... + 2 f n −1 + f n ) 2
Step 1:
Declare i and n to be integer variables. Declare f and result to be float variables. Step 2: Initialize i to be 1. Initialize result to be 0. Step 3: Read in an integer into variable n. Step 4: As long as the value of variable i is less than or equal to the value of variable n, do Steps 5 to 7. Step 5: Read in a real number into variable f. Step 6: If the value of variable i is not equal to 1 and not equal to the value of variable n, then increase the value of variable result by twice of the value of variable f, else increase the value of variable result by the value of variable f. Step 7: Increase the value of variable i by 1. Step 8: Divide the value of variable result by 2. Step 9: Display the value of variable result. Convert the algorithm into a C program. The value of n and fi (i = 1, 2, 3, …n) will be entered in succession through the keyboard during the execution of the program.
(10 marks)
2004-CE-COMP STUD 1B & C-6 (C Version)
9.
A palindrome is a word or a phrase that spells the same forwards and backwards when spaces are not counted. Two examples of palindromes are shown below: Rats live on no evil star (1) Was it a car or a cat I saw (2) Peter writes a program to check whether a string is a palindrome or not. The following shows sample outputs of the program: (In this output, all the data following a colon is entered by the user through the keyboard. All other items are output from the program.) Sample Output 1 Please input a string: Was it a car or a cat I saw It is a palindrome. Sample Output 2 Please input a string: no pain no gain It is not a palindrome. Peter has already written the declaration and main body of the program. Some procedures are missing, as shown below: #include <stdio.h> #define true 1 #define false 0 char inp[80], nospace[80]; Missing procedures here int main() { int result; printf("Please input a string: "); gets(inp); RemoveSp(); Upper(); result = true; Test(&result); if ( result ) printf("It is a palindrome.\n"); else printf("It is not a palindrome.\n"); } The following indicates the description of each identifier used in this program: Identifier inp nospace result i, j
Description a character array to store the input string a character array to store the string with space characters removed an integer variable to store the result of the checking (true or false). local integer variables used as a counter in procedures RemoveSp, Upper and Test
2004-CE-COMP STUD 1B & C-7 (C Version)
You are not allowed to add any new variables in answering the question. Otherwise no marks will be scored. (a)
Write procedure RemoveSp to remove space characters in the string stored in character array inp and store the string into character array nospace. void RemoveSp() { int i, j;
} (3 marks) (b)
Write procedure Upper to change the letters in character array nospace into upper case. void Upper() { int i;
} (3 marks)
2004-CE-COMP STUD 1B & C-8 (C Version)
(c)
Write procedure Test to check whether the string in character array nospace is a palindrome or not. void Test(int* result) {
} (4 marks)
END OF SECTION B
2004-CE-COMP STUD 1B & C-9 (C Version)
Section C (16 marks plus a maximum of 4 marks for effective communication) 10.
Mr. Lee, the department head of the registry of ABC University, wants to computerize the course registration system. He creates two database files, Personal and CourseReg, to store the personal details of students and the subjects that students take, respectively. Personal Field Name Name SID Sex DOB TelNo Addr Email CourseReg Field Name SID SubCode1 SubCode2 SubCode3 SubCode4 SubCode5
Field Type character character character character character character character
Remark Student name Student number Sex Date of birth Telephone number Home address Email address
Field Type character character character character character character
Remark Student number Subject code of the first subject Subject code of the second subject Subject code of the third subject Subject code of the fourth subject Subject code of the fifth subject
An example of CourseReg is shown below: SID SubCode1 SubCode2 SubCode3 SubCode4 SubCode5 127768 012 124 025 453860 105 124 025 023 028 049532 012 110 011 039 041 454890 025 033 011 023 . . . . . . . . . . . . . . . . . . Some fields of subject codes will be left empty if the number of subjects taken is less than five. (a)
(i)
Which field should determine a record uniquely?
(ii)
Why should the data type of SID be character instead of numeric?
(iii)
The subject code is used instead of the subject name in CourseReg. Give two advantages.
(4 marks)
2004-CE-COMP STUD 1B & C-10 (C Version)
Mr. Lee modifies the database structure of CourseReg as follows: CourseReg Field Name SID SubCode
Field Type character character
Remark Student number Subject code
An example of CourseReg is shown below: SID 127768 127768 127768 453860 453860 453860 453860 453860 . . . (b)
SubCode 012 124 025 105 124 025 023 028 . . .
Describe how such a modification benefits the system.
(2 marks) ABC University (Registration Sheet)
Name: Student Number: Sex: Email Address: Subject Code 1: Subject Code 2: Subject Code 3: Subject Code 4: Subject Code 5: During registration, each student has to fill in a registration sheet as shown above and return it to the registration office. Jack, a clerk at the University, will input the data in a computer. Mr. Lee needs to eliminate all errors in the data entry process. Hence, Jack plans to type the data twice in the computer but Mr. Lee does not think this is a good idea. (c)
(i)
Give two disadvantages of Jack’s plan.
2004-CE-COMP STUD 1B & C-11 (C Version)
(ii)
Mr. Lee wants to improve the data entry process by (1) redesigning the registration sheet for students to write, and (2) automating the input process. Briefly describe how to do both of these.
(6 marks) (d)
The input process is now considered error-free. However, the computer will still generate an error report on the data. Give two possible errors.
(2 marks) (e)
Students are required to verify their personal particulars and course registration information in the system. Jack plans to put all students’ information including all the contents of Personal and CourseReg clearly, on the notice board outside the office so that students can check against it. Mr. Lee disagrees with Jack. Why? What should Jack do instead?
(2 marks)
END OF SECTION C END OF PAPER
2004-CE-COMP STUD 1B & C-12 (C Version)
A Partial Character List for ASCII Character 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I
ASCII 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
Character J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c
ASCII 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
Character d e f g h I j k l m n o p q r s t u v w x y z { | }
ASCII 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
List of Operators and Reserved Words (C) #include, +, -, *, /, ++, --, +=, -=, *=, /=, %=, ==, %, >, <, =, >=, <=, !=, &&, ||, !, sqrt, rand, abs, strcat, strncat, strlen, atoi, strcpy, strncpy, const, void, return, int, float, char, \0, strcmp, strncmp, true, false, FILE, main, /*…*/, if…else, for, while, do…while, switch…case…break, break, continue, scanf, printf (%d, %f, %c, %s), \n, \t, fopen, getc, fgets, putc, fputs, EOF, fclose
2004-CE-COMP STUD 1B & C-13 (C Version)