0% found this document useful (0 votes)
62 views4 pages

cs342 Old Midterm

This document appears to be an exam for a programming languages course. It contains a multiple choice section with 14 questions about programming languages and concepts. It also contains a written response section asking students to complete 4 programming tasks in Scheme, analyze variable scoping in a code sample, and outline steps to port a C compiler to a new machine architecture. Students are instructed to indicate which 3 of the 4 written response questions they want graded.

Uploaded by

anagha141516
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views4 pages

cs342 Old Midterm

This document appears to be an exam for a programming languages course. It contains a multiple choice section with 14 questions about programming languages and concepts. It also contains a written response section asking students to complete 4 programming tasks in Scheme, analyze variable scoping in a code sample, and outline steps to port a C compiler to a new machine architecture. Students are instructed to indicate which 3 of the 4 written response questions they want graded.

Uploaded by

anagha141516
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Name:

Student No:

Department of Computer Science


University of Western Ontario
CS 342 Organization of Programming Languages
October 24, 2005

TIME: 1 hour and 45 minutes


Aids Allowed:
• One single-sided 8.5x11 inch sheet of handwritten notes (not mechanically reproduced).
• A copy of the Revised5 Report on the Algorithmic Language Scheme (supplied).

This test has two parts, with a total value of 50 points.

Part I: Multiple Choice [14 Points – 1 point each]


For each of the questions, circle exactly one of A, B, C or D corresponding to the best answer.

1. The main programming language used in the field of Numerical Analysis is:

(A) Java (B) Haskell (C) Fortran (D) Assembler

2. A programming language used heavily in the field of Artificial Intelligence is:

(A) Snobol (B) Lisp (C) Fortran (D) C++

3. Which of the following languages does not natively support garbage collection?

(A) Common Lisp (B) C++ (C) Java (D) Scheme

4. Which of the following programming languages is the oldest:

(A) C++ (B) Java (C) Perl (D) Lisp

5. Which of the following cannot be described by a regular expression?

(A) C-style comments (B) Scheme-style comments


(C) Well nested parentheses (D) Dates in mail messages

6. A language most heavily used in the design of digital circuits is:

(A) DCDL (B) HDTV (C) VHDL (D) VRML

7. The number of programming languages in widespread use today is closest to:

(A) 3 (B) 300 (C) 30,000 (D) 3,000,000

1
Name:
Student No:

8. Which of the programming styles or techniques is least well supported by Scheme?


(A) Functional programming
(B) Imperative programming
(C) Object oriented programming
(D) Recursive programming

9. Which of the following does not give a list in Scheme?


(A) ’(a . (b . (c . ())))
(B) ’(a b c)
(C) #(a b c)
(D) (cons a (cons b (cons c ’())))

10. Which of these Scheme inputs produces a value most similar to a C array?

(A) ’(.1 .2 .3) (B) (list .1 .2 .3) (C) #(.1 .2 .3) (D) (1 . 2 . 3)

11. Which of the following gives a result different from the others in Scheme?

(A) (cons ’a (cons ’b (cons ’c ’()))) (B) (cons ’a ’(b . (c d)))


(C) ’(a b . c) (D) ’(a b c)

12. Which of the following Scheme fragments always evaluates x?

(A) (define f (lambda () (* x y z))) (B) (define v ’(x y z))


(C) (define g (and x y z)) (D) (define u "x y z")

13. Consider the following Scheme program fragment.

(lambda (m n)
(let ((k (- m n)) (h (* m n)))
(+ h k x)))

Which of the following best describes the identifiers in that fragment?


(A) m, n are bound. k, h are determined.
(B) m, n, k, h are free. x is bound.
(C) m, n, k, h are bound. x, +, -, * are free.
(D) m, n, k, h are free. x, +, -, * are bound.

14. In Scheme, which of the following never evaluates all of its subexpressions:
(A) (and a b #\f c d e)
(B) (or a b #\f c d e)
(C) (and a b #f c d e)
(D) (or a b #f c d e)

2
Name:
Student No:

Part II: Written Answers [36 Points]


Each of questions II.1, II.2, II.3, II.4 is worth 12 points.

Indicate here which three you want marked.


If you do not indicate which three you want marked, then question II.4 will not be marked.

Question II.1
(a) Define a function in Scheme to examine a list and count the number of elements that are
equal to 42.

(b) Write a Scheme function spread that takes a list of unary functions f1 , ..., fn , and a single
value v, and returns the list of results of calling fi on v:

(spread (list f1 f2 ... fn ) v) = (list (f1 v) (f2 v) ... (fn v))

For example

(spread (list + / even? pair?) 2) ; Yields (2 1/2 #t #f)

(c) Write a function curried-map that takes a unary function f as its only parameter and
returns a function that maps f on to any list. E.g.

(define negate-a-list-for-me-please (curried-map -))


(negate-a-list-for-me-please ’(1 2 3)) ; Yields (-1 -2 -3)

Question II.2
This question concerns the meaning of let* in Scheme.

(a) Rewrite the following expression

(let* ((a inita) (b initb) (c initc)) (f a b c) (g a b c) )

as an equivalent expression using one or more lets.

(b) Rewrite the same expression as an equivalent expression using one or more lambdas.

(c) Write a Scheme macro to implement let* in terms of lambda. Your solution must work
for a let* with any number of bound variables.

3
Name:
Student No:

Question II.3
This question concerns scoping mechanims. Consider the following program in pseudocode for
a block-structured language:
procedure outer(i, j) {
procedure inner(i) {
printf("Inner %d %d\n", i, j);
}
procedure middle(i, j) {
printf("Middle %d %d\n", i, j);
call inner(10);
}

printf("Outer %d %d\n", i, j);


call inner(20);
call middle(30, 40);
}

call outer(1, 2);

In this pseudo-code, procedure is used to define procedures, and call is used to invoke proce-
dures.

(a) Which of the variables i and j are bound and which are free in each of outer, middle and
inner? Explain briefly, with a couple of sentences.

(b) What does this program print, assuming the language uses static (lexical) scoping? Explain
briefly, with a couple of sentences.

(c) What does this program print, assuming the language uses dynamic scoping? Explain
briefly, with a couple of sentences.

Question II.4
Suppose you were stranded on a desert island with a bicycle-powered Centrino-based PC with a
text editor and a C compiler, including source code. The source code is well structured, without
a clear separation of machine-dependent and machine-independent parts.
One day a crate containing a solar-powered top-of-the line iMac washes up on shore. This
machine uses a PowerPC processor that is incompatible with the Centrino. The iMac has a text
editor and all the documentation you would ever want, but no compiler. You are able to move
files back and forth between the machines using a USB drive.
Describe how would you go about getting a C compiler running on the iMac. Outline your steps
clearly in point form. You have no off-shore network connection from which you can download
anything else – otherwise you would have E-mailed for help already.

You might also like