=========================preview======================
(COMP102)midterm2.pdf
Back to COMP102 Login to download
======================================================
THE HONG KONG UNIVERSITY OF SCIENCE AND TECHNOLOGY

Department of Computer Science
COMP 102: Computer and Programming Fundamentals I
Spring 2000


Midterm Examination (All Sections)

Ren Lan Liao

Date: March 24, 2000 Time: 6:00-8:00 pm Venue: Lecture Theater A
This exam contains 9 questions in 12 pages. Please count the pages.
You have 2 hours to complete this exam.

Problem Your points Max points Problem Your points Max points
1 10 6 14
2 10 7 15
3 4 8 10
4 12 9 15
5 10
Subtotal 46 Subtotal 54
Your total points 100



Please identify yourself: Lab section Name
Student id


Signature

1. (10 points in total)

What are the values of the following expressions?
1.
9/2.0 + 25.3


2.
6 + 4.8/2*3


3.
25%5 + 12.5*2/5


4.
7.5 -38%7*2 +10



What are the values of the following expressions? int x = 8, y = 20;
5.
x / y


6.
x % y


7.
y / x


8.
y % x


9.
double (y / x)


10.
double(x) / y



2. (10 points in total) if int a = 1, b = 2, c = 3;are the following conditions trueor false?
1.
a < c - b


2.
((b / 2) == a )|| c < 3


3.
(b < -2) || (a* 3 >= c) && (b > a)


4.
b/c || !b && c


5.
(a == true) && b


6.
((a - 1) == true )&& c


7.
(b > 5) || (c == 0)


8.
true || true && false


9.
(a / b) && (c == 3)


10.
( a < b) && ((b+1) == c)




3. (4 points in total)
a) What value of x is produced if its initial value is 2?
if (x >= 0 )
x = x + 2;
else if (x >= 2)
x = x + 4 ;

x:

b) What value of x is produced if its initial value is 2?
if (x >= 0 )

x = x + 2;

if (x >= 2)

x = x + 4 ;

x:


4. (12 points in total)

Consider the following definition:

int fun (int x){
return (3 * x + 1);
}

What value is assigned to y in Parts 1-5?
1.
y = fun(2);


2.
y = 2 + fun(3);


3.
z = 1;
y = 2 + fun(3 * z + 1);


4.
z = 5;
fun(z);
y = z;


5.
y = fun(fun(2));



6. Write a statement to cout the value of funwith argument 9.
5. (10 points in total) for statement
a) (4 points) The statement which produces the following output is:

1
22
333
4444
55555

Statement 1
for(a = 1; a <= 5; a++) {
for( b = 1; b <= 5; b++)
cout << b;
cout << endl;
}

Statement 2 :
for( a = 1; a <= 5; a++) {
for( b = 1; b <= a; b++)
cout << a;
cout << endl;
}

Statement 3:
for( a = 1; a <= 5; a++) {
for( b = a; b <= 5; b++)
cout << a;
cout << endl;
}

Statement 4:
for( a = 1; a <= 5; a++) {
for( b = 1; b < a; b = b + a)
cout << b;
cout << endl;
}

b) (6 points) Write a program using 2 while loops to produce the same output mentioned in

a).


6. (14 points in total )
a) (2 points in total) The statement that compares totalwith good_guess, if they are equal then prints the value of total, otherwise prints the value of good_guess, is
b) (2 points ) The statement that prints the text string "corr