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

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

CSE 331 Final Exam 3/12/12

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

Introduction to Programming Using Java (98-388)

6.005 Elements of Software Construction Fall 2008

CSE 331 Final Exam 3/16/15 Sample Solution

Rules and syntax for inheritance. The boring stuff

CSE331 Spring 2015, Final Examination June 8, 2015

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Practice for Chapter 11

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

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

Inheritance (Part 5) Odds and ends

Java Object Oriented Design. CSC207 Fall 2014

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

CSE 331 Midterm Exam 2/13/12

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

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

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

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

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.

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

CSE 331 Final Exam 12/9/13

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

Java Review: Objects

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

Announcements. Lecture 14 Generics 1. Announcements. CSE 331 Software Design and Implementation. Leah Perlmutter / Summer 2018

CSE 331 Software Design and Implementation. Lecture 14 Generics 1

Outline. More optimizations for our interpreter. Types for objects

CSE331 Autumn 2011 Midterm Examination October 28, 2011

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Parametric polymorphism and Generics

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Final Exam CSCI 2600 Principles of Software December 12, 2014

JAVA MOCK TEST JAVA MOCK TEST II

Exam 1 CSCI 2600 Principles of Software October 6, 2015

CSE 331 Spring 2018 Midterm

Exercise 8 Parametric polymorphism November 18, 2016

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

CSE wi Midterm Exam 2/8/18 Sample Solution

CS-202 Introduction to Object Oriented Programming

INHERITANCE. Spring 2019

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

CSE 331 Software Design and Implementation. Lecture 15 Generics 2

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

EINDHOVEN UNIVERSITY OF TECHNOLOGY

25. Generic Programming

CSE 331 Software Design and Implementation. Lecture 13 Generics 1

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

C a; C b; C e; int c;

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

Lecture 11 Subtypes and Subclasses

Another interface: Comparable

CSE 331 Software Design and Implementation. Lecture 12 Subtypes and Subclasses

University of Washington CSE 331 Software Design & Implementation Spring Final exam. Monday, June 10, 2013

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

CSE 331 Midterm Exam Sample Solution 2/18/15

CSE 331 Final Exam 6/7/16

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

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

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

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

Object Oriented Programming: Based on slides from Skrien Chapter 2

Exam 1 CSCI 2600 Principles of Software October 6, 2015

Logistics. Final Exam on Friday at 3pm in CHEM 102

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

Inheritance and Polymorphism

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

Top Down Design vs. Modularization

CS 215 Software Design Homework 3 Due: February 28, 11:30 PM

Representation Invariants and Abstraction Functions

CS 251 Intermediate Programming Inheritance

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

Object Oriented Programming. Java-Lecture 11 Polymorphism

CSE wi Final Exam 3/12/18 Sample Solution

Java Fundamentals (II)

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Solutions to Quiz 1 (March 14, 2016)

Prelim 1. CS 2110, 14 March 2017, 7:30 PM Total Question Name Short answer. OO Recursion Loop invariants Max Score Grader

Subtypes and Subclasses

Lecture Outline. Parametric Polymorphism and Java Generics. Polymorphism. Polymorphism

CSE 331 Final Exam 6/10/14

CSE 331 Software Design & Implementation

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM

CSE 331 Software Design & Implementation

15CS45 : OBJECT ORIENTED CONCEPTS

Universe Type System for Eiffel. Annetta Schaad

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

CSC 1351 The Twelve Hour Exam From Hell

Exam in TDDB84: Design Patterns,

Inf1-OP. Inf1-OP Exam Review. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. March 20, School of Informatics

Exam 2. Topics. Preconditions vs. Exceptions. Exam 2 Review. Exam 2 on Thursday, March 29 th. Closed book, closed computer, closed phone

Chapter 13 Abstract Classes and Interfaces. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

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

CS Programming I: Inheritance

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

Inheritance (Part 2) Notes Chapter 6

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

+ Abstract Data Types

Transcription:

Name Solution The exam is closed book and closed electronics. One page of notes is allowed. The exam has 6 regular problems and 1 bonus problem. Only the regular problems will count toward your final exam score. The bonus problem will be included in your bonus score, which may be used to break ties if you end up on the border between different grades. Please wait to turn the page until everyone is told to begin. Score / 78 1. / 17 2. / 10 3. / 14 4. / 14 5. / 6 6. / 12 7. / 16 Page 1 of 11

