Sie sind auf Seite 1von 10

January 22, 2009

CS201 (Intro. to Computing) FINAL Exam


1 2 3 4 5 6 7 8 9 10 TOTAL

Name : SUNet Username : ID :


Notes: a) Please answer the questions only in the provided space after each question. b) Duration is 160 minutes. c) Close-book, close-notes, no calculators and computers. Two A4 size cheat-note pages are allowed. d) There must be 10 pages (including this one) in this booklet. Please check it out!

QUESTIONS
1) (6 points) Write a function that takes two parameters of a templated type and returns one of them at random. Caution: parameters are not vectors or arrays.

template <class mytype> mytype thefunction (mytype param1, mytype param2) { RandGen r; int which = r.RandInt(2); if (which == 0) return param1; else return param2; }

NAME: 2) (8 points) The function below is a partial solution for the following problem: Write a recursive function that takes a string parameter and returns true if this string parameter is a palindrome. Otherwise, the function returns false. A palindrome is a word that is spelled the same in both directions. For example, madam and kulluk are palindromes. However, the function is incomplete. Complete this function by writing the if condition in the first box, and a string expression in the second box. You are not allowed to delete or update anything. Moreover, you cannot add anything other than the code that you are going to write in the boxes. bool IsPalindrome(string word) { if ( (word.length() == 0) || (word.length() == 1) ) { return true; } else { int len = word.length(); if ( { word.at(0) == word.at(len-1) string newword = ) ;

word.substr(1, len-2)

return IsPalindrome(newword); } else { return false; } } }

NAME: 3) a) (5 points) What is the output of the following program piece?

int t=0; int intarr [] = {7, 4, 1, 9, 2}; while (t < 5) { int temp = intarr[t]; intarr[t] = intarr[4-t]; intarr[4-t] = temp; t++; } for (t=0; t<5; t++) { cout << intarr[t] << " "; } cout << endl; 7 4 1 9 2

b) (2 points) Is there any syntactic problem with the following declaration? If so, explain the problem briefly. const double cs201 = 4.38e-12;

There is no syntactic problem with this declaration.

c) (3 points) Consider num is an int variable. What is the maximum value for num that the expression num+num does not overflow? You may leave your result as a mathematical formula. There might be different answers: Answer1: Integer part of INT_MAX / 2

2 31 1 Answer2: Integer part of 2


Answer3:

2 30 1

Answer4: 1073741823

NAME: 4) a) (6 points - Each correct answer is worth 1 point. Each wrong answer reduces 0.5 point.) Fill in the cells in the following table with true or false depending on the correctness of the propositions. Leaving blank means no answer! Proposition True / False

On the average, sequential search is faster than binary search when False performed on a sorted vector. Assembly language is a high-level programming language. False It is not allowed to access private data members of a class in the main True function. Selection sort is always faster than insertion sort when executed on the False same vectors/arrays. Number of elements of a built-in array cannot be a variable. You can change the value of a const-reference parameter in a function. True False

b) (8 points) The function below is a partial solution for the following problem: Write a function that takes an integer parameter, say limit. The function should generate and return an integer vector. This vector should contain all the integers between (and including) 1 and limit exactly once. Moreover, the vector should be in descending order. You may assume that limit is positive. For example, if limit is 7, then the vector to be returned from this function should contain 7 6 5 4 3 2 1 However, the function is incomplete. Complete this function by writing the function return type in the first box and necessary statements and/or expressions in the other boxes. You are not allowed to delete or update anything. Moreover, you cannot add anything other than the code that you are going to write in the boxes.
tvector <int> { int k; tvector <int> v; for ( { k = limit ; k >= 1 ; k-) generateReverseSortedVector (int limit)

An alternative could be
for (k=1; k<=limit; k++) { v.push_back(limit-k+1); }

v.push_back(k) } return v; }

NAME: 5) (6 points) The following program does not compile due to a syntax error.
#include <iostream> #include <string> using namespace std; void once (string addr, double x) { cout << addr << endl; if (x > 5.8) { sonra (addr, x-1); } } void sonra (string s, double y) { cout << s << endl; if (y >= 5.25) { once(s, y); } } int main() { string a; double b=10.1; cin >> a; sonra (a, b); return 0; }

a) Briefly explain the syntax error. Please also specify the line at which the error occurs. The problem is at sonra (addr, x-1); line. sonra function is called before its declaration b) Correct this syntax error by adding some line(s) of codes to the program. No code deletion or update is allowed; you can only add code. In your answer, write the code to be added. Do not forget to mention the exact place of the addition. The following prototype should be added after using namespace std; and before once's definition. void sonra (string s, double y); // void sonra (string, double); is also OK You can also add the prototype of once, but this is not a must for this program to compile.

