CSE 331 Summer 2016 Final Exam. Please wait to turn the page until everyone is told to begin.

Similar documents
CSE 331 Summer 2017 Final Exam. The exam is closed book and closed electronics. One page of notes is allowed.

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

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

Prelim 1. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants

CSE 331 Final Exam 3/12/12

CSE331 Winter 2014, Midterm Examination February 12, 2014

CSE 331 Final Exam 3/16/15 Sample Solution

CSE331 Winter 2014, Midterm Examination February 12, 2014

CSE 331 Midterm Exam 2/13/12

CSE 331 Midterm Exam Sample Solution 2/18/15

Java Review: Objects

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

CSE 331 Final Exam 12/14/15 Sample Solution. Question 1. (20 points, 1 each) Warmup. For each statement, circle T if it is true and F if it is false.

Assertions, pre/postconditions

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

CSE wi Midterm Exam 2/8/18 Sample Solution

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

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

Exam 1 CSCI 2600 Principles of Software October 6, 2015

CSE331 Spring 2015, Final Examination June 8, 2015

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 10/14/2004 1

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

1 Inheritance (8 minutes, 9 points)

n Specifying what each method does q Specify it in a comment before method's header n Precondition q Caller obligation n Postcondition

Introduction to Programming Using Java (98-388)

CSE 331 Midterm Exam Sample Solution 2/13/12

CS 1110: Introduction to Computing Using Python Loop Invariants

CSE 331 Spring 2018 Midterm

Announcements. Lecture 15 Generics 2. Announcements. Big picture. CSE 331 Software Design and Implementation

CSE 331 Software Design and Implementation. Lecture 15 Generics 2

CS 3 Introduction to Software Engineering. 3: Exceptions

CSE 143 Au04 Midterm 2 Sample Solution Page 1 of 7

Java classes cannot extend multiple superclasses (unlike Python) but classes can implement multiple interfaces.

Prelim 1. CS 2110, March 15, 2016, 5:30 PM Total Question Name True False. Short Answer

CSE 331 Software Design and Implementation. Lecture 3 Loop Reasoning

The list abstract data type defined a number of operations that all list-like objects ought to implement:

15-122: Principles of Imperative Computation (Section G)

Assertions & Design-by-Contract using JML Erik Poll University of Nijmegen

Exam 1 CSCI 2600 Principles of Software October 6, 2015

Reasoning about programs

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Last time. Reasoning about programs. Coming up. Project Final Presentations. This Thursday, Nov 30: 4 th in-class exercise

Program Correctness and Efficiency. Chapter 2

Prelim 1. Solution. CS 2110, 14 March 2017, 7:30 PM Total Question Name Short answer

Programming II (CS300)

CSE 331 Final Exam 12/9/13

Java Review Outline. basics exceptions variables arrays modulo operator if statements, booleans, comparisons loops: while and for

JAVA BASICS II. Example: FIFO

Midterm Exam CS 251, Intermediate Programming March 12, 2014

Final Exam. COMP Summer I June 26, points

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Polymorphism. return a.doublevalue() + b.doublevalue();

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

Prelim 1. CS 2110, March 15, 2016, 7:30 PM Total Question Name True False. Short Answer

When the above code runs, which type s execute method will be run? When the above code runs, which type s execute method will be run?

Assertions. Assertions - Example

Programming II (CS300)

Software Engineering Testing and Debugging Testing

Qualifying Exam Languages

CS61BL Summer 2013 Midterm 2

Checked and Unchecked Exceptions in Java

CS16 Exam #1 7/17/ Minutes 100 Points total

6.005 Elements of Software Construction Fall 2008

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Testing and Debugging

CSE331 Winter 2014, Final Examination March 17, 2014 Please do not turn the page until 8:30. Rules:

CS159. Nathan Sprague. September 30, 2015

CSE P 501 Exam 8/5/04

CS211 Computers and Programming Matthew Harris and Alexa Sharp July 9, Boggle

CSE 331 Final Exam 6/5/17. Name UW ID#

Use the scantron sheet to enter the answer to questions (pages 1-6)

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

CS 251 Intermediate Programming Methods and Classes

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

CS 251 Intermediate Programming Methods and More

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

About this sample exam:

EECS2030 Week 7 worksheet Tue Feb 28, 2017

CSE wi Final Exam 3/12/18. Name UW ID#

CSE351 Winter 2016, Final Examination March 16, 2016

COS 126 General Computer Science Spring Written Exam 1

Exception-Handling Overview

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

17. Assertions. Outline. Built-in tests. Built-in tests 3/29/11. Jelle Slowack, Bart Smets, Glenn Van Loon, Tom Verheyen

Violations of the contract are exceptions, and are usually handled by special language constructs. Design by contract

Lecture 24: Loop Invariants

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

17. Assertions. Jelle Slowack, Bart Smets, Glenn Van Loon, Tom Verheyen

Lecture 4: Procedure Specifications