Problem 1 (Subtypes) 1. Which of the following is NOT an example of a way that a super- and subclass can be tightly coupled? Circle one. Subclass may depend on the pattern of self-calls in the superclass. Subclass may depend on the order of self-calls in the superclass. Subclass may depend on the names of private fields in the superclass. Subclass methods could be called when rep invariant does not hold. 2. Call the superclass from the question above A and the subclass B. Which of the following is a problem with B as a subclass but would no longer be if B were instead written with composition? Circle all that apply. B may depend on the pattern of self-calls in A. B may depend on the order of self-calls in A. B may depend on the names of private fields in A. A s methods could be called when its rep invariant does not hold. B s methods could be called when its rep invariant does not hold. 3. Which of the following would cause a subclass to fail to be a subtype? (Assume each change was legal in Java.) Circle all that apply. Subclass adds a new method. Subclass removes a method. Subclass changes a return type from Number to Integer. Subclass changes a return type from Integer to Number. Subclass strengthens the precondition of a method. Subclass strengthens the postcondition of a method. Page 2 of 11

4. Which Java keyword disallows a subclass from overriding a method? final 5. Which of the following does Josh Bloch suggest may be necessary when designing for inheritance? Circle all that apply. Declare all private fields final. Document self-calls of override-able methods Eliminate calls of override-able methods from the constructor. Provide access to the internal workings of the class by making private methods or fields protected instead. Page 3 of 11

