CIS 120 Midterm II November 8, Name (printed): Pennkey (login id):

Similar documents
CIS 120 Midterm II November 8, 2013 SOLUTIONS

CIS 120 Midterm II November 7, Name (printed): Pennkey (login id):

CIS 120 Midterm II March 29, 2013 SOLUTIONS

Name: Pennkey: CIS 120 Midterm II November 18, 2011

CIS 120 Midterm II November 16, Name (printed): Pennkey (login id):

CIS 120 Midterm II March 31, 2017 SOLUTIONS

CIS 120 Midterm II November 16, 2012 SOLUTIONS

Name: Pennkey (eg, sweirich): CIS 120 Final Exam May 8, 2012

CIS 120 Final Exam May 7, Name (printed): Pennkey (login id):

CIS 120 Midterm I October 2, Name (printed): Username (login id):

CIS 120 Programming Languages and Techniques. Final Exam, May 3, 2011

Programming Languages and Techniques (CIS120)

CIS 120 Final Exam 15 December 2017

CIS 120 Final Exam May 3, Name (printed): Pennkey (login id):

Programming Languages and Techniques (CIS120)

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

CIS 120 Final Exam December 17, Name (printed): Pennkey (login id):

Name: Pennkey: CIS 120 Final Exam December 21, Do not begin the exam until you are told to do so. You have 120 minutes to complete the exam.

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

CIS 110 Introduction to Computer Programming. February 29, 2012 Midterm

Programming Languages and Techniques (CIS120)

CIS 341 Final Examination 4 May 2017

CIS 341 Final Examination 3 May 2011

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

CIS 341 Midterm February 28, Name (printed): Pennkey (login id): SOLUTIONS

CIS 120 Midterm I February 16, 2015 SOLUTIONS

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

CIS Introduction to Computer Programming. 5 October 2012 Midterm

CIS 341 Final Examination 30 April 2013

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

CMSC 330: Organization of Programming Languages

CIS 110 Introduction to Computer Programming. 7 May 2012 Final Exam

CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

CSE331 Winter 2014, Midterm Examination February 12, 2014

CIS 120 Final Exam 16 December Name (printed): Pennkey (login id):

CIS 500: Software Foundations

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

CIS 500: Software Foundations

CIS 500: Software Foundations

Programming Languages and Techniques (CIS120)

CIS 110 Introduction to Computer Programming. 17 December 2012 Final Exam

CIS 110 Introduction to Computer Programming. 13 February 2013 Make-Up Midterm Midterm

Exam 1 - (20 points)

CS18000: Programming I

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

CIS Fall 2012 Midterm, 7 June 2012, Answer Key. Miscellaneous

CIS 120 Programming Languages and Techniques. Final. December 16, 2010

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

CIS 120 Programming Languages and Techniques. Midterm II. November 12, Answer key

CIS 110 Fall 2014 Introduction to Computer Programming 8 Oct 2014 Midterm Exam Name:

CS 101 Spring 2006 Final Exam Name: ID:

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

CS 10, Fall 2015, Professor Prasad Jayanti

Arrays. COMS W1007 Introduction to Computer Science. Christopher Conway 10 June 2003

CS 61B, Spring 1999 MT3 Professor M. Clancy

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

CIS 110 Introduction to Computer Programming Summer 2016 Final. Recitation # (e.g., 201):

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

CIS 110 Introduction to Computer Programming Summer 2014 Final. Name:

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

CIS 120 Programming Languages and Techniques. Midterm II. November 12, 2010

CMSC 330: Organization of Programming Languages. OCaml Data Types

CIS 341 Midterm March 2, Name (printed): Pennkey (login id): Do not begin the exam until you are told to do so.

CSE wi Midterm Exam 2/8/18. Name UW ID #

CIS 110 Introduction to Computer Programming. 12 February 2013 Midterm

CIS 110 Fall 2016 Introduction to Computer Programming 13 Oct 2016 Midterm Exam

Name: CIS 120e Midterm I Review

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

CSE 143 SAMPLE MIDTERM

Announcements. CS18000: Problem Solving And Object-Oriented Programming

CIS 110 Introduction to Computer Programming Summer 2018 Final. Recitation # (e.g., 201):

Programming Languages and Techniques (CIS120)

CIS 341 Final Examination 4 May 2018

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Pace University. Fundamental Concepts of CS121 1

Programming Languages and Techniques (CIS120)

Midterm 1. CMSC 430 Introduction to Compilers Spring Instructions Total 100. Name: March 14, 2012

CSE 332 Spring 2014: Midterm Exam (closed book, closed notes, no calculators)

CMSC330 Fall 2016 Midterm #1 2:00pm/3:30pm

Programming Languages and Techniques (CIS120)

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

CSE331 Spring 2015, Final Examination June 8, 2015

CS171 Midterm Exam. October 29, Name:

UNIVERSITY REGULATIONS

CMSC 330: Organization of Programming Languages

