=========================preview======================
(Comp102)[2006](f)midterm~kytangaa^_10116.pdf
Back to COMP102 Login to download
======================================================
HONG KONG UNIVERSITY OF SCIENCE AND TECHNOLOGY
COMP 102: Computer and Programming Fundamentals I
Fall 2006
Midterm Examination Key
November 3rd, 2006
Student Name: _______________________ Lecture Section: __________________
Student ID: _______________________ Lab Section: __________________
INSTRUCTIONS
1.
This is a closed-book, closed-notes examination.
2.
Check that you have all 14 pages (including this cover page and the last 2 blank pages for rough work).
3.
Write your name, student ID, lecture and lab sections on this page.
4.
Answer all questions using spaces provided and in blue or black ink
5.
Turn off your mobile phone/pager or else you will be immediately disqualified.
Question Maximum Points Your Points
1 10
2 15
3 10
4 10
5 12
6 8
7 17
8 18
TOTAL 100
1. (TOTAL 10 POINTS) What is the value of each of the following expressions? Each expression should be evaluated independently with the same set of initial values as given below.
int count = 0;
int limit = 10;
int x = 2, y = 3, z = 1;
Mark your answers in the ________ space provided after each expression. If the expression is a Boolean expression, then mark your answer to the Boolean expression with either trueor false.
a)(count == 0) && (limit < 20) ________ true ______________ b)limit > 20 || count < 5 ________ true ______________ c)(count = 1) && (x < y) ________ true ______________ d)!(((count<10)||(x<y))&&(count>=0)) ________ false _____________ e)limit / x > 7 || limit < 20 ________ true ______________ f)(5&&7) + (!6) ________1______________ g)limit * y % 4 / 2 ________1______________ h) y/x == z ________true ______________ i)double(++limit/y) ________3______________ j)(3*x + y)/(z +2) ________3______________
2. (TOTAL 15 POINTS) What is the output on the screen of each of the following code segments? Provide your answer using only spaces provided after each question.
a) (2 POINTS)
int n = 3;
while ( n > 0)
cout << n-- << "#";
Answer: _________ 3#2#1#_____________________________________________
b) (2 POINTS)
int n = 3;
do cout << --n << "#";
while ( --n > 0);
Answer: _________ 2#0#______________________________________________
c) (2 POINTS)
int n;
for ( n = 0; n < 3; n++)
cout << n << "#";
Answer: ________ 0#1#2#_______________________________________________
d) (2 POINTS)
int n;
for (n = 10; n > 0; n /= 2);
cout << n << "#";
Answer: ________ 0#_______________________________________________
e) (2 POINTS)
for (int i=0, j=20; i<j; i+=2)
cout << j-- << "#";
Answer: ________ 20#19#18#17#16#15#14#________________________________
f) (2 POINTS)
int n = 1024, log = 0;
for (int i = 1; i < n; i *= 2)
log++;
cout << n << "#" << log;
Answer: ________ 1024#10______________________________________________
g) (3 POINTS)
int a = 57, b=35;
while ( a > 0 ) {
cout << a / b;
a %= b; b /= 2;
}
Answer: _______ 110101________