Quiz 1 (October 25, 2017)

Similar documents
Solutions to Quiz 1 (October 25, 2017)

Quiz 1 (March 14, 2016)

Solutions to Quiz 1 (March 22, 2019)

Solutions to Quiz 1 (March 14, 2016)

Quiz 2 (November 20, 2015)

Solutions to Quiz 1 (October 19, 2015)

Solutions to Quiz 2 (April 27, 2018)

Quiz 2 (April 22, 2016)

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

Quiz 2 (December 3, 2018)

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

Solutions to Quiz 2 (December 3, 2018)

CSE 331 Midterm Exam Sample Solution 2/18/15

CSE wi Midterm Exam 2/8/18 Sample Solution

CSE331 Spring 2015, Final Examination June 8, 2015

CSE 331 Final Exam 3/16/15 Sample Solution

6.170 Lecture 7 Abstract Data Types MIT EECS

CSE 331 Midterm Exam 2/13/12

CSE331 Winter 2014, Midterm Examination February 12, 2014

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

Java Fundamentals (II)

Use the supplementary boxes on the last sheet if you want to change any answer.

CSE 331 Midterm Exam 11/9/15 Sample Solution

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

CSE 331 Final Exam 3/12/12

Representation Invariants and Abstraction Functions

Sec$on 2: Specifica)on, ADTs, RI WITH MATERIAL FROM MANY

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

6.005 Elements of Software Construction Fall 2008

SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 1 EXAMINATIONS 2015/2016 CI101 / CI177. Programming

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

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

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

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

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

CSE 331 Midterm Exam Sample Solution 2/13/12

ITI Introduction to Computing II

Inheritance (Part 5) Odds and ends

CS 101 Spring 2007 Midterm 2 Name: ID:

Quiz 2 (November 28, 2016)

ITI Introduction to Computing II

CSIS 10B Lab 2 Bags and Stacks

CSE wi Final Exam 3/12/18 Sample Solution

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

(b) Draw a hash table having 10 buckets. Label the buckets 0 through 9.

University of Maryland College Park Dept of Computer Science

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

CS 2334: Programming Structures and Abstractions: Exam 1 October 3, 2016

In Java there are three types of data values:

Mutable and Immutable Objects

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2013

Final Exam. Kenneth J. Goldman December 18, Name: Student ID Number: Signature:

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

Aggregation and Composition. [notes Chapter 4]

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

CSE 131 Introduction to Computer Science Fall Final Exam

Object-Oriented Concepts

(b) Draw a hash table having 10 buckets. Label the buckets 0 through 9.

This exam is open book. Each question is worth 3 points.

Ohad Barzilay and Oranit Dror

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

Final Exam Practice. Partial credit will be awarded.

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

Exam 2 CSCI 2600 Principles of Software November 3, 2015

Lecture 7: Data Abstractions

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

Final Exam. COMP Summer I June 26, points

CSE331 Winter 2014, Midterm Examination February 12, 2014

Exam Percentage: / 55 = %

So far. Specifications, conclusion. Abstract Data Types (ADTs) Comparison by Logical Formulas. Outline

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

CS 113 PRACTICE FINAL

CSE 331 Final Exam 6/5/17 Sample Solution

CSE331 Autumn 2011 Midterm Examination October 28, 2011

CS 455 Midterm Exam 2 Spring 2017 [Bono] Tuesday, April 4, 2017

6.824 Distributed System Engineering: Spring Quiz I Solutions

CS2102, B11 Exam 1. Name:

Encapsulation in C++

INHERITANCE. Spring 2019

if (x == 0); System.out.println( x=0 ); if (x = 0) System.out.println( x=0 );

COMP 401 Midterm. Tuesday, Oct 18, pm-3:15pm. Instructions

CSE 331 Spring 2018 Midterm

UMBC CMSC 331 Final Exam

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

University of Washington CSE 140 Introduction to Data Programming Winter Midterm exam. February 6, 2013

Lesson 8 Passing Parameters. By John B. Owen All rights reserved 2011, revised 2014

CS 170 Exam 2. Version: A Fall Name (as in OPUS) (print): Instructions:

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

Creating an Immutable Class. Based on slides by Prof. Burton Ma

Motivations. Chapter 13 Abstract Classes and Interfaces

CS 455 Midterm 2 Spring 2018 [Bono] Apr. 3, 2018

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

Lecture 5 Abstract Data Types

Collections, Maps and Generics

