CS177/457, C++ Programming
Spring, 1998
Examination 1
Closed Book. Answer all the questions. The total points for each question or part of a question follows it in parentheses, thus: (nn)
1
Which of the following have valid C syntax and which do not? (20)
2
For each of the following, evaluate the expression with the following values for the variables: int c = 'B' (ASCII value 66), int i = 10, int j = 2, float f = 12.2. (24)
3
Examine the declarations below and answer the questions that follow.
class One {
public:
int two;
private:
int getThree() { return four; }
int four;
};
class Two {
One four;
public:
int two;
One five;
int getThree() { return four; }
};
One five;
Two six;
Are these expressions valid or invalid? (18) Explain your answer:
4
What does the following program print? (20)
#include <iostream.h>
class Hide {
int x;
public:
Hide() { x = 10; }
void fm1(int y) { x = y; }
int fm2() { return x * x; }
};
int main() {
Hide now, then;
int one = now.fm2();
then.fm1(one);
cout << then.fm2();
}
5
What does the following program fragment print? (18)
int abc = 4, def = 5;
for (int i = abc; i < 7; i++) {
if (i == def)
cout << "this ";
else
if (i < def)
cout << "is ";
else
cout << "it";
}
cout << "?";