CIS 110 Introduction to Computer Programming Summer 2016 Midterm. Recitation # (e.g., 201):

Computer Science II Data Structures

MIDTERM EXAMINATION - CS130 - Spring 2005

I have neither given nor received any assistance in the taking of this exam.

Practice Midterm 1. Problem Points Score TOTAL 50

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

CIS 110 Introduction to Computer Programming Spring 2016 Final Exam

Prelim 2. CS 2110, 16 November 2017, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

Transcription:

CIS 120 Midterm II November 8, 2013 Name (printed): Pennkey (login id): My signature below certifies that I have complied with the University of Pennsylvania s Code of Academic Integrity in completing this examination. Signature: Date: 1 /15 2 /8 3 /20 4 /12 5 /20 6 /25 Total /100 Do not begin the exam until you are asked to do so. You have 50 minutes to complete the exam. There are 100 total points. There are 11 pages in this exam, plus an Appendix. Make sure your name and Pennkey (a.k.a. username) is on the top of this page. Be sure to allow enough time for all the problems skim the entire exam first to get a sense of what there is to do. 1

1. Facts about OCaml and Java (15 points) For each part, circle true or false. a. T F The.equals method in Java is roughly similar to OCaml s = operator. b. T F The == operator in both OCaml and Java tests whether two compound values have identical structure. c. T F Both Java and OCaml provide generic types. OCaml also supports subtyping, while Java does not. d. T F In Java, there is a class that is a subtype of any other class. e. T F In Java, an interface can extend zero, one, or several other interfaces. f. T F In Java, a class can extend zero, one, or several other classes. g. T F In the Java ASM, large data structures such as object values are stored in the stack, not the heap. h. T F In the OCaml ASM, bindings of variables to values in the stack are immutable, while in Java they are mutable. i. T F A Java variable of type String behaves like an OCaml variable of type string option ref. j. T F A static method in Java may refer to an automatically bound variable this. k. T F In Java, the static type of a variable is always a subtype of the dynamic class of the object value that it refers to. l. T F In OCaml, mutable record fields may contain either a proper value or null. m. T F A Java array can be resized by assigning a new value to its length field. n. T F OCaml s anonymous functions can be approximated in Java by using a class with one method. o. T F Recursive functions cannot be defined in Java. 2

2. Java Jargon (8 points) Briefly (two sentences max) define the phrase dynamic dispatch as it applies to Java. 3

3. Subtyping, Interfaces and Static Types (20 points) On page 12 in the Appendix, you will find the code for Java interfaces Widget, LabelController, and Gctx and classes Label and Empty, loosely inspired by Homework 8, plus the class Foo defining three static methods. a. For each variable, fill in its static type and the dynamic class of the object that it refers to in the main method. Note that the type declarations for a4, a5 and a6 have been intentionally omitted. There are several static types that could be used to make these declarations work - use the most specific one. variable static type dynamic class a1 a2 a3 a4 a5 a6 4

b. Which of these statements/declarations would type check if added to the end of the main method? Circle GOOD if the line would not cause a compile-time error, and TYPE ERROR otherwise. a3 = l; GOOD TYPE ERROR a2 = a4; GOOD TYPE ERROR Widget x = Foo.asWidget(a1); GOOD TYPE ERROR Object y = Foo.asWidget(a2); GOOD TYPE ERROR Object z = Foo.asLabelController(e); GOOD TYPE ERROR a2.repaint(new Gctx()) GOOD TYPE ERROR a2.setlabel( CIS 120 rocks! ); GOOD TYPE ERROR a4.setlabel( CIS 120 rocks! ); GOOD TYPE ERROR 5

