Review Problems for Exam 1
CS 316, Spring 2003

NOTE: The midterm exam is on Tuesday, March 11.

The exam will cover the following material. From the textbook Programming Language Pragmatics, Chapter 1, Section 1.6 (pages 15-24), Chapter 2, Sections 2.1, 2.2.1, and 2.2.2 (pages 31-51), and Chapter 3, Sections 3.1-3.3 (pages 105-122), Sections 3.4-3.6 (pages 139-155). From the online Scheme textbook The Scheme Programming Language, all of Chapter 2, Sections 2.1-2.9, and Section 3.2 and Section 3.5.

Problem 1:
For each of the following errors, determine if the error is a syntactic error, a static semantic error, or a dynamic semantic error? Explain each of your answers.
  • (a) A function defined to have two formal parameters is called with only one actual parameter.
  • (b) An array named A is declared to have 100 entries, numbered from 0 to 99, and elsewhere in the code there is the expression A[100].
  • (c) The identifier f is declared to be the name of a function. Elsewhere in the code there is the expression f+100.
  • (d) The identifier g is declared to be the name of a function. Elsewhere in the code there is the expression g(0].

Problem 2:
Answer the following Review Questions from pages 97-98 of Programming Language Pragmatics: 2.7, 2.9, 2.10, 2.12, 2.13 and 2.21.

Problem 3:
For each of the following expressions, rewrite the expression in both prefix and postfix notation, and draw an abstract syntax tree (i.e., an expression tree) for the expression.
(a) a * b + c
(b) a * (b + c)
(c) a * b + c * d
(d) a * (b + c) * d

Problem 4:
Describe in English the language defined by the following grammar.
         <S> -> <A> <B> <C>
         <A> -> 'a' <A> | 'a'
         <B> -> 'b' <B> | 'b'
         <C> -> 'c' <C> | 'c'

Problem 5:
Consider the following grammar.
         <S> -> <A> 'a' <B> 'b'
         <A> -> <A> 'b' | 'b'
         <B> -> 'a' <B> | 'a'
Which of the following words are in the language generated by this grammar?
(a) baab
(b) bbbab
(c) bbaaaaa
(d) bbaab

Problem 6:
Consider the following grammar.
         <S> -> 'a' <S> 'c' <B> | <A> | 'b'
         <A> -> 'c' <A> | 'c'
         <B> -> <A> | 'd'
Which of the following words are in the language generated by this grammar?
(a) abcd
(b) acccbd
(c) acccbcc
(d) acd
(e) accc

Problem 7:
For each of the following parts, define a grammar for the words, over the alphabet containing just 0 and 1, that are described.
(a) All words ending with a 1.
(b) All words of length exactly 8.
(c) All words that contain the string 0101.
(d) All words with odd length. Show how to parse the word 00100 using your grammar.

Problem 8:
The following grammar is motivated by declarations in C.
Declaration ::= Type Declarator ';'
Type ::= 'int' | 'char'
Declarator ::= '*' Declarator
            |  Declarator '[' Digit ']'
            |  Declarator '(' Type ')'
            |  '(' Declarator ')'
            |  Letter
Letter ::= 'a' | 'b' | ... | 'y' | 'z'
Digit  ::= '1' | '2' | ... | '8' | '9'
(a) Give 5 examples of sentences generated by this grammar.
(b) Show that this grammar is ambiguous.

Problem 9:
The following C function computes the number of decimal digits in a positive integer.
      int numdigits(int x)
      {  int t = x, n = 1;
         while (t >= 10)
         {  n++;
            t = t/10;
         }
         return n;
      }
(a) Implement this function in Scheme using a tail recursive function.
(b) Modify your function from part (a) so that it works with negative integers.

