CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

Similar documents
CMSC 433 Spring 2013 Exam 1

CS 520 Theory and Practice of Software Engineering Fall 2018

CMSC 433 Programming Language Technologies and Paradigms. Sharing Objects

CS 520 Theory and Practice of Software Engineering Fall 2017

CS 520 Theory and Practice of Software Engineering Fall 2017

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.

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

Midterm Exam CS 251, Intermediate Programming March 12, 2014

Assignment 2 - Specifications and Modeling

Midterm Exam CS 251, Intermediate Programming October 8, 2014

CSE 331 Midterm Exam 2/13/12

CMSC 433 Programming Language Technologies and Paradigms. Spring 2013

Final exam. CS 2110, December 15, 2016, 9:00AM

Design of Thread-Safe Classes

Midterm #1. CMSC 433: Programming Language Technologies and Paradigms. October 14, 2013

EXAMINATIONS 2012 END-OF-YEAR SWEN222. Software Design. Question Topic Marks 1. Design Quality Design Patterns Design by Contract 12

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

W09 CPSC 219 Midterm Exam Page 1 of 7

Midterm Exam 2 CS 455, Spring 2011

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

Final Exam CS 251, Intermediate Programming December 13, 2017

CSE wi Midterm Exam 2/8/18 Sample Solution

CS170 Introduction to Computer Science Midterm 2

The New Java Technology Memory Model

CS 455 Midterm Exam 2 Fall 2015 [Bono] Nov. 10, 2015

Total Score /1 /20 /41 /15 /23 Grader

Exam 1 CSCI 2600 Principles of Software October 6, 2015

Midterm Exam 2 CS 455, Spring 2015

Fortgeschrittene objektorientierte Programmierung (Advanced Object-Oriented Programming)

More Shared Memory Programming

CS 201, Fall 2016 Sep 28th Exam 1

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

Assertions, pre/postconditions

CSE331 Winter 2014, Midterm Examination February 12, 2014

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

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

The Java Memory Model

Learning from Bad Examples. CSCI 5828: Foundations of Software Engineering Lecture 25 11/18/2014

Midterm on next week Tuesday May 4. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 9

CSE 332 Winter 2018 Final Exam (closed book, closed notes, no calculators)

Lecture 17: Sharing Objects in Java

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

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

Sharing Objects Ch. 3

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

Exam 1 CSCI 2600 Principles of Software October 6, 2015

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

Outline. iterator review iterator implementation the Java foreach statement testing

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

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.

Charlie Garrod Bogdan Vasilescu

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

Midterm Exam 2 CS 455, Fall 2013

Practice Midterm 1. Problem Points Score TOTAL 50

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

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

Array Based Lists. Collections

Java Platform Concurrency Gotchas

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

Question Points Score Total 100

Csci 102: Sample Exam

Midterm Exam. CS169 Fall November 17, 2009 LOGIN: NAME: SID: Problem Max Points Points TOTAL 100