CSE373 Winter 2014, Final Examination March 18, 2014 Please do not turn the page until the bell rings.

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

CS 455 Midterm 2 Fall 2017 [Bono] Nov. 7, 2017

Transcription:

MIT 6.031: Software Construction Prof. Rob Miller & Max Goldman revised Tuesday 24 th October, 2017, 15:03 Quiz 1 (October 25, 2017) Your name: Your Kerberos username: You have 50 minutes to complete this quiz. It contains 10 pages (including this page) for a total of 100 points. The quiz is closed-book and closed-notes, but you are allowed one two-sided page of notes. Please check your copy to make sure that it is complete before you start. Turn in all pages, together, when you finish. Before you begin, write your Kerberos username on the top of every page. Please write neatly. No credit will be given if we cannot read what you write. For questions which require you to choose your answer(s) from a list, do so clearly and unambiguously by circling the letter(s) or entire answer(s). Do not use check marks, underlines, or other annotations they will not be graded. Good luck! Problem Points 1: Code review 20 2: Equality 16 3: Immutable ADT 22 4: Mutable ADT 18 5: Testing and Operations 24 Total 100

2 Your Kerberos username: Quiz 1 (October 25, 2017) For this quiz, a shopping list is an ordered list of unique items together with a positive integer quantity for each item (we will not consider units for quantities, e.g. 1 oz. vs. 1 lb. vs. 1 box, just a number). For example, someone who plans to buy a gallon of milk, a small bunch of bananas, and a box of Klondike bars, might have this shopping list: Milk (1) Banana (3) Klondike bars (1) Shopping lists are ordered, so the following is a different shopping list. quantities, but in a different order: It has the same items and Banana (3) Klondike bars (1) Milk (1) This is not a shopping list: Banana (1) Banana (1) Banana (1) Klondike bars (1) Milk (1) And neither is this: Milk (0) Problems 1 3 refer to the code for FixedShoppingList on page 9, which you may detach. A client uses FixedShoppingList like this: FixedShoppingList s = new FixedShoppingList(Arrays.asList( "Banana", "Milk", "Banana", "Klondike bars", "Banana")); System.out.println(s); which produces: Banana (3) Milk (1) Klondike bars (1) Notice that the bananas have ended up first on this list, and the Klondike bars have ended up last.

Quiz 1 (October 25, 2017) Your Kerberos username: 3 Problem 1 (Code review) (20 points). For each of these code review comments on FixedShoppingList, circle AGREE or DISAGREE and explain why in one sentence. (a) Line 21: in tostring(), reduce the scope of seen. AGREE / DISAGREE because: (b) Lines 21 22: in tostring(), make seen and result final. AGREE / DISAGREE because: (c) Line 3: declare replist as private. AGREE / DISAGREE because: (d) Line 3: declare replist as final. AGREE / DISAGREE because: (e) Line 6: in the constructor, just use Collections.unmodifiableList(items) instead of new ArrayList<>(items). AGREE / DISAGREE because:

