CSE 428: Exercises on scope, parameter passing, dynamic allocation and deallocation


The superscript "(d)" stands for "difficult". Exercises similar to those marked with "(d)" might appear in candidacy exams, but not in the standard exams of CSE 428.

The superscript "(s)" stands for "solution provided". You can find the solution of these exercises here.

  1. (s) Consider the following sequence of declarations:
       z : integer;
       procedure p (x:integer) < body >;
    Write a < body > for p so that the following piece of code
       z := 1; p(z); write(z)
    will have all different outcomes in case of pass by value, pass by reference, and pass by value-result.
  2. (s) Consider a procedure declaration with two parameters x and y of type integer passed by value-result:
       procedure p(value-result x, y : integer); < body >
    If we have a language that does not allow call by value-result, but only call by reference, how can we define the procedure p so that it has the same effect as the above one?

    Hint: To see the difference between call by value-result and call by reference, consider, for example, the case in which the body consists of the commands

        x:=x+1; y:=y+1
    and the call associates x and y to the same actual parameter.

    Another case in which we observe a difference is when one of the actual parameters (say, z) occurs in the body as a non-local (or global) parameter, and the body contains instructions which modify z.

  3. (s) Consider the following procedure declaration:
       procedure p; 
          x: integer; 
          procedure q; 
             begin x := x+1 end; 
          procedure r; 
             x: integer; 
             begin x := 1; q; write(x) end; 
          x:= 2; 
    What is the output produced by calling p in
    1. A language with static scope
    2. A language with dynamic scope
  4. (s) Consider the following program in C++:
  5.    #include < stdio.h > 
       int x=0; 
       void p(int,int); 
       void main(){
          int x = 1;
       void p(int y, int z){
          x = x+1; 
          y = y+1; 
          z = z+1; 
    1. What is the value printed? (Remember that C++ has static scope.)
    2. Suppose that we modify the declaration of p so to pass the parameters by reference. Namely, we write the declaration as p(int &y, int &z){...}. What is the value printed in this case?

  6. (s) Consider the following fragment of code in Pascal:
       var p,q : ^integer;
       q := p;
       p^ := 1;
       q^ := p^ + 1;
    where foo is a procedure call (whose body does not contain occurrences of either p or q).
    1. What is the value printed by write(p^)?
    2. Describe the situation (environment and state) relative to q after the execution of dispose(p). You can use a drawing if you want.
    3. Discuss what can happen when write(q^) is executed.
  7. (s) Consider the following procedure declaration in C:
  8.    void p() {int x; q = &x; *q = 1;}
    where q is a global variable declared as pointer to int and initialized by q = new int;. What output can we expect to be produced by the following piece of code:
       *q = 0; p(); r(); printf("%d\n",*q);
    where r is a procedure which does not mention q.