=========================preview======================
(COMP104)final00F_sol.pdf
Back to COMP104 Login to download
======================================================
Hong Kong University of Science and Technology
COMP104: Programming Fundamentals and Methodology
Fall 2000, Lecture Section 1, 2, 3, 4
Final Examination
Mon 18 December, 12:30 C 3:30PM
Student Name: 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 to make it clear.
5.
Answer all questions in the space provided. Rough work should be done on the back pages.
Question
Score
1
/ 2
2
/ 5
3
/ 11
4
/ 7
5
/ 8
6
/ 15
7
/ 10
8
/ 10
9
/ 18
10
/ 14
Total
/ 100
Question 1: Character i/o (2 marks)
What is the output after executing the following C++ statements?
a)
int n = 0;
if(A == a)
n = 1;
else if(A > a)
n = 2;
else
n = 3;
cout << n << endl;
b)
int n = 0;
char c1 = g, c2 = 6;
char c3 = toupper(c1);
if(isdigit(c2))
if(islower(c3))
n = 1;
else
n = 2;
else
if(islower(c2))
n = 3;
else
n = 4;
cout << n << endl;
Key: (2 marks: 1 mark each)
a) 3 (1 mark)
b) 2 (1 mark)
Question 2: Strings (5 marks)
What is the output after executing the following C++ programs?
a)
#include <iostream>
#include <string>
using namespace std;
void main(){
string s = "abc";
string t = "104";
string w = "ace";
if(w < s)
t += w;
else if(w == s)
t = s;
else
t += s + w;
cout << t << endl;
}
b)
#include <iostream>
#include <string>
using namespace std;
void main(){
string police = "stop";
string course = "104";
string exam;
int n, size = 2;
n = police.find("to");
exam = course.substr(0, size);
exam = police.substr(n, size+1) + exam;
cout << exam << endl;
}
Key: (5 marks)
a) 104abcace (1-mark: 104, 1-mark: abc, 1-mark: ace)
b) top10 (1-mark: top, 1-mark: 10)
Question 3: Character and file i/o (11 marks)
Suppose you are a spy. You want to encrypt the following character sequence, which is stored in a text file named "file1.msg":
killthebadguy(eof)
Write a program so that the encrypted character sequence is displayed on the screen:
fdggoczwvybpt
A key, which is a non-zero integer, is used to perform encryption. In this example, the encryption key is -5, since, for example, 'm' is mapped to 'h' (i.e. 5 characters before 'm').
(a)
Write