Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit


CSE 340 Programming Language Final Exam,

Summer 2018


Q1: Fully  reduce the following expressions [15 points]

1.1 (λ x . λ y . x y) (λ z . (λ c . c y) z)

1.2 (λ x . x foo) (λ y . y y y)

1.3 (λ c. c (λ x . (λ x . λ y . y)) (λ x . λ y . x)) (λ foo . foo)

 


Q2: Consider the following definition [10 points (2 points for each correct answer and 2 points for explanation)]

         fun f(a, b, c) = a[b] + c <= 1

Using Hindley-Milner type inference, determine the type of f. (Explain Why)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Q3: Consider the following code in C syntax. [10 points]

int a, b;

void print() {

    printf("%d, %d\n" , a, b);

}

void g() {

    a = a + 3;

    b = b + 4;

    print();

}

int f() {

    int a, p;

    a = 1;

    b = b + 1;

    p = 2;

    g();

    return a + b + p;

}

int main() {

    int a, b;

    a = 5;

    b = 7;

    g();

    a = f();

    g();

    return 0;

}

What is the output of this program assuming static scoping?

What is the output of this program assuming dynamic scoping?

 

 

Q4: [12 points] Consider the following C code and assume stack memory allocation for nested scope is used

#include <stdlib.h>

int** p;

int** q;

int main()

{

  int*** a;

  {

    int* b;

    a = (int***) malloc(sizeof(int **)); // memory 1

    b = (int*) malloc(sizeof(int)); // memory 2

    *a = &b;

    // point 1

    b = (int*) malloc(sizeof(int)); // memory 3

    *b = *a;

    p = &a;

    q = &b;

    // point 2

  }

}//point 3

Assume all un-initialized pointers are equal to NULL.

Draw box-circle diagram for a and b at point 1.

Draw box-circle diagram at point 2.

Which memory locations are aliases at point 2?

What are the dangling references at point 1?

What are the dangling references at point 2?

Which memory locations have been deallocated by the time point 3 is reached?

 


Q5: Consider the following code in C syntax, (use static scoping) [10 points]

# include < stdio.h >

int x = 1 ;

int  foo ( int  a , int  c)

{

int x;

int y;

x = a + c;

a = a + 1 ;

y = c;

c = y + 1 ;

return(c);

}

int  main ( )

{

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

x = foo (x , a[x ] ) ;

printf ( " % d - % d - % d - % d \ n " , x , a [ 0 ] , a [ 1 ] , a [ 2 ] ) ;

return 0 ;

}

• 5 points If parameters are passed by value, the output of this program is

• 5 points If parameters are passed by reference, the output of this program is