READ THIS NOW!

 

·         Print your name in the space provided below.

·         Print your name and ID number on the Opscan form; be sure to code your ID number on the Opscan form.

·         Choose the single best answer for each question — some answers may be partially correct. If you mark more than one answer, it will be counted wrong.

·         Unless a question involves determining whether given C++ code is syntactically correct, assume that it is valid.  The given code has been compiled and tested, except where there are deliberate errors. Unless a question specifically deals with compiler #include directives, you should assume the necessary header files have been included.

·         Be careful to distinguish integer values from floating point (real) values (containing a decimal point). In questions/answers which require a distinction between integer and real values, integers will be represented without a decimal point, whereas real values will have a decimal point, [ 1704 (integer), 1704.0 (real)].

·         The answers you mark on the Opscan form will be considered your official answers.

·         When you have completed the test, sign the pledge at the bottom of this page and turn in the test. 

·         This is a closed-book, closed-notes examination.  No calculators or other electronic devices may be used during this examination.  You may not discuss (in any form:  written, verbal or electronic) the content of this examination with any student who has not taken it.  You must return this test form when you complete the examination.  Failure to adhere to any of these restrictions is an Honor Code violation.

·         There are 28 questions, equally weighted. The maximum score on this test is 100 points.

 

Do not start the test until instructed to do so!

 

 

 

Print Name (Last, First)                                                                                                 

 

Pledge:  On my honor, I have neither given nor received unauthorized aid on this examination.

 

 

 

                                                                                                                                                                               

                                                                                                                signature


I.  Design Representation                                                                                                                                                                             

 

Use the following partial Structure Chart diagrams below as answers for the next 2 questions:

 

 

 

 

 

 

 

 

 

 

 

 

 

 


                (1)                                                (2)                                               (3)                                                  (4)

 

 

Do not make any assumption about variables that are not shown on the chart. Given the following variable definitions:

 

bool  Dogbert,   Wally;

int    Alice, Boss;

 

 

#1 Which of the above structure chart diagrams for Dilbert()correctly models the code segment below?

 

while (Dogbert)

Dilbert(Dogbert, Wally, Alice, Boss);

 
 

 

 

