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

Fall 2003, Lecture Section 1, 2
Midterm Examination
Tuesday 21 October, 7 C 9PM

Student Name: key Lecture Section:


Student ID: Lab Section/TA Name:

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


2.
Check that you have all 15 pages (including this cover page).


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


4.
Please circle your answer.


5.
Answer all questions in the space provided. Rough work should be done on the back pages.




Question
Score

1
/6

2
/10

3
/7

4
/15

5
/16

6
/14

7
/5

8
/5

9
/4

10
/13

11
/5

Total
/100







Question 1 : Operator Precedence (6 marks)

a) What is the output of the following program? (2 marks)

#include <iostream>
using namespace std;

void main(void){
int a = 15, b = 4, c = 6, e;
e = a / b * c + (b + c) - a % c;
cout << e << endl;
}

Answer (2 marks):
25



b) Give the precedence order of the following operators (group those who have the same precedence order together). (4 marks)

( ), %, +, -, *, /


Answer:
Evaluated first: ( )
Evaluated second: * / %
Evaluated third: +, -

(marking scheme:
+4 all correct
+3 one mistake
+2 two mistakes
+1 some correct
Question 2: C++ operators (10 marks)

a) What is the output the following code? (3 marks)

int x = 13, y = 3, z = 12;
if (--x > z++)
y *= x++;
else
y += --z;
cout << x << << y << << z;

Answer (3 marks: +1 for each number):
12 15 12


b) Write a condition that tests whether a given integer is a leap year. (7 marks)
(Hint: A year is NOT leap year if it is NOT divisible by 4, OR the year is divisible by 100, but not divisible by 400).


int y;
cin >> y ;
if( )

cout << Not a leap year. << endl;
else
cout << Is a leap year << endl;

Answer:

y%4 || (!(y%100) && y%400) or y%4 || !(y%100) && y%400

or

y%4!=0 || (y%100==0 && y%400!=0) or y%4!=0 || y%100==0 && y%400!=0

or

y%400!=0 && y%100==0 || y%4!=0

(7 marks: -1 for each mistake)



Question 3: switch, nested if (7 marks)

Translate the following code segment using nested if into the code using a switch statement. You are not allowed to use more than 10 cases, other wise marks will be deducted. Assume score is an integer between 0 and 100. (7 marks)

if(score >= 90)
cout << A << endl;
else if(score >=80) cout << B << endl;
else if(score >=70)
cout << C << endl;
else if(score >=60)
cout << D << endl;
else
cout <<