4. Objects in OCaml (12 points) Consider the following Java class: class Bar { private int a; private int b; public Bar(int x) { a = 120; b = x public int get() { return a * b; public void set(int x) {a = x; We can encode Bar objects as values belonging to an OCaml record type with two fields, get and set, corresponding to the methods of the same name above. type m = { get : unit -> int; set : int -> unit Your task in this problem is to implement a constructor for such objects a function create that builds values belonging to this record type. For example, the result of the following program should be 42. let obj = create 6 in obj.set(7); obj.get() Complete the definition of create below to achieve this behavior. let create (x : int) : m = 6

5. OCaml Queue + ASM (20 points) Recall the following definitions of queues in OCaml: ( Data structure for mutable queues, as defined in class. ) type a qnode = { v: a; mutable next: a qnode option type a queue = { mutable head : a qnode option; mutable tail : a qnode option a. Suppose we have the OCaml ASM shown below. Write a short piece of code that can get the ASM to this state. (For reference, we provide you with the code for the queue from homework 5 in the appendix, page 13. You are free to call these functions as part of your solution to this part, or not, as you prefer; correct solutions can be written both ways. However, note that the heap structure we ve given you does not satisfy the queue invariant, so your solution clearly cannot consist only of calls to the queue functions, which always maintain the invariant.) q head! tail! qn1 v! 1! next! qn2 v! 2! next! Answer: 7

b. Suppose we start executing from the ASM configuration shown on the previous page. Fill in the template stack and heap diagram below to show what it will look like at the point in the computation marked ( HERE ). (Notice that this code calls a queue function even though our initial configuration does not satisfy the queue invariant!) Note: You should show only the final state! You might need to allocate new heap objects. You may need to add Some bubbles in the appropriate places, The Appendix of the exam contains the complete implementation of queues and an example of the stack and heap diagram for an OCaml program. Make sure your work is clear! If your work is not clear, it will not get credit. Code qn2.next <- Some qn2; enq q 3; ( HERE ) Answer (extend or modify the diagram below, as appropriate): q head! tail! qn1 v! 1! next! qn2 v! 2! next! c. Is the queue invariant satisfied for queue q at the point in the computation marked ( HERE ) of part 5b? (Just write yes or no no justification is needed.) 8

6. Program Design - Array Programming (25 points) Use the four step design methodology to implement a static method called isgoodsquare that takes as input a two-dimensional array of ints and returns true if and only if the array is a square matrix where the sum of the numbers in every horizontal row and every vertical column is the same. The method should return false for ill-formed inputs (null, non-square array). On an empty (0-length) input array, it should return true. a. Step 1 is understanding the problem. You don t have to write anything for this part your answers below will demonstrate whether or not you succeeded with Step 1. b. Step 2 is formalizing the interface. We have done this for you: public static boolean isgoodsquare(int[][] sq) {... c. The next step is writing test cases. For example, one possible testcase is a valid good square: @Test public void testvalidgoodsquare() { int sq[][] = {{8,1,6,{3,5,7,{4,9,2; assertequals(true, isgoodsquare(sq)); The interesting parts of this test are the name, which should communicate the reason for the test ( valid good square ), plus the expected result value and the array to be tested. To avoid writing too much boilerplate, we might abbreviate this test case as follows: Test name Input array Expected output Valid good square {{8,1,6,{3,5,7,{4,9,2 true On the next page, write three more test cases for this method in the same style. 9

Test name Input array Expected output Valid good square {{8,1,6,{3,5,7,{4,9,2 true 10

d. The final step is to implement the method. Please do so below. Do not use any external libraries. public static boolean isgoodsquare(int[][] sq) { 11

Appendix: Widget Code public interface Widget { public void repaint (Gctx gc); public interface LabelController { public void setlabel(string s); public class Gctx { public Gctx() {... public class Label implements Widget, LabelController { private String s; public Label(String s) { this.s = s; public void setlabel(string s) { this.s = s; public void repaint(gctx gc) {... public class Empty implements Widget { public void repaint(gctx gc) { public Empty() { public class Foo { public static Widget aswidget(widget w) { return w; public static LabelController aslabelcontroller(labelcontroller l){ return l; public static void main(string[] args) { Label l = new Label("CIS 120"); Empty e = new Empty (); Widget a1 = l; LabelController a2 = l; Object a3 = e; a4 = Foo.asWidget(l); a5 = Foo.asLabelController(l); a6 = Foo.asWidget(e); 12

Appendix: OCaml Linked Queue implementation type a qnode = { v : a; mutable next : a qnode option type a queue = { mutable head : a qnode option; mutable tail : a qnode option let create () : a queue = { head = None; tail = None let is_empty (q: a queue) : bool = q.head = None let enq (q: a queue) (x: a) : unit = let newnode_opt = Some { v = x; next = None in begin match q.tail with None -> q.head <- newnode_opt; q.tail <- newnode_opt Some qn2 -> qn2.next <- newnode_opt; q.tail <- newnode_opt end let deq (q: a queue) : a = begin match q.head with None -> failwith "error: empty queue" Some qn -> begin match q.tail with Some qn2 -> if qn == qn2 then ( deq from 1 element queue ) (q.head <- None; q.tail <- None; qn2.v) else (q.head <- qn.next; qn.v) ( Make sure to use parens around ; expressions. ) None -> failwith "invariant violation" end end let to_list (q : a queue) : a list = let rec loop (qn : a qnode option) (acc : a list) : a list = begin match qn with None -> List.rev acc Some qn1 -> loop qn1.next (qn1.v :: acc) end in loop q.head [] 13

Appendix: OCaml ASM Example This is an example of the Stack and Heap components of the OCaml Abstract Stack Machine. Your diagram should use similar graphical notation for Some v and None values. ( The types of mutable queues. ) type a qnode = { v : a; mutable next : a qnode option type a queue = { mutable head : a qnode option; mutable tail : a qnode option let qn1 : int qnode = {v = 1; next = None let qn2 : int qnode = {v = 2; next = Some qn1 let q : int queue = {head = Some qn2; tail = Some qn1 ( HERE ) %)'*%'"'%*%+,-./0')1,1,' The OCaml program above yields the ASM Stack and Heap depicted below when the program execution reaches the point marked ( HERE ). 7/"64' 8,".' )%:' )%;' )' v! 1! next! v! 2! next! head! tail! 14