CMSC 330, Spring 2009 Midterm 2 Practice: OCaml & Functional Programming - Prof. Chao Wen , Exams of Programming Languages

Practice problems for midterm 2 of the cmsc 330, spring 2009 course, focusing on ocaml programming language and functional programming concepts. Topics include differences between ocaml modules and java classes, strong and weak typing, call-by-name and lazy evaluation, l-values and r-values, activation records, short circuiting, funargs, static vs. Dynamic scoping, and parameter passing in c. Students are expected to understand and answer questions related to these topics.

Typology: Exams

Pre 2010

Uploaded on 07/30/2009

koofers-user-js7
koofers-user-js7 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 330, Spring 2009, Midterm 2 Practice Problems
1. Programming languages
a. Describe the difference between OCaml modules and Java classes.
Both provide a public definition for a group of functions whose internal
details are hidden, but Java classes can also instantiate objects and
inherit attributes from other classes (not possible with OCaml modules).
b. Describe the difference between strong and weak typing.
Strong typing prevents types from being used interchangeably, weak
typing allows types to be treated as other types through many implicit
type conversions.
c. Explain how call-by-name simplifies implementing lazy evaluation.
Expressions to be evaluated lazily may be passed as arguments to
functions, since function arguments are not evaluated until used.
d. Describe the difference between an L-value and an R-value.
L-values refer to the address of a symbol, R-values refer to the value for a
symbol.
e. What is an activation record, and why is it usually allocated on a stack?
An activation record contains state information for a function invocation.
It is usually allocated on a stack so it can be easily freed upon function
return by popping the stack.
f. Describe short circuiting.
Short circuiting refers to programming language semantics that halts
evaluation of the 2
nd
operand of a logical operator if the overall boolean
result is already determined.
2. Function arguments
For each code, explain whether g is an upward or downward funarg.
a. let f x = let g y = x + y in let app a b = a b in app g 1 ;;
g is a downards funarg since it is a function parameter passed to app
b. let f x = let g y = x + y in g ;;
g is an upwards funarg since it is a function return value for the 2
nd
let
A funarg is simply a function argument where the function is either
1. Passed as a parameter to a function call
2. Returned as the return value of a function call
pf3
pf4

Partial preview of the text

Download CMSC 330, Spring 2009 Midterm 2 Practice: OCaml & Functional Programming - Prof. Chao Wen and more Exams Programming Languages in PDF only on Docsity!

CMSC 330, Spring 2009, Midterm 2 Practice Problems

  1. Programming languages a. Describe the difference between OCaml modules and Java classes. Both provide a public definition for a group of functions whose internal details are hidden, but Java classes can also instantiate objects and inherit attributes from other classes (not possible with OCaml modules). b. Describe the difference between strong and weak typing. Strong typing prevents types from being used interchangeably, weak typing allows types to be treated as other types through many implicit type conversions. c. Explain how call-by-name simplifies implementing lazy evaluation. Expressions to be evaluated lazily may be passed as arguments to functions, since function arguments are not evaluated until used. d. Describe the difference between an L-value and an R-value. L-values refer to the address of a symbol, R-values refer to the value for a symbol. e. What is an activation record, and why is it usually allocated on a stack? An activation record contains state information for a function invocation. It is usually allocated on a stack so it can be easily freed upon function return by popping the stack. f. Describe short circuiting. Short circuiting refers to programming language semantics that halts evaluation of the 2nd^ operand of a logical operator if the overall boolean result is already determined.
  2. Function arguments For each code, explain whether g is an upward or downward funarg. a. let f x = let g y = x + y in let app a b = a b in app g 1 ;; g is a downards funarg since it is a function parameter passed to app b. let f x = let g y = x + y in g ;; g is an upwards funarg since it is a function return value for the 2nd^ let

A funarg is simply a function argument where the function is either

**1. Passed as a parameter to a function call

  1. Returned as the return value of a function call**
  1. Static vs. Dynamic Scoping Consider the following OCaml code. let a = 1 ;; let f = fun ( ) -> a ;; // value of a determined here for static scoping let a = 2 ;; f ( );; // value of a determined here for dynamic scoping

a. What value is returned by the invocation of f( ) with static scoping? Explain. 1, since the binding for “a” in the function “f = fun ( ) -> a” refers to the closest lexical value of “a” at the point where the function is declared in the code (1st^ let a). b. What value is returned by the invocation of f( ) with dynamic scoping? Explain. 2, since the binding for “a” in the function “f = fun ( ) -> a” refers to the closest value of “a” in the call stack at the point where the function is actually invoked (2nd^ let a).

Consider the following OCaml code. let app f w = let x = 1 in f w ;; // value of x determined here // for dynamic scoping let add x y = let incr z = z+x in app incr y;; // value of x determined here // for static scoping (add 2 3) ;;

c. What is the order of invocation for the functions app, add, and incr when evaluating the expression (add 2 3)? 1) add, 2) app, 3) incr incr is defined in add but not invoked until reaching the body of app (as f). d. What value is returned by (add 2 3) with static scoping? Explain. 5, since the binding for x in the function incr refers to the closest lexical value of x (add x ) at the point where the function is declared in the code. e. What value is returned by by (add 2 3) with dynamic scoping? Explain. 4, since the binding for x in the function incr refers to the closest value of x in the call stack ( let x = 1 ) at the point where the function is actually invoked (by app f w … in f w).

  1. Tail recursion For each OCaml function, explain why it is or is not tail recursive a. let rec foo x = 1 + (foo x) It is not tail recursive since 1 must be added to the value of foo x before it can be used as the return value of foo. b. let rec sum l = match l with [] -> 0 | (x::xs) -> x + (sum xs) It is not tail recursive since x must be added to the return value of sum xs before it can be used as the return value of sum. c. let rec last = function [x] -> x | (_::xs) -> last xs It is tail recursive since the value of the recursive call to “last xs” is also the return value. d. let rec fib x = if (x = 0) then 0 else if (x = 1) then 1 else (fib (x-1) + fib (x-2)) It is not tail recursive since there are multiple calls to fib and none are the return value.