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

COMP 1004 Midterm -Fall 2011 -HKUST

Date: Monday, October 17, 2011 Time Allowed: 2 hours, 7C9 pm Instructions:
1.
This is a closed-book, closed-notes examination.

2.
There are EIGHT questions on 13 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.


Student Name
Student ID
Email Address

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

Problem 1 [10 points] True or false Indicate whether the following statements are true or false by circling T or F. You get 1 point for each correct answer, .1 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 expression is true. 1.99 == 1
T F (b) The following variable de.nition will give a compilation error: int A_______1;
T F (c) The following codes will give a compilation error: bool x = true; char y = x;
T F (d) Assuming that variable x is an unsigned ( (x & x) == (x | x) ) int, the following expression is always true.
T F (e) The signed char type can represent a larger set of values than the unsigned char type.
T F (f) When a signed int variable over.ows, it always becomes a negative integer.
T F (g) Assume that in a machine, sizeof(int) and sizeof(float) are both 4 bytes. possible C++ int values can be represented exactly using float in the machine. Then all
T F (h) The break statement terminates the execution of the outermost enclosing loop.
T F (i) The continue statement can only be used inside a loop.
T F (j) The dangling-else problem can be solved by proper code indentation.

Problem 2 [12 points] Order of operations Compute the result of the following expression
5.0 . 3.0+4.0/2.0 . 10.0 where * and / have the same precedence, + and -have the same precedence, and
(a) [4 points] all operators are left-associative and *, / have lower precedence than +, -.
Answer: 30.0
(b)
[4 points] all operators are right-associative and *, / have higher precedence than +, -.

(c)
[4 points] all operators are right-associative and *, / have lower precedence than +, -.


Answer: 1.8
Answer: -0.1
Problem 3 [12 points] Break and continue

#include <iostream> using namespace std;
int main(void)
{
int x;
int y = 0;
while (true)
{
cin . x;

if (x > 0)
{
y += 2.x;
}
else if (x < 0)
{
y += x;
if (y < 0) break; else if (y > 100) continue;
}
y -= 10; cout . "Inside: y = " . y . endl; }
cout . "Outside: y = " . y . endl;
while (cin . x)
;

return