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)

  1. _1_
  2. X0000a
  3. a long name
  4. 2BOrNot2B
  5. what?
  6. 12345.6789
  7. 1,024
  8. –0.00000000006
  9. 'AB'
  10. " 'AB' "

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)

  1. i + f / j
  2. -j / i + ++c
  3. c % i / j
  4. i > 10
  5. 5 + i > j + 10
  6. c / c || i == j

 

 

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:

  1. five.four
  2. five.getThree()
  3. six.five
  4. six.five.two
  5. six.getThree()
  6. six.getThree().four

 

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 << "?";