=========================preview======================
(comp102)[2007](sum)mid~PPSpider^_10121.pdf
Back to COMP102 Login to download
======================================================
Department of Computer Science and Engineering, HKUST
Summer Programming Course 2007
Midterm Examination

Date: July 27, 2007 Time: :14:00 C 16:00
Personal Information:
Name Student ID Email Department
(summerXX) (CS/CPEG)



Instructions:
1) All questions must be answered.
2) This is a closed-book, closed-note exam.
3) Calculator and any other electronic devices are disallowed in this exam.
4) Your answer will be graded on clarity, correctness and precision.
5) Answer the questions in the space provided. Rough work should be crossed out.
6) Your answer should base on the g++ run on the Linux machines in CS Lab 2.

Marks:
Question SubTotal Question SubTotal
1 / 5 6 / 10
2 / 5 7 / 10
3 / 5 8 / 10
4 / 5 9 / 5
5 / 10 10 / 35
Total / 100


With Solution

Question 1: What is the output of the following program? (5 marks)
#include <iostream> using namespace std; int main() {
int n = 0;
while (n == 8) {
n++;
if (n!=8)

n-1; }cout << "n = " << n << endl; return 0;
}
Answer to Question 1:
n = 0
Question 2: What is the resulting TYPE of the following expression if A, B are of type int and C, D are of type double? (5 marks)
A + (B - C) + D

Answer to Question 2:
The resulting type is double.
For questions 3-9, what is the output of the program, if there is no compile / linking error? If there are any compile / linking errors, describe all of them briefly (1-2 sentences for each problem will be sufficient). If there are no compile/linking error but runtime error exists, write down the output until the runtime error occurs and then indicate the reason of the runtime error. Note: You should assume all required standard libraries are included properly.
Question 3: (5 marks)
// The following code is put inside the file: midterm.cpp:
#include <iostream> #include "func.h" using namespace std; int main() {
cout << "main";
someFunc();
return 0; }

// The following code is put inside the file: func.h:
#include <iostream> using namespace std; void someFunc() {
cout << "..." << endl; }


Answer to Question 3:
Result:
main...
Question 4: (5 marks)
double* func( int* array, int size ) { double stat[2] = {array[0], array[0]}; for (int i=1; i<size; i++) {
if ( array[i] < stat[0] ) stat[0] = array[i];
if ( array[i] > stat[1] ) stat[1] = array[i]; } return stat;
}
int main() { int array[4] = {12.5, 23.6, 23.2, 7.8};double* stat = func( array, 4 ); cout << "statistic 1 = " << stat[0] <<
", statistic 2 = " << stat[1] << endl; return 0; }

Answer to Question 4:
Result:
The result is unpredictable since the returned pointer from func points to a local variable,
which will be destroyed after the function returns.

Question 5: (10 marks)
void printArray( const int* array, int length ) {cout << "["; for (int i=0; i<length-1; i++)
cout << array[i] << ", ";if ( length > 0 ) cout << array[length-1]; cout << "]" << endl; }