ICS 313: Fundamentals of Programming Languages (991)                                                          Section: 03

 

Date:   27th October 1999                                          Quiz #2                                                         Time: 20 Minutes

 

Question 1:

Answer only three of the following parts                                                                                               (6 points)

i)        Define functional side effect.

are the changes that are made by the function to its argument (s) or non-local (global) variables.

ii)       Define what is coercion.

is the process of implicit conversion of some of the operands of an operator, or a function, to a compatible type of the other operand(s).

iii)     What are the design issues for selection structure?

·         What is the form and type of the expression that control the selection?

·         Can a single statement, a sequence of statements, or a compound statement be selected?

·         Is the entire construct encapsulated in a syntactic structure?

·         How should unrepresented selector expression values be handled, if at all?

·         Is execution flow through the structure restricted to include a single selectable segment?

iv)     What is the difference between a regular and a guarded commands selection structure?

In regular selection structure conditions are evaluated once at a time, and when one is satisfied the segment of code associated with it is executed. With guarded command all conditions are evaluated at once and if more than one is satisfied then one of these code segments is executed at random.

v)      What are the three general characteristics of subprograms?

·         Each subprogram has a single entry point.

·         The calling program unit is suspended during the execution of the called subprogram, i.e., one subprogram is in execution at any given time.

·         Control always returns to the caller when the subprogram execution terminates

vi)     What is a parameter profile and what is a subprogram protocol?

The parameter profile of a subprogram is the number, order and the types of its formal parameters. The protocol of a subprogram is its parameter profile, plus, if it is a function, its return type.

 

Question 2:

a)   Consider the following C program:                                                                                           (3 points)

int fun (int *i)   {

      *i +=5;

      return 4;

}

void main ()      {

      int x = 3;

      x = x+ fun (&x);

}

What is the value of x after the assignment in main, assuming operands are evaluated right to left. (You need to show how did you find the value)

Since evaluation is from right to left then fun(&x) will be evaluated first. This will result in returning the value 4 plus changing x from 3 to 8. Now the addition will be performed between 4, which the value of fun(&x) and 8, which is the value of x resulting in assigning x the new value of 12.

 

 

b)   Consider the following C program:                                                                                           (6 points)

void main ()      {

      int value = 1; list[2] = {3,5};

      swap(value, list[0]);

}

void swap (int a, int b)       {

      int temp;

      temp = a;

      a = b;

      b = temp;

}

 

What are the values of the variables value, list[0] and list[1] after the call to swap assuming the parameters are passed by:

              i.            Value:            value = 1, list[0] = 3, list[1] = 5

This because no change is done to the parameters value and list[0] since they are passed by value All the swapping was made to the local variables a and b.

            ii.            Reference:     value = 3, list[0] = 1, list[1] = 5

This resulted from passing the address of both value and list[0] for a and b and therefore, the swap actually took place on the memory location of value and list[0].

            iii.            Name:           value = 3, list[0] = 1, list[1] = 5

This is because the textual replacement of the value and list[0] for the a and b in the subprogram will make it look like the following:

void  swap (int a, int b)         {

         int temp;

         temp = value; ----    (1)

         value = list[0]; ---    (3)

         list[0] = temp; ---     (1)

So the swapping took place actually between value and list[0].

           iv.            Value-result:  value = 3, list[0] = 1, list[1] = 5

This resulted from passing the values of both value and list[0] to a and b and the swap actually took place between a and b then the result was copied back to value and list[0]