=========================preview======================
(COMP104)[2008](f)midterm~ycchak^_10130.pdf
Back to COMP104 Login to download
======================================================
(To be completed by TA)
Key:
Lab section:
The Hong Kong University of Science and Technology
COMP104: Programming Fundamentals and Methodology
Fall 2008, Lecture Sections 1, 2, 3
Midterm Examination
Tuesday, October 14, 2008 7:00PM C 9:00PM
Name: _______________ Student ID: _____________ Email: ____________________
Instructions:
1.
This is a closed-book, closed-notes examination.
2.
Write your name, student ID and email on this page. (During the exam, a TA will pass by to check your ID and help fill the boxes at the top.)
3.
Check that you have all 11 pages and additional 3 blank pages for rough work.
4.
Answer all questions using the space provided after each question.
5.
Rough work should be done on the last blank page provided.
6.
All programming questions are in C++.
Question Score
1 /24
2 /6
3 /12
4 /16
5 /8
6 /16
7 /18
Total /100
What are the outputs of the following programs?
(a) 4 marks
#include <iostream>
using namespace std;
int main()
{
intx =10;
inty =15;
if( x== y)
y =x + 10;
x =x -5;
cout <<x << " " << y << endl;
return 0;
}
Answer:
(b) 4 marks
#include <iostream>
using namespace std;
int main()
{
int val = 123;
while (val > 0)
{
cout << val % 10;
val= val / 10;
}
cout << endl;
return 0;
}
Answer:
(c) 4 marks
#include <iostream>
using namespace std;
int foo(char ch)
{
if( ch>='A' && ch <= 'F' )
return 4;
else if( ch < 'M' )
return 1;
else if( ch < 'P' )
return 3;
else if( ch < 'S' )
return 5;
else if( ch < 'U' )
return 9;
return 1; //if all cases are not matched
}
int main()
{
cout << foo('L') << foo('O') << foo('V') << foo('E') << endl;
return 0;
}
Answer:
(d) 4 marks
#include <iostream>
using namespace std;
int main()
{
int i, j, k, count = 0;
for (i =0; i< 3; i++)
for (j = 0; j < i; j++)
for (k = 0; k < j; k++)
count++;
cout << count << endl;
return 0;
}
Answer:
(e) 4 marks
#include <iostream>
using namespace std;
int foo(int a, int b)
{
int tmp =1;
while (--b > 0)
{
tmp *= 10;
}
return tmp * a;
}
int main()
{
int val = foo(5, 4) + foo(5, 2) + foo(4, 1) + foo(3, 3);
cout << val << endl;
return 0;
}
Answer:
(f) 4 marks
#include <iostream>
using namespace std;
void foo(int a, int& b)
{
b= b+ 1;
a= a* 2;
}
int main()
{
int a= 1, b= 2;
foo(a, b);
cout << a << b << endl;
}
Answer:
Consider the following statements:
(A)
const int age = 10;
(B)
int x, y;
(C)
doublex =5.0% 4;
(D)
intx =5 /(12% 4);
(a) 3 marks
Which statement will result in a compilation error and why?
Answer:
(b) 3 marks
Which statement will result in a run-time error and why?
Answer:
The following function is supposed to convert a time in minutes and seconds int