Vous êtes sur la page 1sur 10

Chapter 8 Algorithms in Programming Question Bank

Chapter 8 Algorithms in Programming


Multiple Choice Questions

(U02C08L01Q001)
Which of the following statements about sorting is correct?
A. Arranging the data in a certain order.
B. Summing up all the data to get the total.
C. Finding the position of a particular data.
D. Finding the maximum and minimum values from the data.
Answer
A

(U02C08L01Q002)
How many passes are required to sort a list of 10 elements using bubble sort?
A. 1
B. 5
C. 9
D. 10
Answer
C

(U02C08L01Q003)
How many comparisons have to be carried out in order to sort a list of 10 elements using bubble
sort?
A. 10
B. 45
C. 90
D. 100
Answer
B

Computer & Information Technology for HKCEE 1 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 8 Algorithms in Programming Question Bank

(U02C08L01Q004)
What is the maximum number of swaps which have to be carried out in order to sort a list of 10
elements using bubble sort?
A. 10
B. 30
C. 40
D. 45
Answer
D

(U02C08L01Q005)
Which of the following statements about bubble sort are correct?
(1) Bubble sort is a kind of sorting method.
(2) Implementation of bubble sort using C is easy.
(3) It cannot be used to sort a list of string elements.
A. (1) and (2) only
B. (1) and (3) only
C. (2) and (3) only
D. (1), (2) and (3)
Answer
A

(U02C08L01Q006)
Which of the following statements about bubble sort is/are correct?
(1) It is not a very efficient method of sorting because too many comparisons and swaps are
involved.
(2) The performance of the process is the best if the elements are nearly sorted.
(3) The number of swapping depends on the distribution of the data in the list.
A. (1) only
B. (2) only
C. (1) and (2) only
D. (1), (2) and (3)
Answer
D

Computer & Information Technology for HKCEE 2 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 8 Algorithms in Programming Question Bank

(U02C08L01Q007)
Which of the following statements about linear search are INCORRECT?
(1) Linear search is always faster than binary search.
(2) The whole list of data items should be sorted before searching.
(3) The whole list should be searched in order to confirm a data item is not present.
A. (1) and (2) only
B. (1) and (3) only
C. (2) and (3) only
D. (1), (2) and (3)
Answer
A

(U02C08L01Q008)
Linear search is also known as
A. bubble search.
B. sequential search.
C. quick search.
D. binary search.
Answer
B

(U02C08L01Q009)
Which of the following statements about merging is correct?
A. It is the process of combining two or more lists of sorted data into a single sorted list.
B. It is the process of appending one sorted list of data to the end of another sorted list of data.
C. It is the process of finding a common data item from two sorted lists of data items.
D. It is the process of finding the maximum and minimum data items from two or more lists of
sorted data.
Answer
A

Computer & Information Technology for HKCEE 3 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 8 Algorithms in Programming Question Bank

(U02C08L01Q010)
The following is the sequence of data items stored in a file.
1, 4, 7, 8, 12, 13, 15, 19, 23
In order to look for 8 using binary search, the sequence of the data items being compared during the
search should be
A. 1, 4, 7, 8.
B. 23, 19, 15, 13, 12, 8.
C. 12, 15, 13, 8.
D. 12, 4, 7, 8.
Answer
D

Computer & Information Technology for HKCEE 4 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 8 Algorithms in Programming Question Bank

Conventional Questions

(U02C08L02Q001)
The following is a list of integers.
17, 4, 8, 2, 15
(a) What is the maximum number of passes required to sort the list using bubble sort?
(b) Write down the contents of the list after each pass.
(4 marks)
Answers
(a) 4 passes
(b) Pass 1: 4, 8, 2, 15, 17
Pass 2: 4, 2, 8, 15, 17
Pass 3: 2, 4, 8, 15, 17

(U02C08L02Q002)
List THREE advantages and TWO disadvantages of bubble sort? (5 marks)
Answers
Advantages:
1. Easy to understand.
2. Easy to implement.
3. Efficient if the list of data items is nearly sorted in order.
Disadvantages:
1. A lot of comparisons and swaps are involved.
2. Inefficient for large amount of data to be sorted.

(U02C08L02Q003)
A student writes a program to implement the bubble sort, when he wants to swap two integer data
items A[i] and A[i+1], he writes the following program segment:
A[i] = A[i+1];
A[i+1] = A[i];
Is the above program segment correct? If not, amend the above program.
(4 marks)
Answers
Not correct. We should define another variable called temp and rewrite the program as follows:
temp = A[i];
A[i] = A[i+1];
A[i+1] = temp;

Computer & Information Technology for HKCEE 5 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 8 Algorithms in Programming Question Bank

