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

Fall 2004, Lecture Section 1, 2, 3
Final Examination
Friday, Dec.17, 2004 08:30 C 11:30AM

Student Name: key Lecture Section:


Student ID: Lab Section/TA Name:

Instructions:

1.
This is a closed-book, closed-notes examination, calculator is also not allowed.


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


3.
Write your name, student ID, lecture section, lab section and 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
Topic
Score

1
Strings
/10

2
File I/O
/5

3
Pointers
/9

4
Simple Linked List
/10

5
Algorithms on Linked List
/14

6
Doubly Linked List
/18

7
Classes
/10

8
Dynamic Classes/ADT
/24

Total
/100
/100



Question 1 : Strings (10 marks)





a) What is the output of the following program? (1 mark)

#include <iostream>
#include <string>
using namespace std;

void main(){
string x = "2";
string y = "3";
string z = "5";

if(x+y == z){
cout << "2 + 3 = 5" << endl;
}
else{
cout << "2 + 3 != 5" << endl;
}
}

Answer (1 mark):
2 + 3 != 5


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

#include <iostream>
#include <string>
using namespace std;

void main(){
string x = "4";
string y = "2";
string sum = x + y;
cout << sum << endl;
}


Answer (2 marks, 1 for each digit):
42









c) Complete the following program to print the users input word in reverse order and check if it is a palindrome. A word is a palindrome if its reversion is the same as itself. For example, the output of the program should look like this:

Enter a word to reverse: canoe
Your word spelled backwards is: eonac
It is not a palindrome.

Or

Enter a word to reverse: kayak
Your word spelled backwards is: kayak
It is a palindrome.

Also, it should be case sensitive, i.e., kayak is a palindrome, but Kayak is not. You can use predefined C++ string functions such as xxx.length() (this function returns the number of characters of the given string xxx). (7 marks)

void main(){
string inword, outword;
bool isPalindrome;

// Get user input
cout << "Enter a word to reverse: ";
cin >> inword;

// copy user input into outword, this will make sure
// outword is long enough to contain inword
outword = inword;
// START YOUR CODE HERE ----->








// <---- END YOUR CODE HERE
// Output the reversed word and whether it's a palindrome
cout << "Your wor