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

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

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

University of Palestine. Mid Exam Total Grade: 100

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

CMP 326 Midterm Fall 2015

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

CIS 120 Midterm II November 8, 2013 SOLUTIONS

Prelim 1. CS 2110, 13 March 2018, 5:30 PM Total Question Name Short answer

JAVA OPERATORS GENERAL

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

Introduction to Computing II (ITI 1121) FINAL EXAMINATION

Midterm CSE 131 Fall 2014

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

CMSC 132, Object-Oriented Programming II Summer Lecture 9:

Lara Technologies Special-Six Test

Midterm CSE 131 Winter 2013

Selenium Class 9 - Java Operators

University of Massachusetts Amherst, Electrical and Computer Engineering

CSE 143 SAMPLE MIDTERM

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

CS 455 Midterm Exam 1 Fall 2016 [Bono] Thursday, Sept. 29, 2016

CSC 240 Computer Science III Spring 2018 Midterm Exam. Name

d. If a is false and b is false then the output is "ELSE" Answer?

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

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

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

CMSC 331 Second Midterm Exam

Practice Questions for Chapter 9

INSTRUCTIONS TO CANDIDATES

Computational Expression

Linked List Nodes (reminder)

Place your name tag here

WES-CS GROUP MEETING #9

CMPS 11 Intermediate Programming Midterm 2 Review Problems

CS 231 Data Structures and Algorithms, Fall 2016

CS18000: Programming I

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.

Lab 5 Random numbers!

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?

CSC 172 Data Structures and Algorithms. Lecture #9 Spring 2018

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

No Aids Allowed. Do not turn this page until you have received the signal to start. Read this entire page or you ll miss the bonus question.

Programming Languages and Techniques (CIS120)

Object Oriented Software Design

Common Misunderstandings from Exam 1 Material

6.005 Elements of Software Construction Fall 2008

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

Computer Science 62. Midterm Examination

Prof. Navrati Saxena TA: Rochak Sachan

Programming Languages and Techniques (CIS120)

coe318 Lab 2 ComplexNumber objects

ITI Introduction to Computing II

Java Language Features

Midterm CSE 131 Winter 2014

DM503 Programming B. Peter Schneider-Kamp.

CMSC132 Summer 2018 Midterm 1. Solution

Final CSE 131 Fall 2014

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

Introduction to Linked Data Structures

CMPS 12A Introduction to Programming Midterm 2 Review Problems

Programming Languages and Techniques (CIS120)

An Activation Record for Simple Subprograms. Activation Record for a Language with Stack-Dynamic Local Variables

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

