=========================preview======================
(COMP1004)[2012](f)midterm~=lod8u^_51525.pdf
Back to COMP1004 Login to download
======================================================
For T.A. use only
Section Number

COMP 1004 Midterm -Fall 2012 -HKUST

Date: Saturday, October 20, 2012 Time Allowed: 2 hours, 10C12 noon Instructions:
1.
This is a closed-book, closed-notes examination.

2.
There are EIGHT questions on 12 pages (including this cover page).

3.
Write your answers in the space provided in black/blue ink. NO pencil please.

4.
All programming codes in your answers must be written in ANSI C++.

5.
You may use only the C++ language features and constructs learned in the class so far. For example, no for loops, arrays, nor pointers.

6.
For programming questions, you may de.ne additional helper functions if you see .t.


Student Name
Student ID
Email Address

For T.A. use only
Problem Score
1 / 15
2 / 7
3 / 12
4 / 5
5 / 14
6 / 15
7 / 14
8 / 18
Total / 100

Indicate whether the following statements are true or false by circling T or F. You get 1.5 point for each correct answer, .1.5 for each wrong answer, and 0 if you do not answer. (Remark: statistically speaking, guessing does not help if you do not know the correct answer.)
T F (a) The following C++ statement can be compiled without warnings or errors. cout << true << endl;
T F (b) The following C++ program cannot be compiled successfully. #include <iostream> using namespace std; int main(void) { continue; return 0; }
T F (c) Let A be the machine that executes program a.out, and let a.cpp be the C++ source .le of a.out. Then the .le a.cpp must be written using a text editor on machine A.
T F (d) The following C++ statement gives a compilation error. char c = -1265;
T F (e) A variable must always be initialized when it is de.ned.
T F (f) The following C++ statement leads to an in.nite loop. while (x = 0) ;
T F (g) The following two C++ statements are equivalent: ++x; x++;
T F (h) Pass-by-value is preferred when a function takes large objects as arguments.
T F (i) The value of variable x is 4 after the execution of the following C++ statement. int x = 3.5;
T F (j) Symbolic constants x cannot be initialized as follows. int y = 5; const int x = y * 2;
2

For each of the following program segments, write down the C++ output.
(a) [2 points]
float a = 1;
int b = 5;
cout << a/b << endl;

Answer: 0.2
(b) [2 points]
int a = 1;
int b = 5;
cout << a/b + 3.0 << endl;

Answer: 3.0 (or 3)
(c) [3 points]
int a = 1;
bool b;
cout << (((b = a-2) + 1) == true) << endl;

Answer: 0
Grading scheme: Answers must be exactly correct; no partial credits.
In the following arithmetic expression
10 . 2 1 3 + 3!
xy represents the power operation xy (e.g., 32 = 9); n! represents the unary factorial operator, and n!= n (n . 1) (n . 2) ... 2 1. Compute its result according to the precedence (from high to low) and associativity of the operators given in each table below.
(a) [4 points]