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.

Similar documents
No Aids Allowed. Do not turn this page until you have received the signal to start.

No Aids Allowed. Do not turn this page until you have received the signal to start.

Please note that if you write the mid term in pencil, you will not be allowed to submit a remark request.

Introduction to Generics in Java 5

W09 CPSC 219 Midterm Exam Page 1 of 7

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

CSC 207H Fall L Java Quiz Duration 25 minutes Aids allowed: none

CSCD 326 Data Structures I Queues

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

CSE 143 SAMPLE MIDTERM

Midterm Exam 2 CS 455, Spring 2015

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

Please note that if you write the mid term in pencil, you will not be allowed to submit a remark request.

The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT

Test methods Testing exceptions Common known states Slides by Mark Hancock (adapted from notes by Craig Schock)

JUnit Summary. Unit Test 3/18/2009. Test methods Testing exceptions Common known states

JUnit Summary. Test methods Testing exceptions Common known states Slides by Mark Hancock (adapted from notes by Craig Schock)

INSTRUCTIONS TO CANDIDATES

Interfaces & Generics

Chapter 2. Stack & Queues. M hiwa ahmad aziz

Midterm Exam 2 CS 455, Spring 2011

CS 113 MIDTERM EXAM 2 SPRING 2013

About this exam review

Largest Online Community of VU Students

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

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

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Principles of Software Construction: Objects, Design, and Concurrency. Objects (continued) toad. Spring J Aldrich and W Scherlis

CSE 401 Final Exam. December 16, 2010

CS171 Midterm Exam. October 29, Name:

Array Based Lists. Collections

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

Programming Languages and Techniques (CIS120)

Adam Blank Lecture 3 Autumn 2016 CSE 143. Computer Programming II

CS 2630 Computer Organization. Meeting 10/11: data structures in MIPS Brandon Myers University of Iowa

CSE 131 Introduction to Computer Science Fall Exam II

Student Number: Legibly write your name and student number on this page. Legibly write your name on the back page of this exam.

CSE 143. More ArrayIntList; pre/post; exceptions; debugging. Computer Programming II. CSE 143: Computer Programming II

FINAL EXAMINATION. COMP-250: Introduction to Computer Science - Fall 2011

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

CT 229 Object-Oriented Programming Continued

CE204 Data Structures and Algorithms Part 2

CS 61B Discussion 5: Inheritance II Fall 2014

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

CMPSCI 187 Midterm 1 (Feb 17, 2016)

Midterm Exam CS 251, Intermediate Programming October 8, 2014

Fundamentals of Object Oriented Programming

CS 231 Data Structures and Algorithms, Fall 2016

Why Design by Contract! CS 619 Introduction to OO Design and Development. Design by Contract. Fall 2012

Midterm Exam CS 251, Intermediate Programming March 12, 2014

CS61BL Summer 2013 Midterm 2

Introduction to Computer Science II CS S-20 Linked Lists III

Midterm Exam (REGULAR SECTION)

CS 101 Spring 2006 Final Exam Name: ID:

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

BBM 201 DATA STRUCTURES

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING E&CE 250 ALGORITHMS AND DATA STRUCTURES

Practice exam for CMSC131-04, Fall 2017

CSE 331 Midterm Exam 2/13/12

Array. Prepared By - Rifat Shahriyar

COMP 213. Advanced Object-oriented Programming. Lecture 17. Exceptions

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

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

CIS 110 Introduction to Computer Programming Summer 2017 Final. Recitation # (e.g., 201):

Examination Questions Midterm 1

CS 314 Final Fall 2011

Java and C# in depth

CS 305j Midterm 2 Fall 2006

CS170 Introduction to Computer Science Midterm 2

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

CS 520 Theory and Practice of Software Engineering Fall 2018

Soda Machine Laboratory

EECS2030 Week 7 worksheet Tue Feb 28, 2017

Chapter 6 Introduction to Defining Classes

Programming Languages and Techniques (CIS120)

SYSC 2006 CD. Come to the PASS workshop with your mock exam complete. During the workshop you can work with other students to review your work.

You must include this cover sheet. Either type up the assignment using theory3.tex, or print out this PDF.

CS 61B (Clancy, Yelick) Solutions and grading standards for exam 2 Spring 2001 Exam information

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws

CSE 143 SAMPLE MIDTERM SOLUTION

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

CS 520 Theory and Practice of Software Engineering Fall 2017

Final. Please write (printing clearly) the infonnation requested in the boxes at the top of this page.

CS 116 Week 8 Page 1

Inheritance and Polymorphism

CS 216 Exam 1 Fall SOLUTION

Programming Languages and Techniques (CIS120)

Administrivia. Java Review. Objects and Variables. Demo. Example. Example: Assignments

CS 520 Theory and Practice of Software Engineering Fall 2017

Programming - 2. Common Errors

Class, Variable, Constructor, Object, Method Questions

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

Computer Science 62. Midterm Examination

Come to the PASS workshop with your mock exam complete. During the workshop you can work with other students to review your work.

Chapter 8. Exception Handling. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Data Abstraction and Specification of ADTs

SOLUTIONS (INCOMPLETE, UNCHECKED)

CS365 Midterm -- Spring 2019

Building Java Programs

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

Transcription:

CSC 148H Midterm Fall 2005 St. George Campus Duration 50 minutes Student Number: Family Name: Given Name: 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. #1: /10 Bonus question: if you legibly write your student ID at the top of each odd numbered page you will get an extra mark. #2: /10 #3: /10 BONUS: / 1 TOTAL: /30 Good Luck! PLEASE HAND IN Total pages = 7 Page 1 over...

Question 1. [10 marks] public class ArrayQueue implements Queue { public int size; public Object[] contents; public ArrayQueue(int n) { contents = new Object[n]; /** Precondition: * The queue cannot be full. */ public void enqueue(object o) { contents[size++] = o; /** Precondition: * The queue cannot be empty. */ public Object dequeue() { Object head = contents[0]; for (int i = 0; i < size - 1; ++i) { contents[i] = contents[i + 1]; --size; return head; /** Precondition: * The queue cannot be empty. */ public Object head() { return contents[0]; public int size() { return size; public int capacity() { return contents.length; // End of ArrayQueue. The ArrayQueue class definition to the left is same as the one we discussed in class, except the instance variables size and contents are public. (While it is is not a good idea in general to set these instance variables to have public accessibility, it is useful for the purposes of this exam.) Assume the interface Queue includes the methods headers (other than the constructor) of all the methods in ArrayQueue. Note that the first element in the queue (if any) is always stored at index 0 of the contents array, and the last element (if any) is at index size-1. Write the Java code for a subclass of ArrayQueue, called LeakyQ, which has the following properties. It should have a constructor which takes a single integer parameter which specifies the (constant) capacity of the queue to be constructed. It should also have a remove method which takes a single integer parameter providing the index of a queue element to be removed. The remove method should return the Object that is removed from the queue. Any items which come after the removed item in the queue should be moved one space towards the front of the queue. If the parameter for remove is not between 0 and size-1, then the method should throw a java.lang.indexoutofboundsexception, which is a RuntimeException. Your LeakyQ should not declare any instance variables. Moreover, it should only contain one constructor and one method, as described above. An example of the use of this new class is as follows: LeakyQ q = new LeakyQ(10); q.enqueue("a"); q.enqueue("b"); q.enqueue("c"); q.remove(1); // returns "B" q.size(); // returns 2 q.head(); // returns "A" q.remove(1); // returns "C" q.size(); // returns 1 q.head(); // returns "A" Use the back of the last page for scratch work, and write your solution on the next page. Total pages = 7 Page 2 cont d...

Question 1. SOLUTION (continued) public class LeakyQ extends ArrayQueue { public LeakyQ(int cap) { super(cap); public Object remove(int k) { if (k < 0 k > size() -1) throw new IndexOutOfBoundsException("Index "+k+ " out of bounds."); Object o = contents[k]; --size; for (int i = k; i < size; i++) contents[i] = contents[i+1]; return o; MARKING +1 public class LeakyQ extends ArrayQueue. +1 Constructor, with integer argument. Calls super. +1 public Object remove(int k) (any parameter name) +1 check index in range. +2 throw new exception (does not need to have a message). +1 size decremented. +2 contents shifted by one, starting at removed item. +1 returns the object that was removed. Total pages = 7 Page 3 over...

Question 2. [10 marks] The LinkedRing class provides a linked list of items where the last element in the list refers back to the first element. A sketch of a possible configuration for the LinkedRing is given below: prev: 0 3 1 Each item in the list is stored in a ListNode, which is defined as follows: public class ListNode { public Object value; public ListNode link; public ListNode(Object o) { value = o; Finally, here is the beginning of the LinkedRing class definition: public class LinkedRing { /** If prev == null then the list is empty, * otherwise, prev.link refers to the first item * on the list. */ private ListNode prev; /** The number of items in the list. */ private int size; public LinkedRing() { 2 Write the insert method in the LinkedRing class according to the method comment below. For example, the state pictured to the left would arise from the following code: LinkedRing lr = new LinkedRing(); lr.insert(new Integer(0)); lr.insert(new Integer(1)); lr.insert(new Integer(2)); lr.insert(new Integer(3)); /** Insert object o as the first element in the list, pushing all the other * items in the list one step further away from being the first item. * Postcondition: The link for last item in the list will refer to this * newly inserted item. */ public void insert(object o) { // Complete this method (only). There is more space on the next page. Total pages = 7 Page 4 cont d...

Question 2. SOLUTION (continued) public void insert(object o) { ListNode tmp = new ListNode(o); if (size == 0) { // Create a list of length 1. prev = tmp; prev.link = prev; size = 1; return; tmp.link = prev.link; prev.link = tmp; ++size; MARKING +2 Create a new ListNode containing the Object o. +4 Insertion when the list is empty. 1 Check for empty list. 1 Set prev. 1 Set the new node s link to itself. 1 Set size to be 1 (or increment it by one). +4 Insertion when the list is not empty. 1 Check list is not empty. 1 Link the new node to the old first node. 1 Link the last node (referred to by prev) to the new node. Make sure these updates are done in the correct order (or a temporary variable is used... no deduction for using an extra variable). 1 Increment size. DEDUCTIONS: -2 If the method can ever throw a NullPointerException. -1 If the insertion is done in the wrong place. -2 If part of the list can be lost. Total pages = 7 Page 5 over...

Question 3. [10 marks] Draw the memory model for the situation where the 5th line of the main method is about to be executed. You do not need to draw String or String[] objects. There is more space on the last page. public class Driver { public static void main(string[] args) { 1: Point a = new Point(0, 0); 2: Origin b = new Origin(a); 3: Point c = b.get(); 4: a.x = 100; 5: a.y = 50; SOLUTION: See next page. MARKING: public class Origin { private Point o; public Origin(Point p) { o = p; public Point get() { return o; public class Point { public int x; public int y; public Point(int x, int y) { this.x = x; this.y = y; +4 Runtime Stack 1 Box with label (main:5) and scope (Driver) sub-boxes 1 Point a address1 1 Origin b address2 1 Point c SAME ADDRESS as address1 Can leave types off of a,b,c. Can have a String[] args entry, but can also omit it. +2 Static space. 1 Must have box for Driver, correct label (Driver) and scope (Object). 1 main method in Driver box. Can include boxes for other classes (but not necessary). +4 Object space (Heap) 2 For one Point box, and one Origin box. These boxes should have the correct labels (addresses) and scopes (i.e. Point and Origin, respectively). 1 Contents of Point box. x MUST be 100, and y should be 0 (-1/2 if y is 50). Can leave types off x and y. 1 Contents or Origin box. o MUST have address of a Point box in it. The get() method should be here (1/2 a point), but can leave off the return type of the get() method. Can also have String and/or String[] objects (but not necessary). DEDUCTIONS: -2 If there is more than 1 Point box in the Object Space. -2 Extra boxes on runtime stack, static space, or heap (other than more Point boxes, which is addressed above, or String and String[] boxes, which are optional). Total pages = 7 Page 6 cont d...

Total pages = 7 Page 7 End of Solutions main:5 String[] args Point a xfa Origin b xfb Point c xfa x0f Driver Point Origin xfa int x 100 int y 0 Object Object Point Driver Object void main(string[]) xfb Origin Point o xfa Point get() Question 3. (continued)