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.

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

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

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

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

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

CIS 120 Midterm II November 16, 2012 SOLUTIONS

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

CIS 120 Final Exam 15 December 2017

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

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

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

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

public static boolean isoutside(int min, int max, int value)

CIS 120 Midterm II November 8, 2013 SOLUTIONS

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

public static void negate2(list<integer> t)

Programming Languages and Techniques (CIS120)

CIS 120 Final Exam 19 December 2014 SOLUTIONS

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

CIS 120 Midterm II March 29, 2013 SOLUTIONS

Programming Languages and Techniques (CIS120)

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

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

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

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

List ADT. B/W Confirming Pages

Chapter 11 Paper Practice

Do not turn to the next page until the start of the exam.

CIS 341 Final Examination 4 May 2017

Programming Languages and Techniques (CIS120)

Introduction to Computer Science II (ITI 1121) Final Examination

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

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

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

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

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

Introduction to Programming Using Java (98-388)

Java Review: Objects

1 Shyam sir JAVA Notes

CIS 120 Final Exam 15 December 2017 SOLUTIONS

Programming Languages and Techniques (CIS120)

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

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

An Introduction to Data Structures

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

Introduction to Computing II (ITI 1121) FINAL EXAMINATION

Special error return Constructors do not have a return value What if method uses the full range of the return type?

CIS 120 Midterm II March 31, 2017 SOLUTIONS

Final Exam May 21, 2003

Introduction to Computing II (ITI 1121) Final Examination

File IO. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 20

CS 180 Final Exam Review 12/(11, 12)/08

Data Structures G5029

IBS Software Services Technical Interview Questions. Q1. What is the difference between declaration and definition?

CIS 341 Final Examination 30 April 2013

INTRODUCTION TO SOFTWARE SYSTEMS (COMP1110/COMP1140/COMP1510/COMP6710)

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

Queens College, CUNY Department of Computer Science. CS 212 Object-Oriented Programming in Java Practice Exam 2. CS 212 Exam 2 Study Guide

CS159. Nathan Sprague

CS11 Java. Fall Lecture 4

Programming Languages and Techniques (CIS120)

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

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

Exceptions, try - catch - finally, throws keyword. JAVA Standard Edition

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

CSE331 Winter 2014, Midterm Examination February 12, 2014

TeenCoder : Java Programming (ISBN )

Principles of Functional Programming

Page 1

INSTRUCTIONS TO CANDIDATES

Defensive Programming

CS 251, Intermediate Programming Midterm Exam October 9, 2013

CIS 341 Final Examination 3 May 2011

CS201 ArrayLists, Generics, and Dynamic Data Structures (Chapters 14, 15)

University of Maryland College Park Dept of Computer Science

1.00 Introduction to Computers and Engineering Problem Solving. Final Examination - May 19, 2004

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

Programming Languages and Techniques (CIS120)

CIS 110 Spring 2014 Introduction to Computer Programming 12 May 2014 Final Exam Answer Key

Programming Languages and Techniques (CIS120)

Homework 2: Imperative Due: 5:00 PM, Feb 15, 2019

Recursive Objects. Singly Linked List (Part 2)

CSE 143 Sp03 Midterm 2 Sample Solution Page 1 of 7. Question 1. (2 points) What is the difference between a stream and a file?

Linked List. ape hen dog cat fox. tail. head. count 5

Training topic: OCPJP (Oracle certified professional Java programmer) or SCJP (Sun certified Java programmer) Content and Objectives

Programming II (CS300)

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

CS171 Midterm Exam. October 29, Name:

CS 455 Midterm Exam 2 Fall 2016 [Bono] November 8, 2016

1 Constructors and Inheritance (4 minutes, 2 points)

We would like guidance on how to write our programs beyond simply knowing correlations between inputs and outputs.

CS 216 Exam 1 Fall SOLUTION

I/O in Java I/O streams vs. Reader/Writer. HW#3 due today Reading Assignment: Java tutorial on Basic I/O

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

Question 1. (2 points) What is the difference between a stream and a file?

Subclassing for ADTs Implementation

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

Transcription:

Name: Pennkey: CIS 120 Final Exam December 21, 2011 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 /20 2 /14 3 /18 4 /38 5 /30 Total /120 Do not begin the exam until you are told to do so. You have 120 minutes to complete the exam. There are 120 total points. There are 17 pages in this exam. Make sure your name and Pennkey (a.k.a. username) are on the top of this page. 1