Answer Key. 1. General Understanding (10 points) think before you decide.

Software Engineering

COE318 Lecture Notes Week 10 (Nov 7, 2011)

Inheritance and Testing Spring 2018 Exam Prep 4: February 11, 2019

Fun facts about recursion

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Prelim 1 Solutions. CS 2110, March 10, 2015, 5:30 PM Total Question True False. Loop Invariants Max Score Grader

CS Lecture 19: Loop invariants

Day 8. COMP1006/1406 Summer M. Jason Hinek Carleton University

Transcription:

Name The exam is closed book, closed notes, and closed electronics. Please wait to turn the page until everyone is told to begin. Score / 54 1. / 12 2. / 12 3. / 10 4. / 10 5. / 10 Bonus: 1. / 6 2. / 4 Page 1 of 11

Question 1. Circle the correct answer for each question below. a. If done correctly, which of the following can rule out any possibility of bugs in a complex method: Type Checking Reasoning Testing b. If you believe your reasoning is correct, it is not necessary to write tests. True False c. If you believe your reasoning is correct, it is not necessary to use any runtime assertions. True False d. Which of the following is NOT a benefit of writing method specifications? can prove correctness code is more readable can write tests code is more efficient e. Which of the following is NOT a benefit of crashing immediately upon discovery of a bug in the program? easier debugging limits further damage bug is hidden from the user bug is less likely to go undetected f. Which of the following is NOT necessary to prove a loop correct? show precondition holds show invariant holds initially show invariant and termination condition imply the postcondition show invariant holds after the loop body Page 2 of 11

