Quiz 1 (March 14, 2016)

Similar documents
Solutions to Quiz 1 (March 14, 2016)

Quiz 1 (October 25, 2017)

Quiz 2 (November 20, 2015)

Quiz 2 (April 22, 2016)

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

Solutions to Quiz 1 (March 22, 2019)

CSE wi Midterm Exam 2/8/18 Sample Solution

Solutions to Quiz 1 (October 25, 2017)

Solutions to Quiz 1 (October 19, 2015)

Quiz 2 (December 3, 2018)

CSE331 Winter 2014, Midterm Examination February 12, 2014

Solutions to Quiz 2 (December 3, 2018)

Solutions to Quiz 2 (April 27, 2018)

CSE 331 Midterm Exam Sample Solution 2/18/15

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

CSE 331 Spring 2018 Midterm

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

CSE 331 Final Exam 3/12/12

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

CS108, Stanford Handout #3. HW1 CodeCamp

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

Fall 2017 Mentoring 9: October 23, Min-Heapify This. Level order, bubbling up. Level order, bubbling down. Reverse level order, bubbling up

CSE 331 Final Exam 6/7/16

CSE331 Autumn 2011 Midterm Examination October 28, 2011

Exam 1 CSCI 2600 Principles of Software October 6, 2015

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

CS 455 Final Exam Fall 2013 [Bono] December 12, 2013

CSE331 Spring 2015, Final Examination June 8, 2015

Domain-Driven Design Activity

Massachusetts Institute of Technology 6.005: Elements of Software Construction Fall 2011 Quiz 2 November 21, Instructions

CS 455 Midterm Exam 1 Fall 2017 [Bono] Thursday, Sep. 28, 2017

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

CS-202 Introduction to Object Oriented Programming

CSE331 Winter 2014, Midterm Examination February 12, 2014

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

6.170 Lecture 7 Abstract Data Types MIT EECS

1B1a Arrays. Arrays. Indexing. Naming arrays. Why? Using indexing. 1B1a Lecture Slides. Copyright 2003, Graham Roberts 1

Exam 2 CSCI 2600 Principles of Software November 3, 2015

Assignment 4. Aggregate Objects, Command-Line Arguments, ArrayLists. COMP-202B, Winter 2011, All Sections. Due: Tuesday, March 22, 2011 (13:00)

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

CSE 331 Final Exam 6/5/17 Sample Solution

CSE 331 Winter 2016 Midterm Solution

Java GUI Test #1 Solutions 7/10/2015

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

Java Object Oriented Design. CSC207 Fall 2014

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

CSE 131 Introduction to Computer Science Fall Final Exam

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

CSE 331 Midterm Exam 2/13/12

CSE 331 Final Exam 12/9/13

Spring 2018 June 20 th, 2018 Extra Practice Problems. Short Problems

Why OO programming? want but aren t. Ø What are its components?

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

Announcements. Testing. Announcements. Announcements

QUEEN MARY, UNIVERSITY OF LONDON DCS128 ALGORITHMS AND DATA STRUCTURES Class Test Monday 27 th March

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

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

n HW5 out, due Tuesday October 30 th n Part 1: Questions on material we ll cover today n Part 2: BFS using your graph from HW4

Another interface: Comparable

ASSIGNMENT 2. COMP-202A, Fall 2011, All Sections. Due: Monday, October 17th, 2011 (23:30)

CSE 331 Final Exam 3/16/15 Sample Solution

For this section, we will implement a class with only non-static features, that represents a rectangle

First Name: AITI 2004: Exam 2 July 19, 2004

CSE 331 Software Design & Implementation

CPSC 217 Assignment 3

Exam 1 CSCI 2600 Principles of Software October 6, 2015

Thread Safety. Review. Today o Confinement o Threadsafe datatypes Required reading. Concurrency Wrapper Collections

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

Subtypes and Subclasses

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism

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

CMPSCI 187 / Spring 2015 Hangman

CSE 331 Final Exam 6/10/14

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall Quiz II

Introduction to Algorithms November 17, 2011 Massachusetts Institute of Technology Fall 2011 Professors Erik Demaine and Srini Devadas Quiz 2