1. True or False (20 points) Indicate whether each of these statements is true or false. Some of the questions refer to the Java IO library s FileReader class some of its documentation is reproduced in the appendix. a. T F In Java, a static method gets passed an implicit this parameter. b. T F According to the FileReader documentation, the following variable declaration is well typed: Readable r = new FileReader("file.txt"); c. T F According to the following signature, this FileReader constructor may never raise a NullPointerException. public FileReader(String filename) throws FileNotFoundException d. T F In Java Swing programming, when the program s visual state changes it must call the repaint() method to update the display. (This question was dropped from the exam for being ambiguous, all students received credit for any answer.) e. T F In both the Java Swing libraries and our OCaml GUI library, the job of a listener is help route an event to the appropriate component (or widget) for subsequent handling. f. T F In Java Swing programming, using an anonymous inner class is a good way to implement an ActionListener because the actionperformed method often needs access to the outer class s local state. g. T F It is not possible to write your own Iterator class in Java. h. T F The Java collection type Map<Section,List<Student>> provides a simple means of determining which lab Section a given CIS 120 Student is in. i. T F In Java, if B is a subtype of A, then List<B> is a subtype of List<A>. j. T F For a type like Set<E> (in Java) or a set (in OCaml) to be truly abstract, any client code that uses this type must not be able to determine its concrete representation (via lists, trees, arrays, etc.). 2

