=========================preview======================
(comp102)[2004](s)midterm~plliu^_10114.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 2004
Midterm Examination
Ren Lan Liao
Date: March 26, 2004 Time: 7:00-9:00 pm Venue: Lecture Theater B
This exam contains 8 questions in 10 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
5
15
2
10
6
13
3
12
7
12
4
28
Subtotal
60
Subtotal
40
Your total points
100
Please identify yourself: Lab section
Name
Student id
Signature
1.
(10 points in total) if int a = 1, b = 2, c = 3; are the following conditions true or 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)
T
F
F
T
F
F
FT
T
T
F
2. (10 points) Write Boolean expressions that represent the given English expressions. Assume any variables you use have been declared and initialized.
a) at least one of x or y is odd
Answer:____________(x%2)||(y%2)_________________________________
b) at least one of x or y is non-negative (x is non-negative is written x >=0)
Answer:__________(x>=0)||(y>=0)____________________________________
c) The hit is no more than 2.5 units away from target. (assume existence of int abs(double) function)
Answer:_ abs(hit-target)<=2.5
d) x is 1 or x is equal to y
Answer:_ (x==1)||(x==y)
e) t is between 2.3 and 3.3
Answer:_ (t>2.3)&&(t<3.3)
3.
(12 points in total)
Consider the following definition:
int fun (int x){
return ( x%3 + 1);
}
What value is assigned to y in question 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 fun with argument 9.
3
3
4
5
1
cout<<fun(9);
4. (28 points total)
a) (3 points) What is the output from the following block of code?
int x, y = 4;
for( x = 2; x < y; x++)
y = y++ % x;
cout << y << -;
Answer:
1