=========================preview======================
(Comp102)[2007](s)final~kytangaa^_10122.pdf
Back to COMP102 Login to download
======================================================
THE HONGKONG UNIVERSITY OF SCIENCE AND TECHNOLOGY
Department of Computer Science
COMP 102: Computer and Programming Fundamentals I
Spring 2007 Final Examination
Date: May, 29, 2007
This exam contains
9 questions in 14 pages. Please count the pages.
You have 3 hours to complete this exam.
Please identify yourself: Lecture/Lab section Name
Student ID
I have neither given nor received any unauthorized aid during this examination. The answers submitted are my own work.
I understand that sanctions will be imposed, if I am found to have violated the Universitys regulations governing academic integrity.
Signature:
1 [5 marks] Consider the following enum definition and C++ statements:
enum weekdays {sun,mon,tue,wed,thur,fri,sat};
weekdays day;
For each of the following statement, mark it with True if it is correct or False if it is incorrect. (Please notice that compile warning is treated as correct.)
a) day = fri;
Answer: ___________
b)day = 3;
Answer: ___________
c) day = (weekdays)1;
Answer: ___________
d)int i = day;
Answer: ___________
e) bool b = day;
Answer: ___________
2. [13 marks total] a) [6 marks] Consider the following function of binary search,
int binary_search(char data[], // input: array int size, // input: array size char value // input: value to find ) // output: index of value if found
{ // -1 if value is not found int lower, middle, upper; // indexes to the array lower = 0; upper = size - 1;
while (true) {
for (int i = lower; i <= upper; i++)
cout << data[i]<< ' ';
cout << endl;
middle = (lower + upper) / 2;
if (data[middle] == value)
return middle;
else if (lower >= upper)
return -1;
else if (value < data[middle])
upper = middle - 1;
else
lower = middle + 1;
}
}
Assuming char list[20]="12345678";
What are the outputs of the following function calls? (Please use . to indicate a white space )
cout << binary_search(list, 8, '7') << endl;
Answer:
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
cout << binary_search(list, 8, '0') << endl;
Answer:
b) [7 marks] What is the output of the following program? (Please use . to indicate a white space )
#include<iostream>
using namespace std;
int puzzle(int n){
cout << n << ' ';
if (n==1)
return 1;
if (n%2 == 0)
return 1 + puzzle(n/2 + 1);
if (n%3 == 0)
return 2 + puzzle(n/3);
else
return 10 + puzzle(n%10);
}
int main(){
cout << puzzle(18) << endl;
return 0;
}
Answer: ____________________________________________________________
3 [6 marks total]
a) [2 marks] What is the output of the following program?
#include<iostream>
using namespace std;
int main() {
con