NAME: 6) a) (5 points) What is the output of the following program?


#include <iostream> #include "tvector.h" using namespace std; int main() { int i; tvector <int *> ipv(4); ipv[0] = new int; *ipv[0] = 0; for (i=1; i <= ipv.size()-1; i++) { ipv[i] = ipv[i-1]; *ipv[i] = i; } for (i=0; i <= 3; i++) { cout << *ipv[i] << endl; } return 0; }

3 3 3 3 b) (1 point) The last phase of creating an executable program is the one in which object files and libraries are combined together. What is the name of this phase? Linking Phase

c) (3 points) What is the output of the following program piece?


string batman="Maho", robin="Aga"; if (true) { string batman = "Faso"; cout << batman << " " << robin << endl; } cout << batman << " " << robin << endl;

Faso Aga Maho Aga

NAME: 7) (11 points) What is the output of the following program?


#include <iostream> #include <string> using namespace std; int dia = 15; int bim = 13; double migros = 17.5; void so_static (string add) { static string name="chip"; string other = "hos"; name = name + add; other = other + add; cout << name << " " << other << endl; } void super (int & kim) { double dia; kim = kim -2; dia = migros - bim; migros = migros + 1; bim++; cout << "super: " << dia << " " << bim << " " << migros << " " << kim << endl; } void market (int bim, int & kim) { int migros = kim + 1; kim = bim; dia = dia * 2; bim++; cout << "market1: " << dia << " " << bim << " " << migros << " " << kim << endl; super (bim); cout << "market2: " << dia << " " << bim << " " << migros << " " << kim << endl; } int main() { int i, a, b; for (i=0; i <= 2; i++) { so_static("ton"); } a = 8; b = 2; market(a, b); cout << "main: " << a << " " << b << endl; return 0; }

chipton hoston chiptonton hoston chiptontonton hoston market1: 30 9 3 8 super: 4.5 14 18.5 7 market2: 30 7 3 8 main: 8 8

NAME:

8) (8 points) Following is the private data members of a new class named PerpendicularTriangle. private: double heightSide; double baseSide; double hypotenuseSide;
hypotenuse height

base

Write the implementation of the constructor for this class. The constructor should take height and base as parameters and make the necessary assignments to all of the private data members. Please remark that the hypotenuse is not a parameter of the constructor; you should calculate it using the Pythagoras theorem, which is hypotenuse = height 2 + base 2 .

PerpendicularTriangle::PerpendicularTriangle (double height, double base) { heightSide = height; baseSide = base; hypotenuseSide = sqrt(height*height + base*base); }

NAME: 9) (13 points) Write a program that reads a string from the keyboard. The program will create an output file and write the ASCII code of each character of the input string to this file. You should write one ASCII code on each line of the output file. The name of the output file should be codes.txt. You should open this file, but you do not need to check whether it is successfully opened or not. For example, if the input string is go4Go;ogle then the output file will be as follows. codes.txt
103 111 52 71 111 59 111 103 108 101

In this question, you may prefer not to attempt to solve it by signing the not attempted box below and secure 3 points. If you sign the not attempted box below, you accept that you did not answer this question and you will receive 3 points. In this case, your answer will not be graded even if you write something as solution. Not attempted

#include <iostream> #include <string> #include <fstream> using namespace std; int main() { string fname = "codes.txt"; string inputstr; ofstream out; int i; out.open(fname.c_str()); cin >> inputstr; for (i=0; i < inputstr.length(); i++) { out << int(inputstr.at(i)) << endl; } out.close(); inputstr[i] could be used return 0; instead of inputstr.at(i) }

NAME: 10) (15 points) Write a function that takes two integer vectors, say vec1 and vec2, as parameters. The function should display the elements of vec1 that does not exist in vec2. For example, if vec1 is 5 3 7 12 3 5 1 and vec2 is 9 1 13 5

then the output becomes as follows. 3 7 12 3

In this question, you may prefer not to attempt to solve it by signing the not attempted box below and secure 3 points. If you sign the not attempted box below, you accept that you did not answer this question and you will receive 3 points. In this case, your answer will not be graded even if you write something as solution. Not attempted

void diff (tvector<int> vec1, tvector<int> vec2) { int i, j; bool found; for (i=0; i < vec1.size(); i++) { found = false; for (j=0; j < vec2.size(); j++) { if (vec1[i] == vec2[j]) found = true; } if (!found) cout << vec1[i] << endl; } }