int j = 0, sum = 0; for (j = 3; j <= 79; j++) { sum = sum + j; System.out.println(sum); //Show the progress as we iterate thru the loop.

CS 61B Discussion 5: Inheritance II Fall 2014

Note that if both p1 and p2 are null, equals returns true.

CS 101 Exam 2 Spring Id Name

ITI Introduction to Computing II

Programming Languages and Techniques (CIS120)

for dequeue will be O(N). (Or words to that effect. Needed to mention both or -1.)

Account joeacct = new Account (100, new Account (500)); Account joeacct = new Account (100, new Account (500, null));

ENGR 2710U Midterm Exam UOIT SOLUTION SHEET

20 Subclassing and Mutation

CIS 120 Midterm II November 16, 2012 SOLUTIONS

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

Object Oriented Software Design

More on methods and variables. Fundamentals of Computer Science Keith Vertanen

Class, Variable, Constructor, Object, Method Questions

CIS 120 Final Exam 19 December 2014 SOLUTIONS

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

CS 152 Computer Programming Fundamentals The if-else Statement

JAVA BASICS II. Example: FIFO

About this exam review

CS2102, B11 Exam 2. Name:

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

ECS 140A Midterm 2 November 19, Name (Last, First):, Sign your name:

CSC 172 Data Structures and Algorithms. Lecture #8 Spring 2018

CMPS 12A - Winter 2002 Midterm 2 March 5, Name: ID:

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Introduction to Computer Science II (ITI 1121) Final Examination

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws

Java Exceptions Java, winter semester

CSC Java Programming, Fall Java Data Types and Control Constructs

CMP-326 Final Exam Fall 2013 Solutions Version 1

Java Magistère BFA

Principles of Computer Science

CSE 143 Final Part 1, August 18, 2011 Sample Solution

Transcription:

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

1. (14 points) Pages 7 to 9 define a simplified version of the Java Collection interface (the SimpleCollection and SimpleIterator interfaces), together with a concrete implementation using linked lists (the class LinkedSimpleCollection and the auxiliary class ). These definitions are identical to those in the review handout, except that the LinkedSimpleIterator class has been slightly simplified. Suppose we execute the following program: public class Main { public static void main(string[] args) { SimpleCollection<String> c = new LinkedSimpleCollection<String>(); c.add("c"); c.add("b"); c.add("a"); SimpleIterator<String> i = c.iterator(); i.next(); c.removeall("b"); String s = i.next(); // HERE In the space below, draw a diagram of the memory configuration (both stack and heap) when execution reaches the line marked HERE. (You can omit the parameter args to the main method.) Use the same conventions as the sample stack/heap drawing on page 10. Answer: Stack Heap c LinkedSimpleCollection i first s next next next null String "a" String "b" String "c" LinkedSimpleIterator current Grading scheme: 11-14 points for nearly correct answers; 7-10 points for answers with 2 or 3 individual mistakes; 3-6 points for answers with several issues. 1

2. (6 points) Briefly (in two or three sentences) compare the way mutable state is treated in OCaml and in Java. Answer: OCaml uses several different language constructs to deal with different aspects of mutable state: naming (variable binding), mutability (ref types), and the possibility for a value to be missing (option types). Java combines these features: every variable is mutable, and every variable of reference type can contain the special value null. Grading scheme: 3 points for observing that Java defaults to mutable and OCaml defaults to immutable; 2 points for mentioning explicit ref types (or explicit! and :=); 1 point for metioning null vs. option. 2

3. (4 points) What gets printed if the expression (new C()).foo() is evaluated in the presence of the following class definitions? class MyExn extends RuntimeException { class AnotherExn extends MyExn { class C { public void foo() { try { System.out.println("a"); bar(); System.out.println("b"); catch (AnotherExn e) { System.out.println("c"); catch (MyExn e) { System.out.println("d"); finally { System.out.println("e"); Answer: a j m g h c e public void bar() { try { baz(); catch (AnotherExn e) { System.out.println("f"); throw e; catch (MyExn e) { System.out.println("g"); throw new AnotherExn(); finally { System.out.println("h"); System.out.println("i"); public void baz() { try { System.out.println("j"); if (true) throw new MyExn(); System.out.println("k"); catch (AnotherExn e) { System.out.println("l"); finally { System.out.println("m"); System.out.println("n"); 3

4. (14 points) Complete the following definition of the class IterMax. Instances of IterMax are iterator transformers iterators built from other iterators. The IterMax constructor takes a String iterator wrapped (an instance of the SimpleIterator class defined on page 9) as an argument, and the hasnext and next methods use the corresponding methods of wrapped to do their job. Each time next is called on the outer IterMax iterator, it should return the first from the inner wrapped iterator that is longer than all the s previously returned. For example if the inner Iterator produces the sequence "a" "bb" "c" "ddd" "ab" "abc" "eeee" then the outer one would produce: "a" "bb" "ddd" "eeee" The outer iterator should never return null (even if the inner iterator does). Answer: class IterMax implements SimpleIterator<String> { SimpleIterator<String> it; int m = -1; String n; IterMax (SimpleIterator<String> wrapped) { it = wrapped; advance(); public boolean hasnext () { return (n!= null); public String next () { String s = n; advance(); return s; void advance () { boolean done = false; n = null; while (!done) { if (!it.hasnext()) { done = true; else { n = it.next(); if (n!= null && n.length() > m) { done = true; m = n.length(); 4

5. (12 points) This problem again refers to the definitions of SimpleCollection and related classes on pages 7 to 9. Your job is to complete the definition of a method interleave that combines the contents of two LinkedSimpleCollections in-place, in alternating order. For example, if c contains "a", "c", and "e" (in that order) and c2 contains "b", and "d" (in that order), then after c.interleave(c2), c should contain "a", "b", "c", "d", and "e", and c2 should be empty. If the two initial collections are not the same size, the extra s should appear at the end of the interleaved collection. Your solution should not create any new objects (i.e., don t use new anywhere). Answer: class LinkedSimpleCollection<E> implements SimpleCollection<E> { //... public void interleave(linkedsimplecollection<e> other) { if (other == null) { return; if (this.first == null) { this.first = other.first; other.first = null; return; if (other.first == null) return; <E> last = this.first; <E> left = this.first.next; <E> right = other.first; other.first = null; boolean takeright = true; while (true) { if (left == null) { last.next = right; return; else if (right == null) { last.next = left; return; if (takeright) { last.next = right; right = right.next; else { last.next = left; left = left.next; 5

takeright =!takeright; last = last.next; Grading scheme: The top level grading was broken into three 4-point sections: the basic pointer setup, the basic loop structure, and the corner cases necessary for correctness. Single point deductions were made for, e.g., not considering other == null. Larger deductions were made for bad logic in the loop. Solutions with iterators and some logic were typically awarded 2 points. 6

For Reference: SimpleCollection, SimpleIterator, and interface SimpleCollection<E> { boolean removeall(e ); boolean add(e ); boolean contains(object o); SimpleIterator<E> iterator(); interface SimpleIterator<E> { boolean hasnext(); E next(); class <E> { public E ; public <E> next; public (E, <E> next) { this. = ; this.next = next; 7

For Reference: LinkedSimpleCollection class class LinkedSimpleCollection<E> implements SimpleCollection<E> { private <E> first = null; public boolean removeall(e ) { boolean changed = false; while (first!= null && first..equals()) { first = first.next; changed = true; if (first == null) return changed; <E> prev = first; for (<E> current = first.next; current!= null; current = current.next) { if (current..equals()) { prev.next = current.next; changed = true; prev = prev.next; return changed; public boolean add(e ) { <E> newnode = new <E>(, first); first = newnode; return true; public boolean contains(object o) { for (<E> current = first; current!= null; current = current.next) { if ( (current. == null && o == null) (current.!= null && current..equals(o))) { return true; return false; public SimpleIterator<E> iterator() { return new LinkedSimpleIterator<E>(first); 8

For Reference: LinkedSimpleIterator class class LinkedSimpleIterator<E> implements SimpleIterator<E> { private <E> current; public LinkedSimpleIterator (<E> first) { current = first; public boolean hasnext() { return (current!= null); public E next() { if (current == null) { throw new java.util.nosuchelementexception(); else { E n = current.; current = current.next; return n; 9

For Reference: Sample Stack/Heap Drawing Stack coll1 coll2 c e Heap LinkedSimpleCollection first next null next String "foo" String "bar" LinkedSimpleCollection first next null 10