CS177 C++ Programming
Test 1 Sample Questions

Answers

1

(a) Name three key features of object oriented programming. Is C++: (b) compiled or interpreted ; (c) statically or dynamically type-checked? (d) a subset of ANSI C, a superset of ANSI C, or neither?

2

(a) What is the purpose of access control to data values?

(b) In the code fragment below, which variables are accessible and which

are not?

class A {
   int x;
   int y, z;
public:
   int a;
private:
   int b, c;
};

(c) How may a private variable be made public to one selected function only?

3

Evaluate the following expressions assuming that the initial

values given are set for each one:

int i = 2; int j = 3; char c = 'Z'; float f = 32.2; (Z has ASCII value 90)
    1. Expr1: 1 <= j < 3
    2. Expr2: c - 20 / 10 % j
    3. Expr3: i -- - -- j
    4. Expr4: i + j + f / 2
    5. Expr5: j = 5 && i
    6. Expr6: j += 5 && c
    7. Expr7: j += c -= 2 * i
    8. Expr8: i - 2 || c || j / 10

4

In the following program fragment, state what the scope and lifetime is of each of the names (include all the names).

int x;
extern int y;
static int z;

void function1(int a, int b)
{
   int c;
   static int d;

   while (c) {
     int e;
     e = c + 1;
     { int e = 10;
       if (e == c) 
         return;
     }
   }
}

5

Given this class declaration:

class A {
private:
  int x, y;
public:
       A() { x = y = 10; }
       A(int xx, int yy) { x = xx; y = yy; }
   ...
};

which of the following will finish with an object a having x = 10 and y = 5?

    1. A a(5);
    2. A a(5, 10);
    3. A a();
    4. a.y = 5;
    5. A a(10, 5);

6

For the class A, below, define the two constructors given in appropriate ways.

class A {
  const int y;
  int x;
public:
      A() ....????....
      A(int xx) ....????....
  ...
};

7

In what two situations are reference variables best used Which of the following are correct and which are not, give a suitable declaration of class A?

    1. A &a;
      A b;
      a = b;
    2. A a, b;
      A &c = a;
      c = b;
    3. A a, b;
      A &c = a;
      b = c;

8

What does the following program print?

#include <iostream.h>

int main()
{

class A {
private:
  char *str;
public:
      A(char *s) { str = s; }
  char operator-(int i) { return str[i]; }
};

A s1("this is a string");

cout << s1-8 << endl;
}