Name: __________________________________________________________________

 

Directions: Neatly write your response to each question in the space provided on the exam sheet or on your own scratch paper.  This is a closed book/closed notes exam worth 300 points.  You have fifty minutes to complete the exam.  If you finish early, check your work thoroughly (place a large x through problems on any pages you do not want graded) and hand in all materials including scratch paper.  Then, be courteous to your classmates and quietly leave the classroom.  Good luck!

 

1.      Given the following C-like program:

 

float c, b;

void a (int c) {

      char d,e;

      … }            ß Point 1

void b (float c, h) {

      int f, g;

      … }

void main () {

      int e, float d;

      a(e);

      … }

 

where main() calls on function a(). 

 

When execution reaches Point 1 in function a(), name the variables , their types, and program components  they are from (i.e., function name or static memory) such that the variables are:

 

      a.   (10 Points) live and in the current scope. _____________________

 

      b.   (10 Points) live but not in the current scope._____________________

 

      c.   (10 Points) in the current scope but hidden._____________________

 

      d.   (10 Points) not live._____________________

 


2.   Given the following C-like program segment:

     

      typedf float a;

      union foo {

            char b,c;

            a d; }

      struct bar {

            float e;

            int f; }

      foo.b = “x”;

      foo.c = “y”

 

     

Assume that the next available offset at the beginning of compilation of the code segment is 18 and that:

 

·        char variables consume 1 offset

·        int variables consume 2 offsets, a

·        float variables consume 4 offsets

 

a.   (20 Points) Draw symbol table entries for the code segment entering information

      such as name, offset, and type in the symbol table descriptors.

 

 

 

 

 

 

 

 

 

 

 

 

b.   (20 Points) Show what the activation record the code segment is contained in will

      look like when the function that contains it is in execution. Assign the proper

       number of offsets to each variable  ( I want you to show me how data is allocated

      to this code segment).

 


3.   a.   (10 Points) What is a strongly typed language.

 

 

b.   (10 Points) Name one important way that functions keep C and C++ from

            being strongly typed languages.

 

 

c.   (10 Points) Name one important way that arithmetic expressions keep C and

            C++ from being strongly typed languages.

 

 

d.   (10 Points) Name one more important reason that C and C++ are not strongly

            typed that is not related to functions and arithmetic expressions.

 

 

4.   a.   (10 Points) What is a named constant?

 

 

b.   (20 Points) Write a short program segment that shows how a named constant

      could be used to your advantage.  Include a brief explanation of why you are

      using the named constants next to your program segment.

 

 

 

 

 

 

c.   (20 Points) How do Pascal, FORTRAN 90 and C++ differ in the definition of

      their named constants?

 

 

5.   a.   (10 Points) Circle any of the following that could cause dangling pointers:

 

      (1) Explicit deallocation of pointers

      (2) Deallocation of pointers at the end of a function call.

      (3) Uninitialized pointers.

      (4) No deallocation of pointers at the end of a function call.

 

b.   (10 Points) Circle any of the following that could cause dangling objects.

 

      (1) Explicit deallocation of pointers

      (2) Deallocation of pointers at the end of a function call.

      (3) Uninitialized pointers.

      (4) No deallocation of pointers at the end of a function call.

 


6.   (10 Points)  In C and C++, pointer variables can be explicitly dereferenced with

the * operator.  For example, *p returns the value pointed to by pointer p. 

However, C and C++ also provide for the implicit dereferencing of pointers in

some specific cases.  Name two of those cases.

 

 

 

7.   Given the following C-like declarations:

     

      typedef int a;

      typedef int b;

      a c;

      b d;

 

a.   (10 Points) Are the variables c and d structure compatible?  Why?

 

      b.   (10 Points) Are the variables c and d name compatible?  Why?

 

c.   (10 Points) If the variables c and d are compatible, what does that mean?

 

8.   Given the following C-like code segment:

 

      int j;

      int list[10];

      int *ptr;

      ptr = list;

 

Give three different aliases for list[j] (not including list[j]).

 

      (1) (10 Points)___________________________________

 

      (2) (10 Points)___________________________________

 

      (3) (10 Points)___________________________________

 

9    a.   (10 Points) What is an ordinal type?

 

 

      b.   (10 Points) Name two basic types (i.e., defined by the compiler) that are ordinal

            types.

 

 

      c.   (10 Points) Name one user-defined type that is an ordinal type.

 

10. (10 Points) How are operations on C and C++ strings defined?  Name four

      string operations available in C and C++.