Problem 10:
Implement (invert lst) where lst is a list of 2-lists (lists of length 2) and the return value is a list with each 2-list reversed. So
(invert '((a 1) (b 2) (c 3) (d 4))) ==> ((1 a) (2 b) (3 c) (4 d))

Problem 11:
The following function implements a simple mathematical function that might come up in any algebra or calculus course. Write this mathematical function using standard mathematical notation. Explain in detail how this Scheme function implements the mathematical function.
      (define f
        (lambda (x y)
          ((if (> y 0) + -) x y)))

Problem 12:
Each of the following is a syntactic form. For each of these forms, explain how the evaluation of the form differs from the rule for evaluating a function application. In other words, give the evaluation rule for each of these forms.
(a) (if exp1 exp2 exp3)
(b) (and exp1 exp2)
(c) (define var exp)

Problem 13:
Is the following function tail recursive? Explain your answer. Also, explain what the function does.
       (define f
         (lambda (n)
           (if (= n 0) 0 (+ 1 (f (- n 1))))))

Problem 14:
In each of the following expressions, draw an arrow from each variable reference to its associated declaration as a formal parameter.
        (lambda (x)
          (lambda (y)
            ((lambda (x)
               (x y))
             x)))
        (lambda (z)
          ((lambda (a b c)
             (a (lambda (a) (+ a c)) b))
           (lambda (f x)
             (f (z x)))))

Problem 15:
If two procedures f and g are both procedures of a single argument such that the values produced by g are legal arguments to f, then the composition of f and g is defined to be the procedure that first applies g to its argument and then applies f to the result. Write a procedure called compose that takes any two one-argument procedures and returns the procedure that is their composition. For example,
((compose sqrt abs) -4) ==> 2

Problem 16:
Consider the following definitions.
        (define make-scaled
          (lambda (scale f)
            (lambda (x)
              (* scale (f x)))))

         (define add-one
           (lambda (x)
             (+ 1 x)))

         (define mystery
           (make-scaled 3 add-one))
For each of the following questions, be sure to explain how you arrived at your answer.
(a) What is the value of (mystery 4)?
(b) What is the value of
((make-scaled 2 (make-scaled 3 add-one)) 4)

Problem 17:
In the following C program there are two assignments to the entity **x. For each of these assignments, draw a picture that would help someone visualize the state, at that point in the program, of all of the variables and their memory locations being used by this program. Also, what does this program print?
        #include <stdio.h>

        int main()
        {  int **x;
           int *y;
           int z;
           x = (int**) malloc(sizeof(int*));
           y = (int*) malloc(sizeof(int));
           z = 1;
           *y = 2;
           *x = y;
           **x = z;
           printf("%d\n", *y);
           z = 3;
           printf("%d\n", *y);
           **x = 4;
           printf("%d\n", z);
           return 0;
        }

Problem 18:
The following program prints two integer values; the first value typically is garbage (explain why) but the second value might be 2. Give a detailed explanation of why the second value could be 2. The second value could also be garbage (but it is much less likely). Give an explanation of why the second value could be garbage (Hint: The activation record need not be on a stack).
        #include <stdio.h>

        void p()
        {  int y;
           printf("%d\n", y);
           y = 2;
        }
        int main()
        {  p();
           p();
           return 0;
        }

Problem 19:
The following program prints two integer values; the first value typically is garbage (explain why) but the second value might be 2 or it could also be garbage. Give a detailed explanation of why the second value could be 2. Give a detailed explanation of why the second value could be garbage.
        #include <stdio.h>

        int main()
        {
           {  int x;
              printf("%d\n", x);
              x = 2;
           }
           {  int y;
              printf("%d\n", y);
           }
           return 0;
        }

Problem 20:
Consider the following outline of a Pascal program.
         program main;
            var x, y, z : integer;
            procedure sub1;
               var a, y, z : integer;
               procedure sub2;
                  var a, b, z : integer;
                  begin {sub2}
                     { some code }
                  end; {sub2}
               begin {sub1}
                  { some code }
               end; {sub1}
            procedure sub3;
               var a, x, w : integer;
               begin {sub3}
                  { some code }
               end; {sub3}
            begin {main}
               { some code }
            end. {main}
For each of the procedure bodies sub1, sub2, and sub3, list all of the variables that are visible in the body and specify where each visible variable was declared. Assume static scoping.

Problem 21:
Consider the following outline of a program written in a block structured language that uses dynamic scoping.
         program main;
            var x, y, z;
            procedure sub1;
               var a, y, z;
               begin
                  // some code
               end;
            procedure sub2;
               var a, b, z;
               begin
                  // some code
               end;
            procedure sub3;
               var a, x, w;
               begin
                  // some code
               end;
            begin // main
               // some code
            end.
(a) Suppose that main calls sub1, sub1 calls sub2, and sub2 calls sub3. List all of the variables that are visible in the activation of sub3 and specify where each visible variable was declared.
(b) Suppose that main calls sub2, sub2 calls sub3, and sub3 calls sub1. List all of the variables that are visible in the activation of sub1 and specify where each visible variable was declared.
(c) Suppose that main calls sub3 and sub3 calls sub1. List all of the variables that are visible in the activation of sub1 and specify where each visible variable was declared.


Return to the main homework page.
Return to the CS 316 home page.


compliments and criticisms