True/False (2 points each, 20 points total)

  1. _____ In general, a program written in an interpreted language executes faster than a compiled language program for the same task.
  2. _____ Call-by-reference and call-by-name are both permitted in most modern programming languages.
  3. _____ If the grammar for a language is ambiguous, then every valid program in that language has at least one parse tree.
  4. _____ Objects that belong to a set type, such as that provided in Pascal and Modula, are usually implemented using bit strings.
  5. _____ In a context-free grammar, there can be only one non-terminal symbol on the left side of any production in that grammar.
  6. _____ In Extended Backus-Naur Form (EBNF), the so-called Kleene star (*) is used to say that something may occur zero or more times.
  7. _____ The while and repeat are both forms of iteration.
  8. _____ The case statement can be implemented with a jump table, or a hash function.
  9. _____ In C, a functionís pre-conditions and post-conditions can sometimes be checked with the assert facility.
  10. _____ Memory leaks occur, for example, when a variable is freed before it is allocated.

Short answer questions (5 points each,15 points total)

1. What is the postfix expression equivalent to the following prefix expression?

+ + * A B + C D E

2. What is the infix expression equivalent to the following postfix expression? (Warning! You may need to add parentheses.)

C D G / + M Q / * Z +

3. Whatís the difference between static and dynamic type checking? Name one advantage and one disadvantage of dynamic type checking.

 

(15 points)

What is the output of the following Modula program? The program compiles correctly as shown. (You donít have to worry about leading or trailing blanks.)

 

MODULE puzzle;

FROM InOut IMPORT WriteLn, WriteCard;

VAR a, b : CARDINAL;

PROCEDURE P1 ( VAR a: CARDINAL; b: CARDINAL);

BEGIN

a := a + 1;

WriteCard(a,5);

WriteLn;

WriteCard(b,5);

WriteLn

END P1;

PROCEDURE P2 (x: CARDINAL; VAR y: CARDINAL);

BEGIN

WriteCard(a,5);

WriteLn;

WriteCard(b,5);

WriteLn;

P1(y, x);

END P2;

BEGIN

a := 2;

b := 3;

P1(a, b+2);

WriteCard(a,5);

WriteLn;

WriteCard(b,5);

WriteLn;

P2(a, b);

END puzzle.

 

 

(15 points)

Most programming languages allow some sort of array. In most languages, integers are used as subscripts. In C, single characters can be used as subscripts. In Pascal and Modula, enumeration types may also be used as subscripts. In still other languages, character strings (not just single characters) can be used as subscripts. How might a language facility that allows arrays to be indexed by character strings be efficiently implemented?

 

(15 points)

Given the following BNF grammar, draw a parse tree for the string baababb

S ::= a B

S ::= b A B

A ::= a

A ::= A A b

B ::= b

B ::= b S

B ::= a B B

 

 

 

(20 points)

The following grammar is ambiguous. Find a string that demonstrates this, and draw two of that stringís parse trees. (Hint: at least one such string is four characters or less in length.)

S ::= a B

S ::= a a B

B ::= b

B ::= a b