=========================preview======================
(Comp102)[2007](s)midterm~kytangaa^_10123.pdf
Back to COMP102 Login to download
======================================================
Hong Kong University of Science and Technology
COMP 102: Computer and Programming Fundamentals I
Spring 2007
Midterm Examination Solution
11 April 2007
Student Name: _________________________
Student Number: _________________________
Lecture & Lab Sections: _________________________
Instructions
1.
This is a closed-book, closed-notes examination.
2.
Check that you have all 10 pages (including this cover page).
3.
Write your name, student number, lecture and lab sections on this page.
4.
Answer all questions in the space provided using a ball pen.
5.
Rough work should be done only on the back pages.
6.
Please turn off your mobile phone/pager or else you will be disqualified.
Question Maximum Points Your Points Question Maximum Points Your Points
1 5 6 11
2 8 7 16
3 8 8 8
4 12 9 12
5 20
sub total 53 sub total 47
YOUR TOTAL POINTS
1. (5 points) Which of the following are legal identifier names in C++? Mark the correct answers with Y and incorrect answers with N in the space provided.
a) FindArea ______Y______
b) RADIUS&flu ______N ______
c) findArea ______Y ______
d) 10questions ______N______
e) class2007 ______Y ______
2. (8 points)
int i = 4, j = 17, k = 1,h = 8;
bool x = true, y = true z = false;
What is the value of each of the following expressions? (Each expression should be evaluated with the same set of initial values as given above.) Mark the Boolean answers true with TRUE and false with FALSE in the space provided.
a) 25 % 4 _____1_______________________
b) (int)(76.0252175 * 100) / 100_____76_______________________
c) 'z' - 'a' _____25_______________________
d) ++h * 5; _____45_______________________
e) x || x && z _____TRUE____________________
f) (k!= 1) == !(k == 1) ______TRUE___________________
g) i / 2 + h % j * k ______10_______________________
h) i-- + j % ++k ______5________________________
3. (8 points) a) (3 points) What is the output on the screen after the following code is executed?
#include <iostream>
using namespace std;
int main()
{
double x = 3.5, y = 0.9;
int a = x/y;
if (a == 1)
x+=5;
else if (a == 2)
x+=10;
else if (a ==3)
x +=16;
else if (a == 4)
x += 34;
cout << x << endl;
return 0;
}
Answer: ______19.5________________________________________________
b) (5 points) Replace the if else statement in the a) with a switch statement:
switch (a) { case 1: x+=5; break; case 2: x+=10; break; case 3: x+=16; break; case 4: x+=34; break;}
3
4. (12 points) What is the output on the screen after the following code is executed?
#include <iostream>
using namespace std;
int i, j, k;
int main()
{
i = 3;
j = ++i;
k = 15%6;
cout << i << ' '<< j << ' '<< k << endl;
int j = 2;
i = 3 * j;
k = 3 + j;
cout << i << ' '<< j << ' '<< k << endl;
return 0;
}
Answer:
4 4 3
6 3 5
b)