Problem 2 (Subtypes II) Consider the following Java class: /** Represents a mutable sequence of integers. */ public class IntList { // RI: list is not null, no list entries are null // AF(this) = vals private List<Integer> vals; // If non-null, stores the current value of tostring. // This makes most calls to tostring take O(1) time. private String cachedstr; /** Creates an empty list. */ public IntList() { this.vals = new ArrayList<>(); this.cachedstr = tostring(); /** Adds the given integer to the list. */ public void add(int val) { vals.add(val); cachedstr = null; cachedstr = tostring(); /** Returns a string description of the values. */ public String tostring() { if (cachedstr!= null) return cachedstr; return vals.tostring(); (continued on next page ) Page 4 of 11

Now, consider the following subclass of IntList above: /** Stores a mutable list of integers and their product. */ public class IntListWithProduct extends IntList { // RI: prod is the product of all the values (or 1 if empty) private int prod; /** Creates an empty list. */ public IntListWithProduct() { this.prod = 1; @Override public void add(int val) { super.add(val); prod *= val; // Ex: for [2,3] this will return [2, 3] (product is 6). @Override public String tostring() { return String.format( %s (product is %d), super.tostring(), prod); 1. Explain how a method of IntListWithProduct can be called when the representation invariant does not hold. The superclass constructor calls tostring(), which is overridden. That occurs before the line in the constructor that sets prod to 1. 2. What will cachedstr contain after the self-call of tostring in the IntList constructor? (Note that int variables are initially zero before assigned.) [] (product is 0) 3. What will new IntListWithProduct().toString() actually return? [] (product is 0) (product is 1) 4. Would you say IntList was designed to support inheritance? No, I would not. Page 5 of 11

Problem 3 (Generics) 1. Any class that uses (other) generic classes should usually be generic. True False 2. Any class that implements a container ADT should usually be generic. True False 3. Suppose that T is a generic type parameter. For which of the following types can you perform an instanceof check? Circle all that apply. T T[] List<T> List<?> 4. Which of the following is most useful for working around the lack of coand contra-variant subtyping in generic parameters? arrays generic methods ArrayList generic type erasure Let S be a class and T a subtype. Suppose that our method wants to take a sequence of S s as an argument and use it in a contravariant 1 manner. 5. Which of these would be legal in Java? Circle all that apply. Declare argument as S[] and pass in T[] Declare argument as List<S> and pass in List<T> 6. Which of these would always work correctly (i.e., run without error) in Java? (Assume each was allowed / legal in Java.) Circle all that apply. Declare argument as S[] and pass in T[] Declare argument as List<S> and pass in List<T> 1 Recall: Foo<X> is covariant in X if Foo<X> is a subtype of Foo<Y> when X is a subtype of Y and contravariant if Foo<X> is a supertype of Foo<Y> as X is a subtype of Y. Page 6 of 11

Problem 4 (Design Patterns) 1. Which of the following are advantages of static factory methods over constructors? Circle all that apply. Has a name that can provide useful information about what it creates Can return an existing object instead of creating a new one Can have two with the same argument list Can return a subtype 2. Which of the following are examples of wrappers? Circle all that apply. Adapter Proxy Decorator Visitor 3. Traversing an abstract syntax tree (AST) using the procedural approach (i.e., with code grouped together by operation rather than node type) demonstrates which of the following design patterns? Circle all that apply. Adapter Interpreter Composite Visitor 4. Give an example of a design pattern used in the standard Java library for creating instances of String. Name both the class / method of the Java library and the pattern it represents. a. StringBuilder uses the Builder pattern. b. String.format is a factory method. Page 7 of 11

Problem 5 (Style) Kevin is writing a Matrix class. He starts writing his class as follows: public class Matrix { /** Creates a matrix from a list of values given in * row-major order. */ public Matrix(double[] vals, int rowsize) {... /** Creates a matrix from a list of values given in * column-major order. */ public Matrix(double[] vals, int colsize) {...... Why does this fail to compile? There are two methods definitions with the same signature. To fix it, Kevin changes the code to the following: public class Matrix { /** Creates a matrix from a list of values given in * row-major order. */ public Matrix(double[] vals, int rowsize) {... /** Creates a matrix from a list of values given in * column-major order. */ public Matrix(int colsize, double[] vals) {...... Why, even though it now compiles, does it demonstrate bad style? It would be easy for clients to confuse the two since they differ only in the order of the arguments. Page 8 of 11

Problem 6 (Debugging & Testing) Consider the following class: /** Represents a list of doubles. */ public class DoubleList { // RI: vals!= null and contains no nulls // AF(this) = [vals[0], vals[1],..., vals[vals.length-1]] private Double[] vals; /** Creates an empty list */ public DoubleList() { this.vals = new Double[0]; /** Adds the given value to the end of the list. */ public void add(double val) { // Make a new array containing vals plus the new value. Double[] newvals = new Double[vals.length + 1]; for (int i = 0; i < vals.length; i++) newvals[i] = vals[i]; newvals[vals.length] = val; this.vals = newvals; /** Return the list as an array. */ public Double[] asarray() { return vals; @Override public String tostring() { StringBuilder buf = new StringBuilder( [ ); for (int i = 0; i < vals.length; i++) { if (i > 0) buf.append(, ); buf.append(double.tostring(vals[i])); buf.append( ] ); return buf.tostring(); Page 9 of 11

1. Suppose that the code is crashing with a NullPointerException on the line that reads buf.append(double.tostring(vals[i])); With the code above, what object must be null if that exception occurs? vals[i] must be null (buf is clearly not) 2. Where is the defect that made that object null? Circle one. DoubleList() DoubleList.add client code DoubleList.toString 3. Most likely, how hard will it be to find this defect versus an average bug? harder than average easier than average 4. What should the author of DoubleList have done differently in this class to avoid this failure? He should make a copy of vals in asarray. 5. Write the method body for a JUnit test that would demonstrate the crash discussed in Problem 6 (i.e., that would fail by crashing in that manner). DoubleList lst = new DoubleList(); lst.add(1); lst.add(2); lst.asarray()[1] = null; assertequals( [1, null], lst.tostring()); Page 10 of 11

Problem 7 (Testing II) Consider the following class: /** Represents a mathematical set of integers. * Typical instances are {, {1, 2, and {6, 5, 3. */ public class IntSet {... /** Adds the given value to the set. */ public void add(int val) {...... @Override public String tostring() {... 1. Suppose that we write the following JUnit test: @Test public void testtostring() { IntSet set = new IntSet(); set.add(6); set.add(5); set.add(3); assertequals( {6, 5, 3, set.tostring()); What unspecified aspects of the behavior of IntSet.toString does this test depend on? It depends on the order of the elements when printed, even though a set does not have a defined order. 2. Why might it not be a good idea to add this behavior to the specification even though the tests depend on it? That would tell clients that they can depend on the order when printed. Page 11 of 11