public class Q1 { public int x; public static void main(string[] args) { Q1 a = new Q1(17); Q1 b = new Q1(39); public Q1(int x) { this.

CSE 142 Wi04 Midterm 2 Sample Solution Page 1 of 8

Good Luck! CSC207, Fall 2012: Quiz 1 Duration 25 minutes Aids allowed: none. Student Number:

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

ECE 461 Fall 2011, Final Exam

CSE 332 Winter 2015: Final Exam (closed book, closed notes, no calculators)

Solutions to Quiz 1 (March 14, 2016)

CS 455 Final Exam Fall 2012 [Bono] Dec. 17, 2012

EINDHOVEN UNIVERSITY OF TECHNOLOGY

CSE 1223: Exam II Autumn 2016

UMBC CMSC 331 Final Exam

Final Exam CS 251, Intermediate Programming December 10, 2014

CS 455 Midterm Exam 1 Fall 2015 [Bono] Thursday, Oct. 1, 2015

Last name:... First name:... Department (if not D-INFK):...

Introduction to Locks. Intrinsic Locks

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

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

hwu-logo.png 1 class Rational { 2 int numerator ; int denominator ; 4 public Rational ( int numerator, int denominator ) {

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

Thread-Local. Lecture 27: Concurrency 3. Dealing with the Rest. Immutable. Whenever possible, don t share resources

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

Programming Languages and Techniques (CIS120)

Code Contracts in C#

Question Points Score Total 100

Sharing Objects. Java and Android Concurrency.

CS/ENGRD2110: Final Exam

Implementing a List in Java. CSE 143 Java. Just an Illusion? List Interface (review) Using an Array to Implement a List.

Audience. Revising the Java Thread/Memory Model. Java Thread Specification. Revising the Thread Spec. Proposed Changes. When s the JSR?

CSE 142 Wi03 Midterm 2 Sample Solution All Versions Page 1 of 6

ext Total Score /20 /20 /15 /20 /25 /5 Grader

CS18000: Programming I

CSCI 201L Written Exam #2. 10% of course grade

Practice exam for CMSC131-04, Fall 2017

Points off Total off Net Score. CS 314 Final Exam Spring 2016

Josh Bloch Charlie Garrod Darya Melicher

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

Problems with Concurrency. February 19, 2014

Total Score /15 /20 /30 /10 /5 /20 Grader

Transcription:

Name: CMSC 433 Section 0101 Fall 2012 Midterm Exam #1 Directions: Test is closed book, closed notes. Answer every question; write solutions in spaces provided. Use backs of pages for scratch work. Good luck! Please do not write below this line. 1. 2. 3. 4. 5. SCORE

1. (20 points) Answer each of questions in 1-2 sentences (a) (5 points) What is the difference between a data race and a race condition? (b) (5 points) What is the difference between a user thread and a daemon thread in Java? (c) (5 points) What does it mean for a (correct) class to be thread-safe? (d) (5 points) Why is it a bad idea to publish this in the constructor for a class? 1

2. (20 points) LOCKING, DEADLOCK (a) (5 points) Explain what it means for locks to be reentrant. (b) (10 points) Suppose three objects A, B and C have been declared, and consider the following threads. T1 T2 T3 synchronized(a){ synchronized(b){... synchronized(b){ synchronized(c) {... synchronized(c){ synchronized(a){... Show that this system can deadlock by drawing an appropriate waits-for graph that can arise during an execution of the system. 2

(c) (5 points) How can the system in the previous part of this problem be fixed so that deadlock is impossible? 3

3. (20 points) JAVA MEMORY MODEL, HAPPENS-BEFORE (a) (6 points) Explain how the Java Memory Model treats volatile variables. (b) (7 points) Give the program-order event sequence for the following program. public class Simple { public static void main (String[] args) { int x; int y = 1; x = 1; y += x; 4

(c) (7 points) Consider the following (partial) event sequence for a program. T 0, write, x, 0 T 1, write, y, 1 T 0, unlock, locka T 1, read, x, 0 T 1, lock, locka Is there a data race in this program? Explain. (Hint: use happens-before in your explanation.) 5

4. (20 points) VISIBILITY, PUBLISHING (a) (6 points) Explain the difference between publishing an object and letting an object escape. (b) (7 points) Consider the following class definition. public class Point { private double x; private double y; Point (double x, double y) { this.x = x; this.y = y; double getx() = { return x; double gety() = { return y; Are the objects in this class immutable? Explain. 6

(c) (7 points) Consider the following class, which was studied in lecture. public class Holder { private int n; Holder (int n) { this.n = n; public void assertsanity () { if (n!= n) throw new AssertionError ("BAD CONSTRUCTION!"); Suppose we now publish an object in this class using the following. public volatile Holder h = new Holder(42); Can h.assertsanity() ever throw an AssertionError? Explain. 7

5. (20 points) CODING Many compilers use a so-called string table to store the variable names that a programmer uses in her / his program. The idea is to replace (expensive) string comparisons in the compiler with (efficient) integer comparisons. The table may be thought of as an array. A compiler, upon seeing a variable name, would look the string up in the table and use the array index for that variable name in place of the actual string of characters. Complete the following thread-safe implementation of a class StringTable by providing implementations of two public methods. int lookup (String s): returns the position of s, adding it into the table if necessary String getstring (int i): returns the string stored at position i, or null if i is not a valid position in the table. Here are some useful methods for ArrayList<E> that you may use. boolean add(e e): adds e to the end of the list (boolean return value may be ignored) E get (int index): returns the element at the specified position in the list int indexof (Object o): returns the index of the first occurrence of o in the list, or -1 if o is not in the list int size (): returns the number of elements in the list When comparing strings be sure to use the boolean equals(string s) method, not ==! 8

public class StringTable { // Invariants: // 1. Any string appears at most once in the table. // 2. Once a string is added to the table, its index never changes. private final ArrayList<String> table; public StringTable () { table = new ArrayList<String> (); // Spec for int lookup (String s) // Precondition: none // Postcondition: returns index of s in table, adding s to end if necessary // Exception: none // Spec for String getstring (int i) // Precondition: none // Postcondition: return the string at position i, or null if i is not a position // end of StringTable 9