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

Fall 1999, Lecture Section 1, 2, 3, 4
Midterm Examination
13 October, 7:00pm C 9:00pm


Student Name: Lecture Section:


Student Number: Lab Section/TA Name:

Instructions:

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


2.
Check that you have all 14 pages (including this cover page). There is a blank page at the end in case you need extra space.


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


4.
Answer all questions in the space provided. Rough work should be done on the back pages. Circle your answers so that the grader can easily see them.


5.
No calculators are allowed.




Question
Score
Question
Score

1

10


2

11


3

12


4

13


5

14


6

15


7

16


8

17


9

18


Total (100%)












Part I: C++ Basics

Question 1 (3 marks): Which of the following identifiers are NOT ALLOWED in C++ and why:

Apple, 007, return, StarWars, a==b


Solution
1 Point 007 is illegal C staring with number
1 Point return is illegal C C++ reserved word
1 Point a==b is illegal C special character = not allowed









Question 2 (3 marks): What errors are there in the following C++ statement:

cout >> "Solution is << Answer << endl






Solution
1 Point >> is not allowed for cout
1 Point missing for comment Solution is
1 Point ; missing for the end of statement




















Question 3 (12 marks): What are the values of the following expressions? What are their types?

Value Type

1. 5.0 / floor(2.5) 2.5 double/float

2. 5.0 / ceil(2.5) 1.666 double/float

3. 5 / int(2.5) 2 int

4. 5 / 2.5 2.0 double/float

5. exp(log(10)) 10.0 double

6. ( 8%5 + 4 - (3*5)) + -8 int
( 4 % (2*5) - 8) + 9/2


1 point for each answer


Question 4 (6 marks): What is the output of the following program?

int main( )
{
int a = 3, b = 4, c = 5;
a = b++;
c += b;
a = --c;
b -= c;
c = c++;
cout << a << " " << b << " " << c << endl;
return 0;
}



Solution: 9 -4 10

2 point for each answer








Part II: Choice Constructs

Question 5 (3 marks): What is the value of n after the following C++ statements are executed?

int n = 5;
if(!n)
n += 2;
if(n)
n = -n;
n++;

Solution: -4

All or nothing











Question 6 (3 marks): What is the value of n after the following C++ statements are executed:

int m=0, n=1, i=0, k