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

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

Exercise 3 Subtyping and Behavioral Subtyping October 13, 2017

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

Practice for Chapter 11

INSTRUCTIONS TO CANDIDATES

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

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

Exercise 7 Bytecode Verification self-study exercise sheet

Exercise 13 Self-Study Exercise Sheet

Universe Type System for Eiffel. Annetta Schaad

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Programming Languages and Techniques (CIS120)

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

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

CLASS DESIGN. Objectives MODULE 4

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

Static Program Analysis

Midterm Exam CS 251, Intermediate Programming March 12, 2014

Chapter 11 Classes Continued

Compilation 2012 Static Type Checking

The Liskov Substitution Principle

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

Exercise 6 Multiple Inheritance, Multiple Dispatch and Linearization November 3, 2017

Name: CSC143 Exam 1 1 CSC 143. Exam 1. Write also your name in the appropriate box of the scantron

Exercise 6 Multiple Inheritance, Multiple Dispatch and Linearization November 3, 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.

CS2110 Fall 2011 Lecture 25. Under the Hood: The Java Virtual Machine, Part II

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

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

Exam Duration: 2hrs and 30min Software Design

Object Oriented Issues in VDM++

Midterm Exam CS 251, Intermediate Programming March 6, 2015

Csci 102: Sample Exam

CSE 331 Midterm Exam Sample Solution 2/18/15

Exercise 13 Self-Study Exercise Sheet

Questions Answer Key Questions Answer Key Questions Answer Key

Programming II (CS300)

Java TM Introduction. Renaud Florquin Isabelle Leclercq. FloConsult SPRL.

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

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

Midterm Exam CS 251, Intermediate Programming October 8, 2014

EXAMINATIONS 2009 MID-TERM TEST. COMP 202 / SWEN 202 Formal Methods of Computer Science / Formal Foundations of Software Engineering WITH ANSWERS

More On inheritance. What you can do in subclass regarding methods:

Programming II (CS300)

CS100J, Fall 2003 Preparing for Prelim 1: Monday, 29 Sept., 7:30 9:00PM

Static program checking and verification

Type Hierarchy. Lecture 6: OOP, autumn 2003

Exercise 6 Multiple Inheritance, Multiple Dispatch and Linearization November 4, 2016

Programming Exercise 14: Inheritance and Polymorphism

14. Exception Handling

Exercise 8 Parametric polymorphism November 18, 2016

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

Assertions, pre/postconditions

Inheritance and Substitution (Budd chapter 8, 10)

CSE wi Midterm Exam 2/8/18 Sample Solution

Declarations and Access Control SCJP tips

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

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

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

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

Examination Questions Midterm 1

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

CS-202 Introduction to Object Oriented Programming

I. True/False: (2 points each)

COMP 401 Spring 2013 Midterm 2

Making New instances of Classes

COMP 401 Spring 2014 Midterm 1

Today. Instance Method Dispatch. Instance Method Dispatch. Instance Method Dispatch 11/29/11. today. last time

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

CSE 331 Final Exam 3/12/12

A New Formalization of Subtyping to Match Subclasses to Subtypes

Java Class Loading and Bytecode Verification

Operators and Expressions

COMP 401 Spring 2013 Midterm 2

CIS 120 Midterm II November 16, 2012 SOLUTIONS

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.

AP CS Unit 6: Inheritance Notes

Conversions and Overloading : Overloading

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

Midterm Exam (REGULAR SECTION)

Java and C# in depth

Concepts of Object-Oriented Programming Peter Müller

Java: framework overview and in-the-small features

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

CSE 331 Final Exam 3/16/15 Sample Solution

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm 1 CSC148H1F L0201 (Liu)

Concepts of Object-Oriented Programming Peter Müller

Part 1 (80 points) Multiple Choice Questions (20 questions * 4 points per question = 80 points)

Introduction to Programming Using Java (98-388)

Domain-Driven Design Activity

Programming Languages and Techniques (CIS120)

Exception Handling: Control. Exception handling is the control of error conditions or other unusual events during the execution of a program.

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

Assertions. Assertions - Example

Overriding Variables: Shadowing

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

Inheritance -- Introduction

Transcription:

