Docsity

Midterm Exam with Key - Programming Languages | CS 4610, Exams of Programming Languages

Toggle sidebar
bg1
CS415 Anderson – Fall 2003 Page 1 of 11
CS 415 Midterm Exam – Fall 2003
Name __________________KEY______________________________
Email Address _________________ Student ID # ___________________
Pledge:
This exam is closed note, closed book. Questions will be graded on quality of answer.
Please supply the best answer you can to each question. Good Luck!
Score
Fortran & Algol 60
Compilation
Names, Bindings, Scope
Functional Programming
Logic Programming
Control Flow
Total /72
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Midterm Exam with Key - Programming Languages | CS 4610 and more Exams Programming Languages in PDF only on Docsity!

CS 415 Midterm Exam – Fall 2003

Name __________________KEY______________________________

Email Address _________________ Student ID # ___________________

Pledge:

This exam is closed note, closed book. Questions will be graded on quality of answer. Please supply the best answer you can to each question. Good Luck!

Score

Fortran & Algol 60

Compilation

Names, Bindings, Scope

Functional Programming

Logic Programming

Control Flow

Total /

Fortran and Algol 60 (11 points)

  1. [1 point] Fortran uses pass by __ reference _____ for parameters.
  2. [1 point] Algol was designed by _ a committee of 8, in Zurich in 1958 ______.
  3. [5 points] List 3 significant differences between Fortran and Algol 60.

(Many different answers possible here)

  1. [4 points] Algol 60 introduced blocks of statements. List three reasons why these were useful in Algol 60.
    • programs easier to read (compared to gotos in Fortran)
    • supported structured programming
    • can use in place of a single statement
    • allows nested scopes
    • efficient storage management (allocate on entrance to a block and de-allocate on exit)
    • allows a fix to the dangling else problem
  1. [2 points] Write a regular expression for an identifier in the cs415 language. Identifiers can consists of only letters (upper and lowercase) and digits, and must start with a capital letter and end with a digit.

[A-Z] [A-Za-z0-9] [0-9]*

  1. [2 points] Draw a finite automaton for the regular expression you gave in question 3 above. Be sure to properly denote the accepting states.
  2. [3 points] Describe the dangling else problem.

In the example below, it can be unclear which if stmt the else clause matches with. This can be solved in a number of ways, either by having an else should match the closest unmatched then, or by requiring explicit end markers to show the end of an IF statement.

if cond then if cond then stuff else other stuff

start

A-Z 0-

accept

A-Za-z0-

Names, Scopes, and Bindings (10 points)

  1. [4 points] What does the function test print if the language uses static scoping? What does it print with dynamic scoping? (otherwise assume C++ syntax and semantics).

int n = 100; // global

print_plus_n(int x) { cout << x + n; } increment_n() { n = n + 1; }

test() { int n; n = 1; print_plus_n(25);

n = 33; print_plus_n(n);

increment_n(); cout << n;

print_plus_n(n); }

With Static Scoping:

125 133 33 134

With Dynamic Scoping:

26 66 34 68

Functional programming (14 points)

  1. [5 points] At several schools (MIT, Berkley, Rice) Scheme is the programming language used in introductory programming courses. Give at least 3 reasons why Scheme could be argued to be well-suited for this purpose?

A few reasons:

**- garbage collection (don’t need to worry about memory management)

  • simple syntax makes it easy to learn
  • if used in a purely functional style, no side effects will be used, this tends to avoid bugs related to un-expected side effects and makes it easier to reason about programs
  • good environments/interpreters are available free of charge (Dr. Scheme)
  • benefits of using an interpreter – fast response (don’t have to wait to recompile the whole program each time you change something, makes it easy to experiment), good error messages
  • closely related to math functions – draws on students’ math reasoning abilities.**
  1. [2 points] What is the result of the following in Scheme. (Be specific)

(define y (lambda (x y) (+ x y)))

y is bound to a function that takes two parameters and returns their sum

  1. [2 points] Assuming that the following definitions are executed in this order:

(define b ‘(3 14 27)) (define c (cons (car (cdr b)) (list ‘a ‘b ‘c))))

What is the result of typing the following into the Scheme interpreter:

c => (14 a b c)

(car (cdr (cdr c))) => b

  1. [5 points] Write a recursive Scheme function, double_each that takes a single parameter that is a list and returns a list that contains all of the elements of that list doubled. You can assume that you will always be passed a list, and one that contains only integer values.

Example: (double_each ‘(4 5 6)) => (8 10 12)

(define (double-each x) (if (null? x) () (cons ( 2 (car x)) (double-each (cdr x)))))*

  1. [3 points] More Prolog:

likes(mary,marlins). likes(fred,dracula). likes(fred,bats). likes(sue,yankees). likes(sue,bats). likes(fred,baseball). likes(fred,screaming). likes(fred,marlins). likes(mary,ghosts). likes(bob,cubs). likes(bob,pumpkins). likes(bob,dracula). likes(mary,bats).

friends(X,Y, Something) :- likes(X, Something), likes(Y, Something), not(X = Y).

What does this query produce? Give the complete set of responses obtained after typing ; until a “No” response is given.

?- friends(fred,X,Y).

X = bob Y = dracula ;

X = sue Y = bats ;

X = mary Y = bats ;

X = mary Y = marlins ;

No

Control Flow (10 points)

  1. [5 points] Define side effect and short circuiting. Give an example where short circuiting and side effects could lead to unexpected results (if you didn’t know that short circuiting was being used, for example).

side effect – influencing subsequent computation in any way other than returning a value for use in the surrounding context.

short circuiting – (of Boolean expressions) skips over the second half of an AND if the first part is false, skips over the second part of an OR if the first part if true. (Since in both cases the value of the expression can already be determined based on the value of the first part of the expression.)

Generic example: ( false AND side effect-producing code) If short circuiting is used, then the side effect-producing code would never be executed when the first condition is false. ( cond1 AND i++) If i is meant to count the number of times the condition is tested, it will not be incremented whenever the cond1 is false.

  1. [4 points] Give an example of a tail recursive function. (This can be in any language, and does not have to be perfect syntax, or to actually do anything useful.) What is an optimization that can be applied to tail recursive functions?

int func (int x) { if (x == 0) return 1; else … stuff … return func (x – 1); }

An optimization is to convert the recursive call to a loop. In this way we avoid the overhead of function call and return and can re-use the stack frame from the original call (we don’t need a new stack frame for the recursive calls anymore).

  1. [1 point] Macros in C++ are an example of __ Normal _______ Order evaluation.