(U02C08L02Q004)
Suppose the following function is written to implement the bubble sort algorithm, complete the
function.
#include <stdio.h>
void bubble_sort(int[], int);
int main(void){
int index, data[10]={
10,3,4,8,2,5,9,7,1,6};
bubble_sort(data,10);
for(index=0;index<10;index++)
printf("%d ", data[index]);
printf("\n");
return 0;
}
(9 marks)
Answers
Suggested program codes.
void bubble_sort(int data[], int n){
int i,j,temp;
for (i=n-1;i>0;i--){
for (j=1;j<=i;j++){
if (data[j-1]>data[j]){
temp=data[j-1];
data[j-1]=data[j];
data[j]=temp;
}
}
}
}

Computer & Information Technology for HKCEE 6 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 8 Algorithms in Programming Question Bank

(U02C08L02Q005)
Suppose a list of 10 elements
100, 50, 85, 40, 35, 45, 20, 5, 80, 55
are stored in an array A such that A[1]= 100, A[2]= 50, …. and A[10]= 55.
(a) A linear search is used to find the index of the element 5, how many comparisons should be
carried out before it is located?
(b) Suppose you want to find the index of an element using linear search. But that element is not
found in the list, how many comparisons would be carried out in this case?
(c) Suppose the following function prototype defined for you.
int linearSearch(int data[], int last, int target);
Write a C program segment to find the index of the list element whose value is the same as the
stored value in the variable data using the linear search. Your program segment should return 1
if found and 0 if not found.
(7 marks)
Answers
(a) 8
(b) 10
(c) Suggested program segment:
int linearSearch(int data[], int target, int last) {
int looker=0;
while(looker<last&&target != data[looker])
looker++;
return (target==data[looker]);
}

Computer & Information Technology for HKCEE 7 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 8 Algorithms in Programming Question Bank

(U02C08L02Q006)
Binary search is a search technique that locates the target in a sorted list by repeatedly dividing the
list into 2 halves and searches the half that may probably contain the target until the list becomes
empty or the target is found.
(a) Before binary search can be carried out on a list of numbers, one process has to be carried out
first, what is that process?
(b) Consider the following list of numbers.
3, 5, 8, 9, 11, 15, 19, 24, 25
Suppose we want to search a target value 8 using binary search.
(i) At the beginning, what are the indices of the first and the last elements of the list? What is
the value of the middle element?
(ii) In the second phase of the binary search, what are the indices of the first and the last
elements of the list? What is the value of the middle element?
(iii) In the third phase of the binary search, what are the indices of the first and the last
element of the list? What is the value of the middle element?
(10 marks)
Answers
(a) The list of numbers should be sorted into ascending or descending order before binary search
can be carried out.
(b) (i) The first element’s index is 1 and the last element’s index is 9.
The value of the middle element is 11.
(ii) The first element’s index is 1 and the last element’s index is 4.
The value of the middle element is 5.
(iii) The first element’s index is 3 and the last element’s index is 4.
The value of the middle element is 8.

Computer & Information Technology for HKCEE 8 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 8 Algorithms in Programming Question Bank

(U02C08L02Q007)
Consider the following C program function prototype.
int binarySearch(int data[], int target, int end);
Complete the binarySearch function so that it can carry out binary search to find the index of
the element of data whose value is stored in target. The function returns 1 if the target is
found and 0 if not found.
(11 marks)
Answers
Suggested function:
int binarySearch(int data[], int target, int end){
int first, mid, last;
first=0;
last=end;
while(first<=last){
mid=(first+last)/2;
if(target>data[mid])
first=mid+1;
else if(target<data[mid])
last=mid-1;
else
first=last+1;
}
return target==data[mid];
}

Computer & Information Technology for HKCEE 9 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 8 Algorithms in Programming Question Bank

(U02C08L02Q008)
Consider the declaration
int intarray[]={9,5,2,3,8,7,4};
(a) How would the cell that contains the 7 be referenced?
(b) What is the array index associated with the cell containing the 9?
(c) Write the necessary declarations and statements to assign the sum of the contents of the third
and the fifth cells in the array to a variable named sum.
(d) How would the declaration above differ from the one below?
int intarray[]={9,5,2,3,8,7,4};
Which declaration is better in a stylistic sense? When might the first declaration be used
instead of the second?
(7 marks)
Answers
(a) intarray[5];
(b) 0
(c) int sum;
sum=intarray[2] + intarray[4];
(d) The two declarations have the same effect. The second has more information for the reader of
the program. The first form might be used when there are many initializers; the compiler is less
likely to count incorrectly than a programmer.

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

Vous aimerez peut-être aussi