Concepts of Object-Oriented Programming AS 2017 Concepts of Object-Oriented Programming Midterm Examination 10.11.2017 Prof. Dr. Peter Müller Last name:................................. First name:................................. Student ID number:........................................................................ Department (if not D-INFK):............................................................... I confirm with my signature, that I was able to take this exam under regular circumstances and that I have read and understood the directions below. Signature:.................................................................................. Read completely and carefully the following instructions before starting to work on the exam: 1. Write your last and first name on every page that contains parts of your s. Use a ballpoint pen or a fountain pen (no pencil). Do not use a red pen. Return the instructions, the tasks, and your s. 2. It is neither allowed to use your own papers, documents, scripts, etc., nor any electronic equipment (notebook computers, calculators, cell phones, etc.) 3. Write all of your s in English. 4. Explain your s carefully if a task asks for an explanation. 5. You have 1 hour to complete the exam. SOLUTION 6. Place your student ID on the desk. Task 1 2 3 4 Total Max. points 7 14 8 8 37 Achieved Good Luck!

COOP Midterm Exam AS 2017 Name: 2/9 Task 1 Multiple Choice - 7 points In all of the following questions exactly one answer is correct. To get points, you need to mark only the correct answer. In all other cases you will get no points. A (4 points) Subtyping Consider the following types written in Java-like syntax: class D { A a; B foo(a a) throws RuntimeException; class F { C a; A foo(b a) throws RuntimeException; class G { A a; A foo(c a) throws Exception; class H { A a; A foo(a a) throws IndexOutOfBoundsException; Assume that B <: A, C <: A, and IndexOutOfBoundsException <: RuntimeException <: Exception. Which of the following subtype relations could be allowed by a sound type system? (a) D <: H and H <: G (b) D <: G and H <: F (c) CORRECT: D <: G and H <: G (d) H <: D and H <: F (e) None of the above Answer: a b c d e

COOP Midterm Exam AS 2017 Name: 3/9 B (3 points) Bytecode Verification Assume two Java classes A and B, and assume that B is a direct subclass of A. Consider the following Java bytecode: 0: iload 1 1: aload 2 2: astore 0 3: istore 2 4: aload 0 5: astore 2 6: goto 0 and assume that the input to instruction 0 is ([],[A,int,B]), where the first list indicates the content of the stack and the second list indicates the contents of the registers. The maximal stack size is equal to 2. Which of the following statements is true? (a) CORRECT: The type checker can successfully verify the code without needing type annotations for other instructions. (b) The bytecode cannot be verified due to a type mismatch at instruction 0. (c) The bytecode cannot be verified because in instruction 3 we are storing an integer in an object register. (d) The bytecode cannot be verified due to a stack underflow. (e) None of the above. Answer: a b c d e

COOP Midterm Exam AS 2017 Name: 4/9 Task 2 Behavioral Subtyping - 14 points A modifies clause is a part of a method specification that declares which heap locations can be modified by the method. As an example, in the following Java code, method foo is not allowed to write to any heap location besides this.x and other.x. class X { public int x; public int y; /// modifies (this.x, other.x) void foo(x other) {... A modifies clause can be interpreted as an implicit postcondition that states that all heap locations that are not listed will have the same value after the method invocation as before. A (7 points) A.1 When overriding a method, should it be allowed to expand the modifies clause, i.e., to include more modifiable heap locations? If yes, just write yes, otherwise provide a code example that demonstrates the problem when overriding method foo with an expanded modifies clause. No. class Y extends X { /// modifies (this.x, other.x, this.y) void foo(x other) { y = 2; public void client() { X x = new Y(); x.y = 12; x.foo(x); assert x.y == 12; A.2 When overriding a method, should it be allowed to reduce the modifies clause, i.e., to include less modifiable heap locations? If yes, just write yes, otherwise provide a code example that demonstrates the problem when overriding method foo with a reduced modifies clause. Yes. B (7 points) Consider the following Java code. class Y { public int n; public int k; /// requires i >= 0 /// modifies () /// ensures result >= k

COOP Midterm Exam AS 2017 Name: 5/9 public int m1(int i) {... /// requires n == k /// modifies (this.k) /// ensures result > old(k) public int m3() {... interface I { /// requires y.n > 0 /// modifies (y.n) /// ensures result > 0 public int m2(y y); class Z extends Y implements I { /// requires i > 0 (i < k && k <= 1) /// modifies () /// ensures result == old(k) + i public int m1(int i) {... /// requires y.n > 0 /// modifies (y.n) /// ensures result == y.n * y.n public int m2(y y) {... /// requires n >= k /// modifies (this.k) /// ensures result == n + 1 public int m3() {... Assume that there is no specification inheritance. B.1 Does method Z.m1 definitely fulfill all applicable requirements of behavioral subtyping? If yes, just write yes, otherwise point out the requirement that is violated. No. The precondition of Z.m1 is not compatible with the precondition of Y.m1. B.2 Does method Z.m2 definitely fulfill all applicable requirements of behavioral subtyping? If yes, just write yes, otherwise point out the requirement that is violated. No, it is not compatible with the postcondition of I.m2 (since y.n can be modified). B.3 Does method Z.m3 definitely fulfill all applicable requirements of behavioral subtyping? If yes, just write yes, otherwise point out the requirement that is violated. Yes (since this.n is not modified).

COOP Midterm Exam AS 2017 Name: 6/9 Task 3 Linearization - 8 points In this task you will have to model a traditional bakery, which produces and sells different types of Products. These Products satisfy the following subtype relations: Cake <: Dessert and BakedProduct <: Product BakedProduct <: Dessert ( <: means is NOT a subtype of ) Panettone <: BakedProduct and Panettone <: Cake A (3 points) An incomplete Scala implementation of the bakery is given below: class Product { override def tostring = "product" class Dessert { override def tostring = "dessert" class Cake { override def tostring = "cake" class BakedProduct { override def tostring = "baked " + super.tostring class Panettone { Fill in the boxes, such that the code above reflects at least the aforementioned subtype relations and it type checks. You are allowed to introduce new subtype relations in your, but you are NOT allowed to define new types. B (5 points) Now consider that the bakery offers two additional BakedProducts, modelled in Scala through the following traits: trait Bread extends BakedProduct { override def tostring = super.tostring + " or bread" trait SeasonalProduct extends BakedProduct { override def tostring = super.tostring + " for the holiday season" Fill in the empty lines from Task A by mixing-in the traits, such that for a client who orders a Panettone with SeasonalProduct with Bread (as shown below), cake or bread for the holiday season will be printed on the bill. // Client code: val panettone = new Panettone with SeasonalProduct with Bread println(panettone.tostring) // prints: cake or bread for the holiday season All the subtype relations from Task A must still hold. If one of the lines should remain empty, please explicitly write "/*none*/". The final code should type check.

COOP Midterm Exam AS 2017 Name: 7/9 Solution 1: class Product { override def tostring = "product" class Dessert extends BakedProduct /*none*/ { override def tostring = "dessert" class Cake extends Dessert /*none*/ { override def tostring = "cake" class BakedProduct extends Product /*none*/ { override def tostring = "baked " + super.tostring class Panettone extends Cake with Bread { Solution 2: class Product { override def tostring = "product" class Dessert extends BakedProduct /*none*/ { override def tostring = "dessert" class Cake extends Dessert /*none*/ { override def tostring = "cake" class BakedProduct extends Product /*none*/ { override def tostring = "baked " + super.tostring class Panettone extends Cake with Bread with SeasonalProduct {

COOP Midterm Exam AS 2017 Name: 8/9 Task 4 Overloading and Overriding - 8 points Consider the following class in Java: public class Person { protected double salary; public Person(double salary) { this.salary = salary; public boolean havesameincome(person other) { return this.salary == other.getincome(); public double getincome() { return salary; Consider also the following subclass of Person, a person with a spouse, which takes the salary of the spouse into account as well: public class MarriedPerson extends Person { private double spousesalary; public MarriedPerson(double salary, double spousesalary) { this.salary = salary; this.spousesalary = spousesalary; public boolean havesameincome(marriedperson other) { return this.getincome() == other.getincome(); public double getincome() { return ((salary + spousesalary) / 2); A (4 points) Show an example with variables p1,p2, such that p1.havesameincome(p2) returns false, but p1.getincome() == p2.getincome() returns true. In other words, fill in the following blank with valid code, such that the assertion below the following box is valid. Do not use reflection and assume that Person has no other subclasses. Person p1; MarriedPerson p2; p1 = new MarriedPerson(a,b); p2 = new MarriedPerson(c,d); for any a, b, c, d such that a + b = c + d but a (c + d)/2. assert (!p1.havesameincome(p2) && p1.getincome() == p2.getincome());

COOP Midterm Exam AS 2017 Name: 9/9 B (4 points) Propose changes to Person and MarriedPerson such that the assertion above will fail. B.1 Can you change only MarriedPerson.haveSameIncome, such that the assertion above will fail for your to subtask A? If yes, provide the modified method. Otherwise, explain why this is not possible. Yes, the following works. public boolean havesameincome(person other) { // changed MarriedPerson to Person in signature return this.getincome() == other.getincome(); B.2 Can you change only Person.haveSameIncome, such that the assertion above will fail for your to subtask A? If yes, provide the modified method. Otherwise, explain why this is not possible. Yes, the following works. public boolean havesameincome(person other) { return this.getincome() == other.getincome(); // changed calls to salary to getincome here Another trivial would be: public boolean havesameincome(person other) { return true; Also possible: Type-check with instanceof, then cast both to MarriedPerson and call havesameincome on casted objects. Also possible: Change parameter type to MarriedPerson.