Ryerson University Department of Electrical & Computer Engineering COE618 Midterm Examination February 26, 2013 Name: Student # : Time: 90 minutes Instructions This exam contains 6 questions. Please check your copy to make sure that it contains 8 pages (including this page) before you start. (1) This is a closed-book exam. (2) If in doubt on any question, then you must clearly state your own assumption(s). (3) Answer all of the following questions in the space provided. For marking use only (do not fill) Questions Score Max 1 10 2 10 3 10 4 10 5 20 6 10 Total 70 1
1. (10 marks) Write TRUE or FALSE beside each of the following statements: a) A producer creates new objects of a data abstraction given objects of their type as input. b) An abstraction function must be one-to-one. c) An immutable class must declare all its instance variables to be final. d) An abstraction function captures the designer s intent in choosing a particular representation. d) A change to the concrete representation of a data abstraction always changes the abstract concept that is represented. 2. (10 marks) a) Name two relationships that can exist between two use cases. b) Which UML diagram describes the dynamic behavior of the system? c) Which UML diagram describes the static structure of the system? 2
d) Which UML diagram describes the functionality of the system from the user s point of view. 3. (10 marks) Consider the following code: public class Recursion { public static void main(string[] args) { recurse("world"); public static void recurse(string w) { int l = w.length(); if(l == 1) { System.out.println(w); return; System.out.println(w); String v = w.substring(0, l-1); recurse(v); System.out.println(w); What will be the output if we run the main method of the Recursion class. [Note: The substring(int beginindex, int endindex) method of the String class returns a new string that is a substring of this string. The substring begins at the specified beginindex and extends to the character at index endindex - 1. Thus the length of the substring is endindexbeginindex.] 3
4. (10 marks) Consider the following Java code. The classes E is not a checked exception. 1 public class Main { 2 public static void main(string[] args) { 3 int[] a = {2, 4, 3; 4 try { 5 6 7 if( i == 3 ) throw new E(); 8 int j = a[i]; 9 System.out.println( i + j ); 10 11 catch(e e) { 12 System.out.println("catch of E"); 13 14 catch(arrayindexoutofboundsexception e) { 15 System.out.println("catch of ArrayIndexOutOfBoundsException"); 16 17 finally { 18 System.out.println("inside finally"); 19 20 System.out.println("main ends"); 21 22 4
Indicate the output of this code for the following cases: (a) In Line 5, the statement is int i = 2; (b) In Line 5, the statement is int i = 3; c) In Line 5, the statement is int i = 4; 5
5. (20 marks) Consider the following implementation of the Account class: public class Account implements Cloneable { //OVERVIEW: Account represents a bank account. A typical bank //account has owner s name and balance. // Abstraction function is: // AF(c) = an account A // where A.name = c.name and // A.balance = sum (for all i) of c.transactions[i]; // Rep invariant is // c.name!= null and // sum (for all i) of c.transactions[i] >= 0 private String name; private int[] transactions; public Account(String n, int[] trans) { this.name = n; this.transactions = trans; repok(); public boolean repok() { // EFFECTS: Returns true if the rep invariant holds // for this; otherwise returns false // a) Write your code here 6
public Account clone() throws CloneNotSupportedException { // b) Write your code here a) Fill in the body of the repok() method. b) Override the clone() method so that it returns a deep copy (and not a shallow copy). c) Does Account class expose its rep? Explain. 7
6. (10 marks) Consider the following class definition: public class Item { private int id; private Item next; public Item(int i, Item n) { id = i; next = n; public void setnext(item n) { next = n; public Item getnext() { return next; Write code segments that results in the reference diagram shown below: (squares are Item objects with its two instance variables; a, b and c are Item references) a 5 b 9 c 2 1 8