4 Your Kerberos username: Quiz 1 (October 25, 2017) Problem 2 (Equality) (16 points). Alyssa looks at FixedShoppingList and realizes that it should implement equals(). She also sees that that just checking whether two FixedShoppingList instances have identical rep values will not be a correct implementation. (a) Write two different rep values for FixedShoppingList that represent the same abstract value according to the code and specs given (e.g., tostring): (b) And write clearly and completely their abstract value: Suppose we implement equals() correctly, and now wish to implement hashcode(). Remember that if two objects are equal, then they must have the same hashcode. For each option below for the body of hashcode(), circle CORRECT or INCORRECT and explain why in one sentence. (c) return replist.hashcode(); CORRECT / INCORRECT because: (d) if (replist.isempty()) { return 0; else { return replist.get(0).hashcode(); CORRECT / INCORRECT because:

Quiz 1 (October 25, 2017) Your Kerberos username: 5 Problem 3 (Immutable ADT) (22 points). Reminder: please write neatly. Make sure each answer satisfies all the requirements of the question. (a) Write an abstraction function for FixedShoppingList that works with the code and specs given. (b) Write requires and effects to create a stronger spec for withanother() (compared to the one in the code, lines 7 8) that is still satisfied by the given implementation. In addition, your spec must allow this as a valid test case: [ Milk (1) ].withanother("cookies") = [ Milk (1), Cookies (1) ] where we check the items, their order, and their quantities in the result. (c) Write requires and effects to create a weaker spec for howmany() (compared to the one in the code, lines 12 13) that is still satisfied by the given implementation. In addition, your spec must allow this as a valid test case: [ Milk (1) ].howmany("milk") = 1

6 Your Kerberos username: Quiz 1 (October 25, 2017) Problem 4 (Mutable ADT) (18 points). Problems 4 6 refer to the code for MutableShoppingList on page 10. A client uses MutableShoppingList like this: MutableShoppingList m = new MutableShoppingList(); m.another("banana"); m.another("milk"); m.another("banana"); m.another("klondike bars"); m.another("banana"); System.out.println(m); which produces: Banana (3) Klondike bars (1) Milk (1) Ben looks at MutableShoppingList. Alyssa, how does this TreeMap work? It keeps its keys sorted in alphabetical order. OK, says Ben. (a) Write requires and effects to create a stronger spec for the MutableShoppingList() constructor (compared to the one in the code, lines 4 5) that is still satisfied by the given implementation. (b) Write one statement to include in the rep invariant for MutableShoppingList that works with the code and specs given, and that 6.031 does not already assume implicitly. (c) And write one piece of the rep invariant that 6.031 does already assume implicitly.

Quiz 1 (October 25, 2017) Your Kerberos username: 7 Problem 5 (Testing and Operations) (24 points). Alyssa looks at MutableShoppingList. This howmany method has a bug! Did anyone test this thing? Devise a testing strategy for the howmany() operation: (a) Write one good three-part partition on the method s implicit input only, ignoring item. (b) Write one good three-part partition on the relationship between the method s two inputs. MutableShoppingList uses SortedMap and TreeMap. (c) In one sentence, what is the relationship between the types SortedMap and TreeMap? (d) In one sentence, what is the relationship between the specs of SortedMap and TreeMap? One creator ADT operation used in the MutableShoppingList code is the TreeMap constructor (line 6). For each other kind of ADT operation in our taxonomy, give an example that is either defined or used in the MutableShoppingList code. Write the operation s type signature in function notation with its name, inputs, and outputs. Creator: TreeMap : void TreeMap (e) Producer: : (f) Observer: : (g) Mutator: :

8 Your Kerberos username: Quiz 1 (October 25, 2017) S F B E T U R F C

Quiz 1 (October 25, 2017) Your Kerberos username: 9 You may detach this page. Write your username at the top, and hand in all pages when you leave. Immutable FixedShoppingList 1 /** Immutable shopping list. */ 2 public class FixedShoppingList { 3 List<String> replist; 4 /** Make a new shopping list. */ 5 public FixedShoppingList(List<String> items) { 6 replist = new ArrayList<>(items); 7 /** Make a new shopping list that includes itemtoadd. */ 8 public FixedShoppingList withanother(string itemtoadd) { 9 List<String> items = new ArrayList<>(replist); 10 items.add(itemtoadd); 11 return new FixedShoppingList(items); 12 /** Return the quantity of item on this list. */ 13 public int howmany(string item) { 14 int count = 0; 15 for (String listitem : replist) { 16 if (item.equals(listitem)) { count++; 17 return count; 18 /** Return a string where the items on this list appear in order, 19 * each on a separate line along with its quantity. */ 20 @Override public String tostring() { 21 Set<String> seen = new HashSet<String>(); 22 StringBuilder result = new StringBuilder(); 23 for (String item : replist) { 24 if (! seen.contains(item)) { 25 seen.add(item); 26 result.append(item + " (" + howmany(item) + ")\n"); 27 return result.tostring();

10 Your Kerberos username: Quiz 1 (October 25, 2017) You may detach this page. Write your username at the top, and hand in all pages when you leave. Mutable MutableShoppingList 1 /** Mutable shopping list. */ 2 public class MutableShoppingList { 3 SortedMap<String, Integer> itemcounts; 4 /** Make a new shopping list. */ 5 public MutableShoppingList() { 6 itemcounts = new TreeMap<>(); 7 /** Add 1 of the given item to this list. */ 8 public void another(string item) { 9 itemcounts.put(item, itemcounts.getordefault(item, 0) + 1); 10 /** Return the quantity of item on this list. */ 11 public int howmany(string item) { 12 return itemcounts.get(item); // BUG! 13 /** Return a string where the items on this list appear in order, 14 * each on a separate line along with its quantity. */ 15 @Override public String tostring() { 16 StringBuilder result = new StringBuilder(); 17 for (String item : itemcounts.keyset()) { 18 result.append(item + " (" + howmany(item) + ")\n"); 19 return result.tostring();