2. Inheritance and Overriding (14 points) Consider the following Java class declarations: class A { public void print1() { System.out.println("A s print1"); public void callprint() { print1(); class B extends A { public void print2() { System.out.println("B s print2"); @Override public void callprint() { print2(); class C extends A { @Override public void print1() { System.out.println("C s print1"); Answer the multiple choice questions on the following page. 3

For each code snippet below, indicate what string will get printed to the console, or mark Ill typed if the snippet has a type error. a. B x = new B(); x.print1(); A s print1 B s print2 C s print1 Ill typed b. A y = new B(); y.print1(); A s print1 B s print2 C s print1 Ill typed c. A q = new B(); q.print2(); A s print1 B s print2 C s print1 Ill typed d. C z = new C(); z.print1(); A s print1 B s print2 C s print1 Ill typed e. A v = new B(); v.callprint(); A s print1 B s print2 C s print1 Ill typed f. C w = new C(); w.callprint(); A s print1 B s print2 C s print1 Ill typed g. A u = new C(); u.callprint(); A s print1 B s print2 C s print1 Ill typed 4

3. OCaml: Trees and First-class Functions (18 points) Recall our OCaml definition of binary trees that store data in their internal nodes. type a tree = Empty Node of a tree * a * a tree An example of such a tree is given by: let t : int tree = Node(Node(Node(Empty, 1, Empty), 2, Node(Empty, 0, Empty)), 3, Node(Empty, 4, Empty)) We draw it like this (eliding the Empty values): 3 / \ 2 4 / \ 1 0 Consider the following OCaml function: let rec tree_fold (combine : b -> a -> b -> b) (base : b) (t : a tree) : b = begin match t with Empty -> base Node(lt, x, rt) -> combine (tree_fold combine base lt) x (tree_fold combine base rt) end For each OCaml program below, circle the value computed for ans. a. let c = fun (ls:int) (x:int) (rs:int) -> ls + x + rs let b = 0 let ans = tree_fold c b t 5 10 11 16 b. let c = fun (ls:int) (x:int) (rs:int) -> ls + x + rs let b = 1 let ans = tree_fold c b t 5 10 11 16 5

The tree t is reproduced here for your reference: t = 3 / \ 2 4 / \ 1 0 c. let c = fun (ls:int list) (x:int) (rs:int list) -> [x] @ ls @ rs let b = [] let ans = tree_fold c b t [3; 1; 0; 2; 4] [3; 2; 4; 1; 0] [3; 2; 1; 0; 4] [3; 4; 2; 0; 1] d. let c = fun (ls:int tree) (x:int) (rs:int tree) -> Node(ls, x + x, rs) let b = Empty let ans = tree_fold c b t 6 6 6 6 / \ / \ / \ / \ 4 6 4 6 4 8 8 4 / \ / \ / \ / \ 3 2 3 2 2 0 0 2 ˆˆˆˆˆˆˆˆ e. let rec l (t:int tree) : int = begin match t with Empty -> 0 Node(Empty, x, Empty) -> x Node(lt, _, rt) -> (l lt) + (l rt) end let ans = l t 5 10 11 16 f. let rec r (t: a tree) : a tree = begin match t with Empty -> Empty Node(lt, x, rt) -> Node(r rt, x, r lt) end let ans = r t 6

3 3 3 3 / \ / \ / \ / \ 2 4 4 2 4 2 4 2 / \ / \ / \ / \ 1 0 0 1 1 0 0 1 ˆˆˆˆˆˆˆˆˆ 7

4. Program Design and Encapsulation (38 points) In this problem you will use the test-driven design methodology to implement a Java class that represents a (simplified version of) finite multisets. For the purposes of this problem, a finite multiset is a collection of natural numbers (i.e. non-negative integer values). Order does not matter, but there may be more than one occurrence of each element. Each multiset has a size, which is the total number of occurrences of all elements. Here are some examples, written using a mathematical notation: { the empty multiset, its size is 0 {0 the multiset containing just one occurrence of 0, its size is 1 {1, 2, 1 a multiset with two occurrences of 1 and one occurrence of 2, its size is 3 Such a structure can be defined in Java using an interface similar to Java s Set interface: interface FiniteMultiSet { void add(int x); // add one occurrence of x to the multiset void remove(int x); // remove one occurrence of x, if present, from the multiset int size(); // return the number of elements in the multiset int numberof(int x); // return the number of occurrences of x The following class implements the FiniteMultiSet interface for bounded sets, whose elements are in the range 0 to max-1. class FMSet implements FiniteMultiSet { private int[] elts; private int size; public FMSet(int max) { this.elts = new int[max]; this.size = 0; public int size() { return size; public void add(int x) { if (x < 0 x >= elts.length) { throw new IllegalArgumentException("exceeded capacity"); elts[x] = elts[x] + 1; size = size + 1; public int numberof(int x) { if (x < 0 x >= elts.length) { throw new IllegalArgumentException("exceeded capacity"); return elts[x]; public void remove(int x) {... 8

a. (5 points) Fill in the blanks of the following unit tests so that, according to the implementation of FMSet above, all of the tests succeed. public class FMSetTest { public void testempty() { FMSet s = new FMSet(0); assertequals(0, s.size()); public void testempty2() { FMSet s = new FMSet(3); assertequals(0, s.size()); public void testadd0() { FMSet s = new FMSet(3); s.add(0); assertequals(1, s.numberof(0)); assertequals(1, s.size()); public void testadd00() { FMSet s = new FMSet(3); s.add(0); s.add(0); assertequals(2, s.numberof(0)); assertequals(2, s.size()); public void testadd01() { FMSet s = new FMSet(3); s.add(0); s.add(1); assertequals(1, s.numberof(0)); assertequals(1, s.numberof(1)); assertequals(2, s.size()); 9

b. (9 points) Complete three distinct test cases for the remove method (whose implementation was omitted). Each test case should test a different aspect of remove s functionality. You may insert additional uses of assertequals. public void testremove0() { FMSet s = new FMSet(3); s.remove(0); assertequals(0, s.numberof(0)); assertequals(0, s.size()); public void testremove1() { FMSet s = new FMSet(3); s.add(0); s.remove(0); assertequals(0, s.numberof(0)); assertequals(0, s.size()); public void testremove2() { FMSet s = new FMSet(3); s.add(0); s.add(0); s.remove(0); assertequals(1, s.numberof(0)); assertequals(1, s.size()); public void testremove3() { FMSet s = new FMSet(3); s.add(0); s.add(1); s.remove(0); assertequals(0, s.numberof(0)); assertequals(1, s.numberof(1)); assertequals(1, s.size()); 10

c. (6 points) The implementor of the FMSet class intends the code to enforce the following invariants: (1) elts!= null (2) elts[i] >= 0 for all i in 0...(elts.length-1) (3) size = elts[0] + elts[1] +... + elts[elts.length-1] Complete the implementation of the remove method. It should never throw an exception, and it should preserve the FMSet invariants. NOTE: you do not need to use try...catch. public void remove(int x) { if (x < 0 x >= elts.length) { return; if (elts[x] > 0) { elts[x]--; size--; d. (10 points) The FMSet implementor wanted to make a more convenient way of constructing a multiset out of an integer array, so he added the following constructor. It is intended to create an FMSet from an array in such a way that if the array is does not satisfy the invariants, an IllegalArgumentException is thrown. public FMSet(int[] arr) { // maintain invariant (1) if (arr == null) { throw new IllegalArgumentException(); // initialize the array elts = arr; // calculate the size size = 0; for(int i=0; i<arr.length; i++) { size = size + elts[i]; (Question continues on the next page.) 11

Although the constructor above is guaranteed to preserve invariant (1), invariants (2) and (3) can both be broken by code that uses this constructor. Demonstrate that this is the case by completing the code for the following two tests. public void testinv2() { int[] a = {-1,0,0; try { FMSet s = new FMSet(a); fail(); catch (IllegalArgumentException e) { public void testinv3() { int[] a = {1,0,0; FMSet s = new FMSet(a); a[0] = 2; assertequals(1, s.numberof(0)); assertequals(1, s.size()); 12

e. (8 points) Fix the implementation of the broken FMSet constructor so that all interactions with the FMSet are guaranteed to preserve invariants (2) and (3). Your implementation should raise an IllegalArgumentException if a does not represent a valid multiset. public FMSet(int[] arr) { if (arr == null) { throw new IllegalArgumentException(); elts = new int[arr.length]; size = 0; for(int i=0; i<arr.length; i++) { if (arr[i] < 0) throw new IllegalArgumentException(); this.elts[i] = arr[i]; size = size + elts[i]; 13

5. Java ASM and Linked Datastructures (30 points) This problem concerns the following Java implementation of a singly-linked Queue datatype. import java.util.nosuchelementexception; class QueueNode<E> { public E v; public QueueNode<E> next; QueueNode(E elt, QueueNode<E> n) { v = elt; next = n; class Queue<E> { private QueueNode<E> head; private QueueNode<E> tail; public Queue() { head = null; tail = null; public boolean isempty() { return head == null; public void enq(e elt) { QueueNode<E> newnode = new QueueNode<E>(elt, null); if (tail == null) { head = newnode; tail = newnode; else { tail.next = newnode; tail = newnode; public E deq() { if (head == null) { throw new NoSuchElementException(); E x = head.v; head = head.next; if (head == null) { tail = null; return x; 14

a. (15 points) Draw the state of the Java Abstract Stack Machine that will be reached at the point marked HERE after running the code below. Do not show the class table, but do show the stack and heap. Be sure to label the dynamic class of each object in the heap. The appendix contains an example Java ASM configuration for your reference. Queue<String> q1 = new Queue<String>(); Queue<Queue<String>> q2 = new Queue<Queue<String>>(); q2.enq(q1); q2.enq(q1); // HERE!"#$%&'()#*+,#+-./0(%+ 2%,(9+ :0,7+ q1! q2! Queue! head! tail! null null Queue! head! tail! QueueNode! v! next! QueueNode! v! next! null!12345+6+27&8#*+4533+ 15

b. (15 points) Complete the method twin, which is added to the Queue class and creates a twin for each element in the queue. That is, if the Queue q contains the elements 1, 2, 3 in order from head to tail, then q.twin() will update q to contain the elements 1, 1, 2, 2, 3, 3. Your method should re-use the QueueNode objects from the original queue; note that this means you cannot use the deq and enq operations manipulate the pointers directly. Be sure to preserve the queue invariants. public void twin() { QueueNode<E> n = head; while (n!= null) { QueueNode<E> newnode = new QueueNode<E>(n.v, n.next); n.next = newnode; tail = newnode; n = newnode.next; Hint: If your solution does not easily fit into the space above, it is too complicated. 16

Appendix Example Java Abstract Stack Machine configuration, excluding the class table, for the following program when run to the point marked HERE : public class Node { public int elt; public Node next; public Node(int e0, Node n0) { elt = e0; next = n0; public static void main(string[] args) { Node n1 = new Node(1, null); Node n2 = new Node(2, n1); Node n3 = n2; // HERE n3.next.next = n2; Node n4 = new Node(4, n1.next); n2.next.elt = 17;!"#$%&'()#*+,#+-./0(%+ 9"&:$7,(0+ 2%,(:+ ;0,7+ n3.next.next = n2;! Node n4 = new Node(4,n1.next);! n2.next.elt = 17;! n1! n2! n3! Node! elt! 1 next! null Node! elt! 2 next!!12345+6+27&8#*+4533+ 17

Overview Package Class Use Tree Deprecated Index Help PREV CLASS NEXT CLASS FRAMES NO FRAMES All Classes SUMMARY: NESTED FIELD CONSTR METHOD DETAIL: FIELD CONSTR METHOD Java Platform Standard Ed. 6 java.io Class FileReader Method Summary java.lang.object java.io.reader java.io.inputstreamreader close, getencoding, java.io.filereader read, read, ready Methods inherited from class java.io.inputstreamreader All Implemented Interfaces: Methods Closeable, inherited Readable from class java.io.reader mark, marksupported, read, read, reset, skip public class FileReader extends InputStreamReader Methods inherited from class java.lang.object clone, equals, finalize, getclass, hashcode, notify, notifyall, tostring, wait, Convenience class for reading character files. The constructors of this class assume that the default character wait, wait encoding and the default byte-buffer size are appropriate. To specify these values yourself, construct an InputStreamReader on a FileInputStream. Constructor Detail FileReader is meant for reading streams of characters. For reading streams of raw bytes, consider using a FileInputStream. FileReader Since: JDK1.1 See Also: public FileReader(String filename) InputStreamReader, throws FileNotFoundException FileInputStream Field Parameters: Summary filename - the name of the file to read from Throws: Fields inherited from class java.io.reader lock Creates a new FileReader, given the name of the file to read from. FileNotFoundException - if the named file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading. Constructor Summary FileReader FileReader(File file) public FileReader(File file) throws FileNotFoundException FileReader(FileDescriptor fd) Creates a new FileReader, given the File to read from. Creates a new FileReader, given the FileDescriptor read from. to read from. FileReader(String filename) Parameters: Creates file a new - the FileReader, File to read given fromthe name of the file to read from. Throws: FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading. FileReader public FileReader(FileDescriptor fd) 18