1. Programming Concepts: Closures and Encapsulation (12 points)
Multiple choice. For each question, mark all correct answers. There may be zero or more than
one.
a. What value is computed for ans by the following OCaml program?
type ’a ref = { mutable contents : ’a }
let x = {contents = 4}
let f : int -> int =
let x = {contents = 2} in
fun (y:int) -> y + x.contents
;; x.contents <- 1
let ans = f x.contents
Q 2 Q 3 Q 4 Q 5
b. After running the following OCaml program, the closure named f will contain value bindings
for which identifiers?
let a : int = 50
let f : int -> int =
let b = 60 in
fun (c:int) -> a + b + c
Q a Q b Q c Q f
c. Consider the (simplified) Pixel class given by the Java code below:
class Pixel {
public int[] rgb = {0, 0, 0};
public Pixel(int r, int g, int b) {
rgb[0] = r; rgb[1] = g; rgb[2] = b;
}
public int[] getComponents() {
return rgb;
}
}
To properly encapsulate the state of Pixel objects, the programmer should:
Category | exam bundles |
Comments | 0 |
Rating | |
Sales | 0 |