CSE 331 Software Design & Implementation

CSE 331 Midterm Exam 11/9/15 Sample Solution

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

CS/ENGRD 2110 SPRING 2018

CS 455 Final Exam Spring 2018 [Bono] May 8, 2018

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.

This homework has an opportunity for substantial extra credit, which is described at the end of this document.

CS/ENGRD 2110 SPRING Lecture 4: The class hierarchy; static components

Expected properties of equality

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

Be sure check the official clarification thread for corrections or updates to this document or to the distributed code.

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

Final Examination CS 125 Introduction to Computer Science Fall Hours

Topic 7: Algebraic Data Types

WELCOME! (download slides and.py files and follow along!) LECTURE 1

You have three hours to complete this exam. There are 50 total points.

Lesson 6A Loops. By John B. Owen All rights reserved 2011, revised 2014

Navigate Your Kindle (2nd Generation)

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

C12a: The Object Superclass and Selected Methods

CSE 131 Introduction to Computer Science Fall Exam II

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

Practice midterm exam

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

Transcription:

MIT 6.005: Software Construction Max Goldman revised Sunday 13 th March, 2016, 15:30 Quiz 1 (March 14, 2016) Your name: Your Athena username: You have 50 minutes to complete this quiz. It contains 12 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 name 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! DO NOT WRITE BELOW THIS LINE Problem Points Grade Grader 1: Multiple Choice 20 2: Specifications 20 3: AFs & RIs 20 4: Testing 20 5: Rep Exposure 20 Total 100 This course makes use of Athena, MIT's UNIX-based computing environment. OCW does not provide access to this environment.

2 Your name: Quiz 1 (March 14, 2016) Problem 1 (Multiple Choice) (20 points). (a) Which of the following must be true of an underdetermined function specification? (choose all that apply) A. An underdetermined spec means the implementation is unwritten. B. An underdetermined spec means the implementation is nondeterministic. C. An underdetermined spec allows multiple valid outputs for some input. D. An underdetermined spec allows multiple valid inputs that give some output. (b) After the following code is executed, what is the value of array arr? (choose one answer) final String[] arr = new String[2]; String s = "6.005"; arr[0] = s; s = "is"; arr[1] = s; String t = arr[0]; t = "fun"; A. [ "is", "is" ] B. [ "6.005", "is" ] C. [ "fun", "is" ] D. none of the above (c) The line of Java code String t = arr[0]; involves... (choose all that apply) A. assignment B. equivalence relation C. mutation D. static typing

