Name:
Student ID:
Email instructor a text
file that contains your answers. Use "EE261 Final" as subject of your
email.
Ux represents Course Unit x.
Total: 20 points, 5 points for each of U1-U4.
1. Which of the following are NOT valid C++ identifiers? [1%, U1]
a. theDog
b. allInOne
c. all_in_one
d. 2morrow
e. page#
f. AMOUNT
g. C++
h. i
i. _function
j. 100
Answer:
2. Which of the following are literal constants? [1%, U1]
a. 's'
b. 256
c. "Hi, I am not a string literal..."
d. 015
e. 1024f
f. 3.1415926
g. "s"
h. "256"
Answer:
3. EE261 Fall 2009 has x students, and y of them receive the letter
grade A. Write an expression to calculate the percentage of A students.
(Hint: for 33%, number 33 is the percentage) [1%, U1]
4. Write a function palindrome() to test whether a string is a
palindrome. A palindrome is a string that reads the same backward as
forward, such as madamImadam
or poordanisinadroop. [2%,
U1/U3]
5. Write a loop to sum up all numbers between 0 and a given upper
bound alpha, inclusive, that
are multiples of 5. For
example, if alpha = 15, then the sum should be 30 (5+10+15). [1%, U2]
...
int main(){
int alpha;
cin >> alpha;
if (alpha < 0){
cout << "alpha cannot be negative. Quit."
<< endl;
return 1;
}
int sum;
// add your loop here!
// output
cout << "The sum of all multiples of 5 between 0
and "
<< alpha << ", inclusive, is " << sum <<".\n";
return 0;
}
6. The following code snippet is designed to count the number of ASCII
characters
contained in a text file named "data.in".
...
1 int countChars (ifstream
& file){
2 int count = 0;
3 char c;
4 file.get(
c );
5 while (
file )
6 {
7
8 ++count;
9
10 file.get( c
);
11 }
12 return count;
13 }
14
15 int main (){
16 int numOfChars;
17 ifstream
inFile;
18 inFile.open("data.in");
19 int
numOfChars = countChars(inFile);
20 cout
<< numOfChars << endl;
21 return 0;
22 }
The file "data.in" contains the following content, where exactly one
blank is used to separate each pair of words. There is NOT any newline
character at the end of the file.
=======BEGIN of data.in===========
To Be or Not to Be, This is the
Question.
========END of data.in============
(1) What does the program output when run against the file "data.in"?
[1%, U3]
(2) List ALL the global variables, local variables, value parameters,
reference parameters, and definitions and calls of user functions
that are defined within this code snippet. [2%, U3]
7. Write a function named smallestInteger,
which takes a parameter bound
of int type and yields the smallest integer k so that k*k*k >= bound. You are not
allowed to use standard functions such as pow(). You can assume that
bound is not negative. It is up to you to decide whether to use value
parameters or reference parameters. [1%, U3]
8. List ALL the binary arithmetic expressions and logical expressions
in the isLeapYear() function below. [2%, U2]
bool isLeapYear (int year) {
return year % 4 ==0 &&
year % 100 !=0 || year % 400 == 0;
}
9. An address manipulation program was discussed in class and its code
can be found online here.
The following questions can be answered based on this program.
(1) List ALL the data members of the class Address. [0.5%, U4]
(2) List ALL the function members of the class Address. [0.5%, U4]
(3) List ALL the constructors, accessors, and mutators of the class
Address. [1%, U4]
(4) List two examples of function declarations. [0.5%, U4]
(5) List two examples of function definitions. [0.5%, U4]
(6) List one example for each of string literals, integral literals,
and named constants. [1%, U1]
(7) List two examples for relational expressions, one example for
compound logical expression, and one example for function calls that
returns boolean. [2%, U2]
10. Write a function find(int a[], int size, int val) that returns the
index of an element in array a that is equal to val, -1 if there is no
such an element. [1%, U4]
11. Write a recursive function in C++ that returns the value of the
mathematical function f(x) defined as follows [1%, U4]
f(x) = f(x-2) x>=10
f(x) = x+2, otherwise
What is the largest possible value for f(x)?