=========================preview======================
(COMP104)[2009](f)midterm~kmlaiab^_23242.pdf
Back to COMP104 Login to download
======================================================
For T.A. use only
Section
Number

COMP 104 Midterm -Fall 2009 -HKUST

Date: Monday, October 12, 2009 Time Allowed: 1 hour 30 minutes, 8C9:30 pm Instructions: (1) This is a closed-book, closed-notes examination.
(2)
There are 10 questions on 13 pages.

(3)
The last 3 pages are scrap paper, which you do not need to submit.

(4)
Answer the questions in the space provided.

(5)
You must answer all questions in black or blue ink. (NO pencils please)

(6)
All codes described in the questions are ANSI C++ compliant.

(7)
All codes you are asked to write MUST be ANSI C++ codes.


Student Name
Student ID

For T.A. use only
Problem Score
1 / 16
2 / 6
3 / 6
4 / 6
5 / 7
6 / 7
7 / 8
8 / 8
9 / 22
10 / 14
Total / 100


Part I: Multiple Choice Questions

1. [16%] Circle the right answer(s) for each of the following multiple choice questions. Note: There can be more than one correct answer for each question. This question does not give out partial credit.
(a)
[4%] Which of the following C++ functions returns the value of a to the power of b? (E.g., 2 to the power of 3 is equal to 2 2 2 = 8.)

A. int power(int a, int b) { return a^b; }
B. int power(int a, int b) { int i=0, r=0; while (i < b) {r=r.a; i=i+1;}}
C. int power(int a, int b) { int i=0, r=0; while (i < b) {r=r.a; i=i+1;} return r; }
D.
.
int power(int a, int b) { int i=0; r=1; while (i < b) {r=r.a; i=i+1;} return r; }

E.
int power(int a, int b) { int i=0; r=1; while (i < a) {r=r.b; i=i+1;} return r; }



(b)
[4%] Which of the following identi.ers is/are legal in C++?


A. int double;
B.;

. char
C. bool catch-22;
D. char1 or2;
E.
. .oat Float;
(c) [4%] What does the following piece of code do?
int x;
while(true);
{
x = 1;
}
A. It does not compile.
B. It sets x to 1 once.
C. It sets x to 1 multiple times.
D.
. It never sets x to 1.
E.
. It never exits the loop.
(d) [4%] What is true about the following function?
int func(int x)
{
if (x > 0)
return -x -1;
else
return x + 1;

}
A.
. It may return a positive number.
B.
. It may return a negative number.
C.
.
It may return zero.

D.
It always returns the value of x.

E.
It may change the value of x.



Part II: Program Tracing
For each of the following programs, write down the output in the space provided.
2. [6%]
#include <iostream> using namespace std;
int main( )
{
int a=-1, b=0;
if(a) if(b) cout . a . endl;
else
cout . b . endl; cout . a . endl; return 0;
}
Output:
0 -1
(3 for each number, -1 for each formatting mistake like no new line)
3. [6%]
#include <iostream> using namespace std;
int main( )
{ int a = 1, b = 3;
if ((a=0) || (b=4)) { a = a+3;
}
cout . a . endl;
cout . b . endl;
return 0;
}

Output:
3 4
(3 for each number, -1 for each formatting mistake like no new line)
4. [6%]