=========================preview======================
(COMP102)final_2001fall_Q_ans.pdf
Back to COMP102 Login to download
======================================================
THE HONG KONG UNIVERSITY OF SCIENCE AND TECHNOLOGY
Department of Computer Science
COMP 102: Computer and Programming Fundamentals I
Fall 2001 Final Examination
Version C
Date: 22 December 2001 Time: 10:00-1:00 pm
Venue: Room 1403
This exam contains 10 questions in 16 pages. Please count the pages.
You have 3 hours to complete this exam.
Problem Your points Max points Problem Your points Max points
1 6 6 5
2 10 7 5
3 10 8 10
4 15 9 10
5 10 10 19
Subtotal 51 Subtotal 49
Your total points 100
Please identify yourself: Lecture/Lab section Name
Student ID
Signature
1. (6 points) For each of the follow C++ code segments, mark it with CORRECT if it is correct
and WRONG if it is NOT correct. You should consider both syntax and logical correctness.
_Correct_a)char message[ ] = "Hello";
__Wrong__b)void fn (const int x[ ] ) { if ( x[0] < 0) x[0] = 0; } __Wrong__c)char message[6]; message = "Hello"; _Correct_d)int array[] = {1, 2, 3};
__Wrong__e)int x[5]; x[5] = 0; __Wrong___f)int array[4] ={ 0, 1, 2, 3, 4 };
2. (10 points) The following program shows the result of a battle between the Taliban and Pashtun
tribesmen in Afghanistan. The function call count(taliban) uses enumeration constants as array indices to store the values in the taliban array. What will the report function print on the screen?
#include <iostream.h>
#include <string.h>
enum {totalfighters, fighters, prisoners, dead, totaldead};
const int FIGHTERS = 500;
void count(int army[]);
// This function produces the values {250, 55, 30, 140, 205} when it is
// called with the taliban array.
// The values are {450, 120, 45, 60, 110} for the pashtun array.
void report(int army[], char name[], char city[]){ cout << "Current " << name << " dead: " << army[dead] << endl; cout << "Current " << name << " prisoners: "
<< army[prisoners] << endl; cout << "Remaining " << name << " fighters in " << city << ": " << army[fighters] << endl; cout << "The " << name << " has lost " << army[totaldead] << " fighters." << endl; cout << "Total remaining " << name << " fighters: "
<< army[totalfighters] << endl << endl; } int main () {
int taliban[5] = {FIGHTERS, 0, 0, 0, 0};
int pashtun[5] = {FIGHTERS, 0, 0, 0, 0};
count(taliban);
report(taliban, "Taliban", "Kandahar");
count(pashtun);
pashtun[totalfighters] += taliban[prisoners];
report(pashtun, "Pashtun", "Kandahar");
return 0;
}
Answer: Complete the following output produced by the reportfunction:
Current Taliban dead: __140_____
Current Taliban prisoners: ___30_____
Remaining Taliban fighters in Kandahar: ___55_____
The Taliban has lost ___205___ fighters.
Total remaining Taliban fighters: __250____
Current Pashtun dead: ___60_____
Current Pashtun prisoners: ___45_____
Remaining Pashtun fighters in Kandahar: __120_____
The Pashtun has lost __110_____ fighters.
Total remaining Pashtun fighters: ___480____
3. (10 points