=========================preview======================
(COMP104)midterm02.pdf
Back to COMP104 Login to download
======================================================
Hong Kong University of Science and Technology
COMP104: Programming Fundamentals and Methodology

Fall 2002, Lecture Section 1, 2, 3, 4
Midterm Examination
Wednesday 23 October, 7 C 9PM

Student Name: Lecture Section:


Student ID: key Lab Section/TA Name:

Instructions:
1.
This is a closed-book, closed-notes examination.


2.
Check that you have all 17 pages (including this cover page).


3.
Write your name, student ID, lecture section, lab section/TA name on this page.


4.
Please circle your answer.


5.
Answer all questions in the space provided. Rough work should be done on the back pages.




Question
Score

1
/5

2
/5

3
/4

4
/5

5
/8

6
/7

7
/10

8
/2

9
/6

10
/12

11
/17

12
/4

13
/9

14
/6

Bonus
( /5)

Total
/100







Question 1: Basic Expressions (5 marks, 1 mark for each part)
Given the variables:

int X = 10;
int Y = 5;
int Z = -3;
double R = 3.4;

Evaluate the following Boolean expressions:

a.
(X <= Y) && (Y > Z)

Answer: False


b.
(X > Y) && (Y > Z) && (X != Z) || (X % Y) > Z

Answer: True


c.
((int)R) >-Z && 1

Answer: False


d.
!(1 && 0 || !1)

Answer: True


e.
!1 || Z && Y

Answer: True



Question 2: Basic Expressions and Increment Operators (5 marks)
What is the output of the following code segments?

(a) (1 mark)
int x=3, y=3, z=5;
if (++x>y)
x++;
cout << x;
Answer: 5

(b) (1 mark)
int x=3, y=3, z=4;
if (x--==--z)
x+=2;
cout << x;
Answer: 4

(c) (3 marks)
int x=3, y=3, z=4, a=5;
if ((x>=y++) && (a--==++z))
x++;
cout << x << " " << y << " " << z;

Answer: (1 mark each number)
4 4 5
Question 3: Expressions (4 marks, 1 mark for each part)

Write the output of the following code segments (if a statement produces an error mention it). Assume math.h has been included.
(a)
int x=1, y=100;
cout << sqrt(x/y);

Answer 0

(b)
int x=1, y=100;
cout << ((double)x)%y;

Answer Syntax Error

(c)
int x=1, y=100;
cout << (double)(x-=103%y*2);

Answer -5

(d)
int x=1, y=100;
cout << ++(floor(x-=103%y));

Answer Syntax Error


Question 4: If-else statement (5 marks)

(a) (3 marks)
What is the output of the following code?
int x=-5, y=0, z=5;
if (x)
if (y)
if (z)
y++;
else x++;
else z++;
cout << x << " " << y << " " << z;

Answer: (3 marks, 1 mark for each number)
-5 0 6







(b) (2 marks)
What is the syntax problem with the following code?
int x=-5, y=0, z=5;
if (x)
if (y)
if (z) y++;
if (y) y++;
else x++;
else if (y) x++;
else z++;
cout << x << " " << y << " " << z;
Answer: (2 marks)
The second else [else if(y)] cannot be matched.


Question 5