final-fa12-solutions

Rubric 4 points for constructors 1 points for both

Info iconThis preview shows pages 11–16. Sign up to view the full content.

View Full Document Right Arrow Icon
Rubric; 4 points for constructors: (1 points for both use of new DNode constructor, 1 point for correct arguments) 3 points for “wiring”: 1 point per pointer 3 points for stack shape: 1 point for type declaration, 2 points for only using variable x Two example solutions (there are many others): DNode x = new DNode("B", null , null ); x.next = x; x = new DNode("A", null , x); x.prev.prev = x; 11
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
or DNode x = new DNode("A", null , null ); x = new DNode("B", null , x); x.next = x; x.prev.prev = x; x = x.prev; 12
Background image of page 12
5. Java’s Type System (8 points) Consider the following class definitions: class S { } class T extends S { } class U extends S { } class V extends U { } For each of the questions below, circle all the correct answers—there may be zero, one, or more. a. What is the static type of x in the code below? S x = new V(); x = new U(); a. Object b. S c. T d. U e. V b. What is the dynamic type of the value stored in x after running the code below? S x = new V(); x = new U(); a. Object b. S c. T d. U e. V c. Which types can we place in the hole marked __?__ below so that no ClassCastException is thrown when this program is run? Object o = new U(); Object x = (__?__)o; a. Object b. S c. T d. U e. V d. Which types, when placed in the hole marked __?__ below, cause the compiler to generate an “incompatible types” error message? T t = new T(); boolean b = t instanceof __?__; a. Object b. S c. T d. U e. V Rubric: 1 pt. for each of the seven circles. no extra circles allowed in first two parts 8th point if no extra circles in last two parts -1 point for each additional circle after 2 13
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
6. First-class Functions and fold (12 points) Recall the OCaml definition of binary trees, and consider the fold function for such trees: type ’a tree = | Empty | Node of (’a tree) * ’a * (’a tree) let rec fold (combine : ’a -> ’b -> ’b -> ’b) (base : ’b) (t : ’a tree) : ’b = begin match t with | Empty -> base | Node(lt, x, rt) -> combine x (fold combine base lt) (fold combine base rt) end In this problem, you will explain how many functions can be written in terms of fold . Consider the following recursive tree functions: let rec sum (t : int tree) : int = begin match t with | Empty -> 0 | Node(lt, x, rt) -> (sum lt) + x + (sum rt) end let rec size (t : ’a tree) : int = begin match t with | Empty -> 0 | Node(lt, _, rt) -> (size lt) + 1 + (size rt) end let rec is_full (t : ’a tree) : bool = begin match t with | Empty -> true | Node(lt, _, rt) -> end let rec preorder (t : ’a tree) : ’a list = begin match t with | Empty -> [] | Node(lt, x, rt) -> x::(preorder lt)@(preorder rt) end let rec postorder (t : ’a tree) : ’a list = begin match t with | Empty -> [] | Node(lt, x, rt) -> (postorder lt)@(postorder rt)@[x] end let rec inorder (t : ’a tree) : ’a list = begin match t with | Empty -> [] | Node(lt, x, rt) -> (inorder lt)@[x]@(inorder rt) end 14
Background image of page 14
For each function above, select the combination of combine and base arguments to fold such that you get an equivalent implementation by writing: fun t -> fold combine base t Rubric: 1 pt. per blank combine
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 16
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page11 / 17

Rubric 4 points for constructors 1 points for both use of...

This preview shows document pages 11 - 16. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online