This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: CMSC330 Fall 2009 Midterm #2 Solutions 1. (6 pts) Programming Languages a. (3 pts) Describe one design choice for type declarations for static types in a programming language, and list a programming language using this approach. Manifest (explicit) – type specified in variable declaration (Java, C, Java) Inferred (implicit) – not specified, determined by compiler from usage (OCaml) b. (3 pts) How can programmers write Java programs which (effectively) pass functions as arguments to other functions? Give a brief answer. Pass an object implementing an interface with a known method. 2. (8 pts) Scoping Consider the following OCaml code. let app f y = let y = 5 in let x = 7 in let a = 9 in f y ;; let add x y = let incr a = a+y in app incr x ;; (add 2 4) ;; a. (2 pts) List the order the functions add , incr , and app are invoked in (add 2 4) add, app, incr b. (3 pts) What value is returned by (add 2 4) with static scoping? Explain. 9, since for incr a = a+y: a=5 (let y = 5…in f y) and y=4 (add x y = …) (add 2 4). c. (3 pts) What value is returned by (add 2 4) with dynamic scoping? Explain. 10, since for incr a = a+y: a=5 (let y = 5…in f y) and y=5 (let y = 5…in f y). Explanation: The return value is determined by incr, when it is invoked as (f y) by app. The value of incr a is a+y. The formal parameter a is bound to the value of the argument passed to incr. In app when (f y) is invoked, the argument y always has the value of 5, since the closest definition of y (both static & dynamic) is the let y = 5 in app. value of 5, since the closest definition of y (both static & dynamic) is the let y = 5 in app....
View Full Document
This note was uploaded on 01/13/2012 for the course CMSC 330 taught by Professor Staff during the Fall '08 term at Maryland.
- Fall '08