Question 2. For each question below, write a short answer (1-2 sentences). a. If you were redesigning the Java libraries from scratch, would you have NullPointerException be a checked or unchecked exception? Unchecked. If it were checked, every method that used a reference variable (i.e., most of them) would have to declare that it could throw one, which would be extremely laborious and would provide little benefit. b. The following code does not compile: class Foo<T> { public void foo() { T[] arr = new T[10]; // compiler error... What do you think the author should do instead? Either allocate a new Object[10] and cast it to T[] or else just use an ArrayList<T> instead. c. Consider the following method specification: /** @returns the sum of a and b */ Double sum(number a, Number b); Describe two different ways to weaken the specification. 1. Change the return type to Number. 2. Change either argument type to Double. Page 3 of 11

Question 3. Consider the following methods operating on lists: // Returns the sum of the given numbers. double sumnumbers(iterator<number> iter) { double s = 0; while (iter.hasnext()) s += iter.next().doublevalue(); return s; // Returns the sum of the given doubles. double sumdoubles(iterator<double> iter) { double s = 0; while (iter.hasnext()) s += iter.next().doublevalue(); return s; Suppose that we also have the following list variables: List<Number> numlist = new ArrayList<Number>(); numlist.add(1.0); numlist.add(2.0); numlist.add(3.0); List<Double> dbllist = new ArrayList<Double>(); dbllist.add(1.0); dbllist.add(2.0); dbllist.add(3.0); a. Circle those of the following lines of code that have a compiler error: s = sumnumbers(numlist.iterator()); s = sumnumbers(dbllist.iterator()); s = sumdoubles(numlist.iterator()); s = sumdoubles(dbllist.iterator()); Page 4 of 11

b. One line above that has a compiler error can be fixed by introducing an adapter that wraps the iterator currently used in the code and adapts it to fit the interface needed by the method being called. To use it, the line above would be changed to look like this: sum??(new IterAdapter(??List.iterator())); Write an implementation of IterAdapter that will make that one line compile and run correctly when changed as just shown. /** Converts Iterator<Double> to Iterator<Number>. */ public class IterAdapter implements Iterator<Number> { private Iterator<Double> iter; /** Creates a wrapper on the given iterator. */ public IterAdapter(Iterator<Double> iter) { this.iter = iter; @Override public boolean hasnext() { return iter.hasnext(); @Override public Number next() { return iter.next(); This could then be used above as: sumnumbers(new IterAdapter(dblList.iterator())); c. If you were designing Java from scratch, would you want programmers to have to write the code above? Explain. No. The adapter above doesn t actually do anything! It just takes the outputs from the iterator and returns them. The only purpose of this adapter is to fix a weakness in the type checker. The compiler should just figure this out on its own. Page 5 of 11

The next two problems have the same format. Each shows you some code that has a significant bug. Then its ask you to: (1) explain where the proof of correctness would break down and (2) describe a test that would have caught the bug. Here is an example of what we are looking for Consider the following code, which has a significant bug: /** @returns (degfahr 32) * 5 / 9; public static int fahrenheittocelcius(int degfahr) { int x = 5 * degfahr; int y = x / 9; int degcelcius = y - 32; return degcelcius; a. Where does the proof of correctness for this code fail? Forward reasoning tells us that degcelcius = degfahr * 5 / 9 32. Returning this fails to match the postcondition. The latter wants the number (degfahr 32) * 5 / 9, which generally is not the same. b. Describe a test (in English or JUnit code) that would have caught the bug. assertequals(0, farhenheittocelcius(32)); or Calling fahrenheittocelciums with input 32 would return the answer 32 * 5 / 9 32, which is not zero, whereas the correct answer is (32 32) * 5 / 9 = 0. Page 6 of 11

Question 4. Consider the following code, which has a significant bug: 1 /** @returns the greatest common denominator of m & n */ 2 public static int gcd(int m, int n) { 3 if (m >= n) 4 return gcdhelper(m, n); 5 else 6 return gcdhelper(m, n); 7 8 9 /** @returns the gcd of m and n if m >= n 10 * @throws IllegalArgumentException if m < n */ 11 public static int gcdhelper(int m, int n) {... Hint: you don t need to know what the greatest common denominator (gcd) is to solve this problem. c. Where does the proof of correctness for this code fail? Forward reasoning tells us that m < n before line 6. As a result, the precondition of gcdhelper does not hold, so we cannot infer the postcondition we need after the call. d. Describe a test (in English or JUnit code) that would have caught the bug. assertequals(1, gcd(1, 2)); // fails due to exception Page 7 of 11

Question 5. Consider the following code, which has a significant bug: 1 /** @requires max >= 2 2 * @returns the largest prime not bigger than max */ 3 public static int getlargestprime(int max) { 4 int lastprime = 2; 5 int n = 2; 6 // Inv: lastprime is largest prime not bigger than n 7 while (n!= max) { 8 if (isprime(n)) 9 lastprime = n; 10 n += 1; 11 12 return lastprime; 13 14 15 /** @returns true iff the number is prime */ 16 public static boolean isprime(int n) {... a. Where does the proof of correctness for this code fail? Forward reasoning inside the loop body (starting from the loop invariant) tells us that lastprime holds the largest prime not bigger than n - 1, but that is not the same as Inv, so we can t conclude that the loop invariant holds after the loop body. b. Describe a test (in English or JUnit code) that would have caught the bug. assertequals(3, getlargetprime(3)); // fails Page 8 of 11

Bonus Question 1. Consider the following code: public class IntList {... /** @modifies this * @effects Removes all entries in the list, from the * given index to the end, with the given value. * E.g., on [1, 2, 3, 2, 5, 2], removefrom(3, 2) * would change the list to [1, 2, 3, 5] */ public void removefrom(int index, int value); /** @modifies this * @effects Removes all entries after the first * occurrence of the given value appearing in the * list after the given index. E.g., on * [1, 2, 3, 2, 5, 2], removeafter(3, 2) would * change the list to [1, 2, 3, 2]. */ public void removeafter(int value, int index);... There are at least three different ways in which this code is worrisome ways in which it is likely to lead to bugs in the client code. Describe two of them. 1. The method removefrom has two arguments of the same type, so clients could easily mix up the arguments and see no compiler error. (The same issue exists with removeafter.) 2. The two methods removefrom and removeafter are inconsistent in the order of the two arguments (value and index), which makes a mistake by the client even more likely. 3. The two methods removefrom and removeafter have very similar names. The names do not make clear enough which is which. It would be easy for the client to mix up these methods. The descriptions of these methods are also hard to follow, which is not good. The above class is a wonderful example of how not to write code. Page 9 of 11

Bonus Question 2. Consider the following code: /** Maintains a map built from a list of (key,value) * pairs read one-at-a-time (i.e., from a stream ). */ public class MapStream<K,V> { private Map<K,V> map = new HashMap<K,V>(); /** @returns the value (if any) for the given key */ public V get(k key) { return map.get(key); /** @effects Adds the next (key,value) from the stream. * @returns the key from the next pair */ public K next() { K key = nextkey(); V val = nextvalue(); map.put(key, val); return key; /** @returns key from the next (key,value) pair. */ protected abstract K nextkey(); /** @returns value from the next (key,value) pair. */ protected abstract V nextvalue(); /** Maintains a map of pairs (n, p) where p is the n-th * prime number. These are added in order by n. */ public class PrimeStream extends MapStream<Integer,Integer> { private int n = 0; private int lastprime = 1; @Override protected Integer nextkey() { n += 1; lastprime += 1; while (!isprime(lastprime)) lastprime += 1; return n; @Override protected Integer nextvalue() { return lastprime; Page 10 of 11

This above code works correctly, but one aspect is extremely worrisome, particularly if the superclass and subclass were written by different people. Describe why it would be easy for the author of the superclass to break the code in the subclass without realizing it. (This should only take a few sentences.) The subclass only works correctly because nextkey is called before nextvalue. If the superclass were to call these in the opposite order, the subclass code would break. It is unlikely that the author of the superclass realized that the client would be written in such a way as to become dependent on the order of these two calls, so they might change the order for some reason in the future and not realize that the change would break some subclasses. Page 11 of 11