You are on page 1of 12

Chapter 8 Algorithms in Programming Question Bank

Chapter 8 Algorithms in Programming


Multiple Choice Questions

(U03C08L01Q001)
What is the meaning of sorting?
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

(U03C08L01Q002)
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

(U03C08L01Q003)
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 A2)
Chapter 8 Algorithms in Programming Question Bank

(U03C08L01Q004)
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

(U03C08L01Q005)
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 Pascal is easy.
(3) It cannot be used to sort a list of string elements.
A. (1) and (2) only
B. (2) and (3) only
C. (1) and (3) only
D. (1), (2) and (3)
Answer
A

(U03C08L01Q006)
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 A2)
Chapter 8 Algorithms in Programming Question Bank

(U03C08L01Q007)
Which of the following statements about linear search are INCORRECT?
(1) It 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. (2) and (3) only
C. (1) and (3) only
D. (1), (2) and (3)
Answer
A

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

(U03C08L01Q009)
What is the meaning of merging?
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 A2)
Chapter 8 Algorithms in Programming Question Bank

(U03C08L01Q010)
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 A2)
Chapter 8 Algorithms in Programming Question Bank

Conventional Questions

(U03C08L02Q001)
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

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

(U03C08L02Q003)
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, correct it. (5 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 A2)
Chapter 8 Algorithms in Programming Question Bank

(U03C08L02Q004)
Suppose the following procedure Bubblesort is written to implement the bubble sort algorithm,
finish the procedure.

Program sorting;
const
Size = 10;
var
List : array[1..Size] of integer;
Procedure Bubblesort;
var
Pass, i, temp : integer;
begin
:
:
:
end. (6 marks)
Answers
for Pass := 1 to Size - 1 do
for i := 1 to Size - Pass do
if List[i] > List[i+1]
then begin
temp := List[i];
List[i] := List[i+1];
List[i+1] := temp;
end

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

(U03C08L02Q005)
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 declarations are defined for you.

var
i, data : integer;
A : array[1..10] of integer;
found : Boolean;

Write a Pascal 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
produce a suitable message if the stored value in data is not found in the list. (11 marks)

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

Answers
(a) 8
(b) 10
(c) begin
i := 1;
found := false;
repeat
if A[i] = data
then found := true
else i := i + 1
until (i > 10) or found;
if found
then writeln(‘The index of the data is ‘, i)
else writeln(‘Data is not found in the list’)
end;
{Alternative answer}
begin
i := 1;
found := false;
while (i <= 10) and not found do
if A[i]= data
then found := true
else i := i + 1;
if found
then writeln(‘The index of the data is ‘, i )
else writeln(‘Data is not found in the list’)
end;

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

(U03C08L02Q006)
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. Name the 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?
(7 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 9 © Pearson Education Asia Limited 2004
(Module A2)
Chapter 8 Algorithms in Programming Question Bank

(U03C08L02Q007)
Consider the following Pascal program declarations.
var
List : array[1..100] of integer;
Procedure BinarySearch;
var
First, Last, Middle, Target : integer;
Found: Boolean;
Complete the procedure BinarySearch so that it can carry out binary search to find the index of
the element of List whose value is stored in Target. (13 marks)
Answers
begin
write(‘Enter the data to be searched: ‘);
readln(Target);
First := 1;
Last := 100;
Found := FALSE;
while (First <= Last) and not found do
begin
Middle := (First + Last) div 2;
if Target = List[Middle]
then found := TRUE
else if Target > List[Middle]
then First := Middle + 1
else Last := Middle – 1
end;
if Found
then writeln(‘The index of the required data is ‘, Middle)
else writeln(‘The data is not found!’)
end;

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

(U03C08L02Q008)
Merging is the process of combining two or more lists of sorted data into a single sorted list.
(a) Suppose the following arrays A and B are two arrays with element type integer and sorted in
ascending order. These two arrays are merged and the results are stored in another array C.
Write out the contents of array C.
Array A: 1, 3, 4, 6, 8, 12, 15
Array B: 2, 7, 14
(b) Suppose a Pascal program has the following declarations.
Const
sizeA = 5;
sizeB = 3;
sizeC = sizeA + sizeB;
var
ListA : array[1.. sizeA] of integer;
ListB : array[1.. sizeB] of integer;
ListC : array[1.. sizeC] of integer;
Procedure MergeSort;
var
A, B, C, i : integer;
begin
A := 1;
B := 1;
C := 0;
:
:
end;
The procedure MergeSort is written to merge the arrays in ListA and ListB and store the
results in ListC, write down the program segment of MergeSort until List A or List B is
empty.
(c) Write down the program segment of MergeSort to copy the remaining part of List A or List B
to List C if either one of them is not empty. (14 marks)

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

Answers
(a) Array C: 1, 2, 3, 4, 6, 7, 8, 12, 14, 15
(b) while (A <= SizeA) and (B <= SizeB) do
if ListA[A] < ListB[B]
then begin
C := C + 1;
ListC[C] := ListA[A];
A := A + 1
end
else begin
C := C + 1;
ListC[C] := ListB[B];
B := B + 1
end;
(c) if A <= SizeA
then for i := A to SizeA do
begin
C := C + 1;
ListC[C] := ListA[i]
end
else for i := B to SizeB do
begin
C := C + 1;
ListC[C] := ListB[i]
end;

Computer & Information Technology for HKCEE 12 © Pearson Education Asia Limited 2004
(Module A2)

You might also like