void Dilbert(bool& Dogbert, bool& Wally,

      int Alice, int&  Boss) {

   if (Alice > 0)

       //code under control of if

 
 

 

 

 

 

 

 


#2 Which of the above structure chart diagrams for Dilbert()correctly models the code segment below?

 

 

 



II.  Pointers                                                                                                                                                                                                      

 


Assume the following declarations:

 

const int SIZE = 10;

int x = 0, y[SIZE]={0};

 

int* a; int* b=y;

 

Use the responses:

 

(1) Valid                  (2)  Invalid

 

for the next 6 questions (#3 - #9). Considering each statement below independently, determine whether each statement would compile (not link) without errors after the statement:

 

                a = new int[SIZE];

 

#3   

delete [int] a;           

 

#4   

*b = *&a[SIZE - SIZE];    

 

#5   

a[0] = *y;                

 

#6   

*(y++) = a[SIZE-1];       

 

#7   

&a[0] = &y[1];            

 

#8   

y = new int[SIZE];        

 

 

char *p = new char[3];

char *q = new char[3];

p[0]= ‘\\’; q[1]= ‘\t’;

p[2]= ‘\n’; q[3]= ‘\0’;

 
#9  Identify the logical situation that results from the statements to the right:        

                                                     

(1)  Alias pointer exists                                       (2)  Dangling Reference exists

(3)  Illegal memory address reference               (4)  Memory garbage exists

(5)  Undefined pointer dereferenced                 (6)  No logical error occurs

 

 

char *q = new char[3];

q[0]= ‘Q’; q[1]= ‘\0’;

q++; q++;

q[0] = ‘\n’;

 

 
#10  Identify the logical situation that results from the code fragment at the right:

 

(1)  Alias pointer exists                                       (2)  Dangling Reference exists

(3)  Illegal memory address reference               (4)  Memory garbage exists

(5)  Undefined pointer dereferenced                 (6)  No logical error occurs

 

 

 


#11  What value is printed by the code fragment below?

 

const int SIZE = 10;   // HINT:  sizeof(float) == sizeof(int)

float* x; float* y;

 

x = new float[SIZE];  // assume allocation starts at address 00004004

 

for (int i = 0; i < SIZE; i++)

      x[i] = float(i);

y = &x[1];

y++;

cout << “ y = ” << y << endl;

 

(1)  1      (2)  2      (3) 00004006           

 

(4) 00004008            (5) 0000400C      (6) None of the above

 

Consider the following code:

 

void GetMem (int* const arr,

             int size, int init);

 

const int SIZE = 10;

void main() {

int* a;

 

  GetMem( a, SIZE, SIZE); 

 

  for (int i =0; i < SIZE; i++)

      cout << a[i] << “ ”;

 

  delete [] a;

}

 

//allocate array memory & initialize

void GetMem( int* const arr,

             int size, int init)

{

  arr = new int[size]; //get new array

  for (int* i=arr; size>0; i++, size--)

      *i = init;       //initialize

}

 

#12          In the code above, how is the array int pointer variable  a  being passed to the GetMem() function?

 

                (1)  by value                          (2) by reference                                    (3)  by const reference

                (4)  as a const pointer          (5)  as a pointer to  a const target      (6)   as a const pointer to  a const target

                (7) none of the above

 

#13          Unfortunately the above call to GetMem() does not function as intended. Select the statement below that best describes how to fix the problem.

 

(1)  the integer pointer parameter, a, must be passed as a const reference parameter to prevent a compilation error in GetMem() from occurring when the function allocates the new array.

 

(2)  the integer pointer parameter, a, must be passed as a pointer to  a const target parameter to prevent a compilation error in GetMem() from occurring when the function allocates the new array.

 

(3)  the integer pointer parameter, a, must be passed as a reference parameter to prevent a compilation error in GetMem() from occurring when the function allocates the new array.

 

(4)  the integer pointer parameter, a, must be passed as a const pointer parameter to prevent a compilation error in GetMem() from occurring when the function allocates the new array.

 

 (5)        none of the above

 

Use the responses:

 

(1) Valid                  (2)  Invalid

 

for the next 6 questions (#14 - #19). Considering each numbered question statement in the function below separately, determine whether each statement would be valid or invalid:

 

Assume the following function declarations:

 

void fn(const int* x, int* const y, const int* const z);

 

void main() {

 

  int* p = new int[10];

  int* q = p;

  int* r = p;

  int* s = p;

 

  fn(q, r, s);

 

}

 

void fn(const int* x, int* const y, const int* const z) {

  int a[5] = {0, 1, 2, 3, 4};

 

  x = a;                      //#14: (1)Valid or (2)Invalid ?

 

  x[0] = 5;                   //#15: (1)Valid or (2)Invalid ?

 

  y = a;                      //#16: (1)Valid or (2)Invalid ?

 

  y[1] = 5;                   //#17: (1)Valid or (2)Invalid ?

 

  z = a;                      //#18: (1)Valid or (2)Invalid ?

 

  z[2] = 5;                   //#19: (1)Valid or (2)Invalid ?

 

}

 

 

 

 


III.  Class Basics

 

Assume the following class declaration and implementation:

 


class Safe {

private:

   bool   lock;   //true = locked

   string combo;  //combination

   bool   door;   //true = closed

public:

   Safe(bool key = false,

        string open=”R23L32R33”,

        bool entry = true);

 

   void  OpenDoor();

   void  CloseDoor();

   bool  UnLock  (string open);

   void  Lock();

};

 

Safe:: Safe (bool key, string open,

             bool entry){

   lock  = key;

   combo = open;

   door  = entry; 

}

 

void Safe:: OpenDoor () {

   door = false;

}

 

void Safe:: CloseDoor () {

   door = true;

}

 

bool Safe:: UnLock  (string open) {

   if (combo == open) { //match

      lock = false;

      OpenDoor();

      return( true );

   }

   else return ( false );

}

 

void Safe:: Lock () {

   CloseDoor();

   lock = true;

}

 


 


 


Circle the number of the best answer to each question:

 


#20          What does the following statement accomplish:            Safe Brinks(true, “R6L28R1”, true);

 

(1)   define an instance of the class Brinks.

(2)   define an instance named Brinks of a class Safe that is closed & locked with a non-default combination.

(3)   define an instance named Safe of a class Brinks with a closed & locked with a non-default combination.

(4)   define an instance named Brinks of a class Safe with unknown status.

(5)   define an instance named Safe of a class Brinks with unknown status.

 (6)  None of these

 


#21          What does the following statement accomplish:            Safe Brinks;

 

(1)   define an instance of the class Brinks.

(2)   define an instance named Brinks of a class Safe that is closed & locked with the a non-default combination.

(3)   define an instance named Brinks of a class Safe that is closed & locked with the default combination.

(4)   define an instance named Brinks of a class Safe with unknown status.

(5)   define an instance named Safe of a class Brinks with unknown status.

 (6)  None of these

 

 

#22          How many of the member functions in the Safe class should have been declared as const member functions?:        

 

(1) 1          (2) 2       (3) 3       (4) 4      

(5) 5          (6) 6       (7) 7       (8) 0

 

#23          How many “reporter” member functions does the Safe class declaration contain?

 

(1)   1                              (2)  2                       (3)           3

(4)   4                              (5)  0                       (6)           None of the above

 


#24          What do the following statements accomplish:              Safe Brinks();

                                          UnLock.Brinks(“R23L32R33”);

 

(1)   instructs the Safe object Brinks to test the entered combination.

(2)   instructs the Safe object Brinks to disengage its lock and open its door.

(3)   instructs the Safe object Brinks to try and open its door.

(4)   the statement contains a syntax error

(5)   None of these

 

#25          What do the following statements accomplish:             

 

bool CrackSafe (Safe vault, string combination);

 

// in main ()

Safe Brinks;

if (CrackSafe(Brinks, “R23L33R32”))

      cout << “***Break In Attempted***”;

 

 

bool CrackSafe (Safe vault, string combination) {

         return( vault.combo != combination ); 

}

 

(1)   causes the Brinks object to display a Break In Attempt message

(2)   causes the Safe object to display a Break In Attempt message

(3)   does not cause the Brinks object to display a Break In Attempt message

(4)   does not cause the Safe object to display a Break In Attempt message

(5)   None of these

 


IV.  Separate Compilation                                                                                                                                                                             

 

For the next two questions, consider a C++ program composed of two cpp files and two corresponding header files, as shown below.  All function calls are shown, as are all include directives, type declarations and function prototypes.  In the source and header files, there should be only one physical occurrence of a function prototype, and one physical occurrence of a type declaration.  Do not assume that any preprocessor directives are used but not shown.

 

 


// structs.h

#ifndef STRUCTS_H

struct That {

   . . .

};

#endif

 

// main.cpp

#include "structs.h"

#include "midterm.h"

. . .

int main() {

   That R;

   Fn(R);

   . . .

}


// midterm.h

#ifndef MIDTERM_H

#include “structs.h”

void Fn(That rec);

#endif

 

// midterm.cpp

#include "midterm.h"

. . .

void Fn(That rec) {

   . . .

}

 


 

 

26.   If the organization shown above is used, and no preprocessor directives are added, what will the compiler complain about when main.cpp is compiled?

 


1)     Nothing.

2)     Multiple definitions for That.

3)     Multiple definitions for Fn().

4)     Both 2 and 3.

5)     Undeclared identifier That.

6)     Undeclared identifier Fn().

7)     Both 5 and 6.

8)     2, 3, 5, and 6

9)     None of these.


 

 

27.   If the organization shown above is used, and no preprocessor directives are added, what will the compiler complain about when midterm.cpp is compiled?

 


1)     Nothing.

2)     Multiple definitions for That.

3)     Multiple definitions for Fn().

4)     Both 2 and 3.

5)     Undeclared identifier That.

6)     Undeclared identifier Fn().

7)     Both 5 and 6.

8)     2, 3, 5, and 6

9)     None of these.


 

 

28.   (True or False) The inclusion of the #include "structs.h"  statement in  main.cpp does not matter, (i.e. if #include "structs.h"  were omitted from main.cpp the same compilation as above would result?

 


1)     True

 

2)     False