Quiz 1 (March 14, 2016) Your name: 3 (d) Alyssa P. Hacker is designing an immutable type to represent users in her computer system. The User s login name is stored as: private final String kerberos; She defines two User objects as equal if their login names are the same, ignoring case: @Override public boolean equals(object other) { if (!(other instanceof User)) { return false; User that = (User)other; return this.kerberos.equalsignorecase(that.kerberos); @Override public int hashcode() { /* TODO Which of the following implementations of hashcode() would be valid, satisfying the Object contract? (choose all that apply) A. return 31; B. return this.kerberos.hashcode(); C. return this.kerberos.tolowercase().hashcode(); D. return this.kerberos.touppercase().hashcode(); (e) If the code in answer choice (A) above appeared in your 6.005 code review assignment, which of the following comments would be appropriate criticisms? (choose all that apply) A. The code isn t DRY. B. The code uses magic numbers. C. The code exposes User s representation. D. The code is unnecessary, we don t need to override hashcode if we only return a constant.

4 Your name: Quiz 1 (March 14, 2016) Problem 2 (Specifications) (20 points). A tetromino is a shape made out of four adjacent squares. These shapes are most famous from the game Tetris where the player must rotate and translate falling tetrominoes in order to fit them together. There are seven possible tetrominoes that lie on the 2D plane, and each is identified by a letter it looks like: I O T J L S Z Define a tetromino shape letter as one of the seven letters I O T J L S Z, either upper- or lowercase. Tetrominoes may be rotated by 0, 90, 180, or 270 degrees. A diagram of all seven tetrominoes in all four orientations is on the last page of this quiz. Let s define an abstract data type to represent tetrominoes: public class Tetromino { // determine if a character is a valid tetromino shape letter public static boolean isvalidshape(char shape) {... // make a new tetromino public Tetromino(char shapeletter, int rotationdegrees) {... // rotate this tetromino public void rotateclockwise() {... // get the shape of this tetromino public char shape() {... (a) Fill in this table with information about the operations of Tetromino. The answers for isvalidshape are already given. operation type signature classify the type of ADT operation Java implementation strategy isvalidshape char boolean not applicable static method Tetromino rotateclockwise shape

Quiz 1 (March 14, 2016) Your name: 5 (b) Consider these different specifications for the isvalidshape function: public static boolean isvalidshape(char shape) Spec A Spec B * @param shape any character * @return true iff shape is a lowercase tetromino shape letter * @param shape an English alphabetic character * @return true iff shape is a lowercase tetromino shape letter Spec C * @param shape a lowercase tetromino shape letter * @return true Compare these specifications. For each pair below, circle one correct option and write write a brief explanation to complete the sentence. A vs. B: Spec A is B vs. C: Spec B is equivalent to weaker than stronger than incomparable to equivalent to weaker than stronger than incomparable to spec B because spec C because

6 Your name: Quiz 1 (March 14, 2016) Problem 3 (AFs & RIs) (20 points). Here is an implementation of the Tetromino ADT. A rotatable tetromino. public class Tetromino { //... static method isvalidshape... private final char shape; private int rotation; // Abstraction function // TODO // Representation invariant // TODO // Safety from rep exposure // TODO * Make a new Tetromino with the given shape and rotation. * @param shapeletter uppercase tetromino shape letter * @param rotationdegrees clockwise rotation in degrees, * must be 0, 90, 180, or 270 public Tetromino(char shapeletter, int rotationdegrees) { this.shape = shapeletter; this.rotation = rotationdegrees / 90; * TODO public void rotateclockwise() { rotation = (rotation + 1) % 4; * @return shape of this tetromino: I O T J L S or Z public char shape() { return shape; @Override public String tostring() { return shape + "-shape@" + rotation * 90 + "deg";

Quiz 1 (March 14, 2016) Your name: 7 (a) For each of the statements below, say whether it should be included in the internal documentation of Tetromino by writing: AF if the statement belongs in the abstraction function RI... the rep invariant EXP... the argument that type has no rep exposure NONE if it should not be included in any of those You should include in the AF, RI, or EXP all good statements that are compatible with the code and specs on the previous page. Do not include statements that are not compatible with the code and specs. shape is private and an immutable value rotation is private and an immutable value this Tetromino is never returned to clients shape is an uppercase tetromino shape letter shapeletter is an uppercase tetromino shape letter the tetromino has the shape given by shape rotation = 0 the tetromino is rotated clockwise by rotation the tetromino is rotated clockwise rotation times 90 degrees 0 <= rotation < 4 0 <= rotation < 360 rotation is one of { 0, 90, 180, 270

8 Your name: Quiz 1 (March 14, 2016) Consider these different specifications for the rotateclockwise method: public void rotateclockwise() For each possible specification below, write a one-sentence code review comment that identifies the most serious problem with the spec. (b) * Update this tetromino s rotation number to add 1, mod 4. Comment: (c) * @return this tetromino rotated by 90 degrees clockwise Comment: (d) * Rotate this tetromino (does not otherwise modify the tetromino). Comment:

Quiz 1 (March 14, 2016) Your name: 9 Problem 4 (Testing) (20 points). Let s consider the problem of packing tetrominoes into a rectangle by rotating and translating them in the 2D plane. For example, here are tetrominoes I, L, & Z packed into a 5 4 rectangle, and a 7 2 rectangle: 5 7 4 2 For those three tetrominoes, the minimum area they can be packed into is 7 2 = 14 squares. (a) * @param shapes string of tetromino shape letters * @return a 2-element list representing a minimum-area rectangle into which * the tetrominoes given by shapes can be packed * (for example, pack("ilz") might return the list [ 2, 7 ]) public static List<Integer> pack(string shapes) {... Start writing a black box testing strategy for pack(..) by giving one good partitioning for input shapes: For each of the test cases below, in the first box write YES or NO in the first box to say whether the test valid or not. If the test is not valid, write a one-sentence reason why not. We ll use Python s syntax to represent lists for brevity. (b) shapes = "X" rectangle = [ 0, 0 ] Valid? Reason if invalid: (c) shapes = "I" rectangle = [ 1, 4 ] Valid? Reason if invalid: (d) shapes = "LO" rectangle = [ 3, 3 ] Valid? Reason if invalid:

10 Your name: Quiz 1 (March 14, 2016) Problem 5 (Rep Exposure) (20 points). Let s define a mutable abstract data type TetrominoGrid to represent tetrominoes arranged on a fixed-size grid, where every tetromino fits on the grid and none of the tetrominoes overlap. For example: 0 1 2 3 0 1 2 3 4 On the next page is an implementation of TetrominoGrid. In its rep, TetrominoGrid stores the location of each tetromino using a map from integers to tetrominoes. The integer keys are square numbers: Each square in the grid is numbered starting from 0 in the upper-left corner. On a grid of width width, square at row row and column col is numbered row width + col. For example, here s how our type would represent the example above: 0 1 2 3 0 1 2 3 4 0 3 6 width = 5, height = 4 tetrominolocations = { 0: (Tetromino: I-shape rotated 0 degrees), 3: (Tetromino: L-shape rotated 0 degrees), 6: (Tetromino: Z-shape rotated 90 degrees) The TetrominoGrid ADT is implemented using the same mutable Tetromino ADT from previous questions. (a) Identify all instances of rep exposure in TetrominoGrid on the next page. For each one, write: 1. the line number most directly responsible for the problem, 2. at most one-sentence description of the rep exposure, and 3. at most one-sentence description of how to fix the problem, or a single corrected line of code. There may be more boxes than you need. Line #: Explanation: Line #: Explanation: Line #: Explanation: Line #: Explanation: Line #: Explanation:

Quiz 1 (March 14, 2016) Your name: 11 You may detach this page from your quiz, but you must write your name above and turn in all pages. * Mutable type representing a fixed-size grid with a valid arrangement of * tetrominoes: every tetromino fits on the grid without overlapping. public class TetrominoGrid { 1) private final int width; 2) private final int height; 3) public final Map<Integer, Tetromino> tetrominolocations; // create a new grid and try to add some tetrominoes public TetrominoGrid(int width, int height, List<Tetromino> initial) { 4) this.width = width; 5) this.height = height; 6) this.tetrominolocations = new HashMap<>(); for (Tetromino tetromino : initial) { 7) this.add(tetromino.shape()); // get the tetromino whose upper-left corner is the given square public Tetromino gettetrimino(int row, int col) { 8) return tetrominolocations.get(row * width + col); // return a list of all tetriminos currenty on the grid public List<Tetromino> gettetriminosonboard() { 9) List<Tetromino> tetrominoes = new ArrayList<>(); for (Tetromino tetromino : tetrominolocations.values()) { 10) tetrominoes.add(tetromino); 11) return Collections.unmodifiableList(tetrominoes); // try to add a tetromino to the grid, if it can fit without overlap public boolean add(char shape) { 12) boolean canbeplaced = false; // can we fit the tetromino anywhere? 13) int topleft = -1; 14) int rotation = 0; //... code to check whether the new tetromino fits and doesn t overlap... //... updates the values of the local variables accordingly... if (canbeplaced) { 15) tetrominolocations.put(topleft, new Tetromino(shape, rotation)); 16) return canbeplaced; //... other operations...

12 Your name: Quiz 1 (March 14, 2016) You may detach this page from your quiz, but you must write your name above and turn in all pages. 0 degrees 90 degrees 180 degrees 270 degrees I O T J L S Z

MIT OpenCourseWare https://ocw.mit.edu 6.005 Software Construction Spring 2016 For information about citing these materials or our Terms of Use, visit: https://ocw.mit.edu/terms.