Sie sind auf Seite 1von 5

Chapter 7 Debugging and Testing Question Bank

Chapter 7 Debugging and Testing


Multiple Choice Questions

(U02C07L01Q001)
What could the following error message mean?
Error abc.c 6:Statement missing ; in function main
A. There is an error in line 6.
B. There is an error in line 5.
C. There is an error in the processor.
D. A and B are probably true.
Answer
D

(U02C07L01Q002)
Which of the following statements have syntax errors? (x, y and z are integer variables.)
(1) x; y; z = 0;
(2) x = y = z = 0;
(3) x = 0; y = 1; z = 2;
A. (1) and (2) only
B. (1) and (3) only
C. (2) and (3) only
D. (1), (2) and (3)
Answer
A

(U02C07L01Q003)
Which of the following statements and the corresponding results is/are correct?
Statements Results
(1) printf(“%d”, 28/5); 5
(2) printf(“%d”, 28%5); 5
(3) printf(“%f”, 28%5); 5.6
A. (1) only
B. (1) and (2) only
C. (2) and (3) only
D. (1), (2) and (3)
Answer
A

Computer & Information Technology for HKCEE 1 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 7 Debugging and Testing Question Bank

Conventional Questions

(U02C07L02Q001)
Complete the dry run table of the following program segments. (Assume X = -1, Y = 0 and Z = 2.)

(a) (b) (c) (d) (e)


...... ...... ...... ...... ......

Z = X; X = X + 2; X = X+Y; X = Y+Z; X = Z-Y;


X = Y; Y = Y * 3; Y = Y+Z; Y = X+Z; Y = X-Z;
Y = Z; Z = X + Y; Z = Z+X; Z = X+Y; Z = Y-X;
...... ...... ...... ...... ......

(a) (b) (c) (d) (e)


X Y Z X Y Z X Y Z X Y Z X Y Z
-1 0 2 -1 0 2 -1 0 2 -1 0 2 -1 0 2

(15 marks)
Answers
(a) (b) (c) (d) (e)
X Y Z X Y Z X Y Z X Y Z X Y Z
-1 0 2 -1 0 2 -1 0 2 -1 0 2 -1 0 2
-1 1 -1 2 2
0 0 2 4 0
-1 1 1 6 -2

Computer & Information Technology for HKCEE 2 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 7 Debugging and Testing Question Bank

(U02C07L02Q002)
A program changes the input amount of money to $5, $1, 50-cent and 10-cent coins such that the
number of coins is the least.

Sample output:
Enter an amount of money ($0.1-$49.9):$22.8
$22.8 can changed to :
$5 coin × 4
$1 coin × 2
50-cent coin × 1
10-cent coin × 3

A student writes the following program to do the above job.

1 #include <stdio.h>
2 void main( ){
3 int Amount;
4 printf("Enter an amount of money ($0.1-$49.9): $");
5 scanf("%f", &Amount);
6 printf("$%.1f can changed to:\n", Amount);
7 printf("$5 coin x %d\n", Amount/5);
8 printf("$1 coin x %d\n", Amount%5);
9 printf("50-cent coin x %d\n", (Amount * 10)/5);
10 printf("10-cent coin x %d", (Amount * 10)%5);
11 }

However, there are several errors in the program. Find out the errors and correct them.
(4 marks)
Answers
Line Corrected statements
no.
3 float Amount;
7 printf("$5 coin x %d\n", (int)floor(Amount/5));
8 printf("$1 coin x %d\n", (int)Amount%5);
9 printf("50-cent coin x %d\n",
(int)(((Amount-floor(Amount))*10)/5));
10 printf("10-cent coin x %d", (int)((Amount-
floor(Amount))*10)%5);

Computer & Information Technology for HKCEE 3 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 7 Debugging and Testing Question Bank

(U02C07L02Q003)
There are mistakes in every line of the program. Identify and correct them.

Line Number Statement


1. #include (stdio.h)
2. #define PI = 3.14;
3. void main{
4. int radius
5. float area
6. printf(Enter the radius: );
7. scanf("%d", radius);
8. area = radius * radius * PI;
9. printf(The area is %7.2f.\n, area);
10.

(10 marks)
Answers
Line Number Statement
1 #include <stdio.h>
2 #define PI 3.14
3 void main ( ){
4 int radius;
5 float area;
6 printf("Enter the radius: ");
7 scanf("%d", &radius);
8 area = (float)radius * radius * PI;
9 printf("The area is %7.2f.\n", area);
10 }

Computer & Information Technology for HKCEE 4 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 7 Debugging and Testing Question Bank

(U02C07L02Q004)
The following program is designed to input values for an array of three int and then display the
contents of the array. It does not work correctly. Find the bug in the program.
1 #include <stdio.h>
2 int main(void){
3 int intarray[3], count;
4 printf("intarray %x, &count %x.\n", intarray, &count);
5 for (count=0; count<=3; count++){
6 printf("Cell #%d: ",count);
7 scanf("%d", intarray + count);
8 }
9 printf("The contents of the array are: ");
10 for (count=0; count<=3; count++)
11 printf("%d\n", intarray[count]);
12 return 0;
13 }

(2 marks)
Answers
Lines 5 and 10 should be
for (count=0; count<3; count++){
and
for (count=0; count<3; count++)
respectively.

Computer & Information Technology for HKCEE 5 © Pearson Education Asia Limited 2004
(Module A1)

Das könnte Ihnen auch gefallen