A what is the static type of x in the code below s x

Info icon This preview shows pages 13–17. Sign up to view the full content.

View Full Document Right Arrow Icon
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
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) -> ((size lt) = (size rt)) && is_full lt && is_full 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
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 base sum d 0 size c 0 is_full none none preorder h or i [] postorder f [] inorder g [] Your choices for combine and base are enumerated below. Write the choice a–j for the combine function and the choice a–j for base . If no choice works ( i.e. the function cannot be expressed as a fold) write “none” in both slots. You may use the same choice more than once. Combine choices: Base choices: (a) ( fun x lv rv -> Node(rv, x, lv)) (b) ( fun x lv rv -> lv + rv) (c) ( fun x lv rv -> lv + 1 + rv) (d) ( fun x lv rv -> lv + x + rv) (e) ( fun x lv rv -> (size lv) = (size rv)) (f) ( fun x lv rv -> [email protected]@[x]) (g) ( fun x lv rv -> [email protected][x]@rv) (h) ( fun x lv rv -> [x]@[email protected]) (i) ( fun x lv rv -> x::[email protected]) (j) ( fun x lv rv -> [email protected]@x) (a) true (b) false (c) 0 (d) 1 (e) Empty (f) [] (g) [x] (h) lv (i) rv (j) [email protected] 15
Image of page 15

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

View Full Document Right Arrow Icon
7. Binary Search Trees & OCaml programming (20 points) This problem uses the same OCaml type of trees as in Problem 6, repeated here for your reference: type ’a tree = | Empty | Node of (’a tree) * ’a * (’a tree) a. State the binary search tree invariant in words. We have done the case for Empty trees: The Empty tree is a binary search tree.
Image of page 16
Image of page 17
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern