Introduction to Computing II (ITI1121) FINAL EXAMINATION

Similar documents
Introduction to Computing II (ITI 1121) FINAL EXAMINATION

Introduction to Computing II (ITI 1121) MIDTERM EXAMINATION

Introduction to Computing II (ITI 1121) FINAL EXAMINATION

Lecture 5: Sep. 23 &25

Lists, Stacks, and Queues (plus Priority Queues)

Data Structures and Algorithms Winter Semester

Outline Resource Introduction Usage Testing Exercises. Linked Lists COMP SCI / SFWR ENG 2S03. Department of Computing and Software McMaster University

Lecture 4: Stacks and Queues

CMSC 132, Object-Oriented Programming II Summer Lecture 6:

CMSC 132, Object-Oriented Programming II Summer Lecture 12

1 ListElement l e = f i r s t ; / / s t a r t i n g p o i n t 2 while ( l e. next!= n u l l ) 3 { l e = l e. next ; / / next step 4 } Removal

Queues. Principles of Computer Science II. Basic features of Queues

Solution suggestions for examination of Logic, Algorithms and Data Structures,

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

SDS developer guide. Develop distributed and parallel applications in Java. Nathanaël Cottin. version

import java. u t i l. ;... Scanner sc = new Scanner ( System. in ) ;

Binary Search Trees. Lecture 29 Section Robb T. Koether. Hampden-Sydney College. Fri, Apr 8, 2016

Hypothesis Tests and Estimation for Population Variances. Copyright 2014 Pearson Education, Inc.

CS-140 Fall 2018 Test 2 Version Practice Nov. 12, Name:

Stacks. Definitions Operations Implementation (Arrays and Linked Lists) Applications (system stack, expression evaluation) Data Structures 1 Stacks

1 Trees. Listing 1: Node with two child reference. public class ptwochildnode { protected Object data ; protected ptwochildnode l e f t, r i g h t ;

MA162 EXAM III SPRING 2017 APRIL 11, 2017 TEST NUMBER 01 INSTRUCTIONS:

Course Announcements. John Jannotti (cs32) Scope, Collections & Generics Feb 8, / 1

Without fully opening the exam, check that you have pages 1 through 10.

ITI Introduction to Computing II

CS-140 Fall 2018 Test 2 Version Practice Nov. 12, 2018

Insert Sorted List Insert as the Last element (the First element?) Delete Chaining. 2 Slide courtesy of Dr. Sang-Eon Park

MA EXAM 2 INSTRUCTIONS VERSION 01 March 9, Section # and recitation time

STUDENT NAME: STUDENT SIGNATURE: STUDENT ID NUMBER: SECTION NUMBER RECITATION INSTRUCTOR:

ITI Introduction to Computing II

MA 262, Spring 2018, Midterm 1 Version 01 (Green)

CSEN202: Introduction to Computer Programming Spring Semester 2014 Final Exam

CMSC 132, Object-Oriented Programming II Summer Lecture 10:

Spring 2017 Exam 2 NAME: PIN:

FACULTY OF SCIENCE ACADEMY OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING OBJECT ORIENTED PROGRAMMING DATE 07/2014 SESSION 8:00-10:00

Paper Reference. Paper Reference(s) 6684/01 Edexcel GCE Statistics S2 Advanced/Advanced Subsidiary. Friday 23 May 2008 Morning Time: 1 hour 30 minutes

Part I: Definitions and Properties

Spring 2018 Exam 2 MARK BOX HAND IN PART NAME: PIN: INSTRUCTIONS

MA EXAM 3 INSTRUCTIONS VERSION 01 April 14, Section # and recitation time

Specification of Chemical Formulæ in XL with Operator Overloading

CMPSCI 311: Introduction to Algorithms Second Midterm Exam

University of Florida EEL 3701 Fall 1996 Dr. Eric M. Schwartz

MODULE TITLE : MASS AND ENERGY BALANCE TOPIC TITLE : ENERGY BALANCE TUTOR MARKED ASSIGNMENT 3

University of Florida EEL 3701 Summer 2015 Dr. Eric. M. Schwartz Department of Electrical & Computer Engineering Tuesday, 30 June 2015

STUDENT NAME: STUDENT SIGNATURE: STUDENT ID NUMBER: SECTION NUMBER AND RECITATION INSTRUCTOR:

Math 51 Midterm 1 July 6, 2016

Outline Resource Introduction Usage Testing Exercises. Linked Lists COMP SCI / SFWR ENG 2S03. Department of Computing and Software McMaster University

Verifying Java-KE Programs

Math 51 First Exam October 19, 2017

Computer Science Introductory Course MSc - Introduction to Java

STUDENT NAME: STUDENT SIGNATURE: STUDENT ID NUMBER: SECTION NUMBER AND RECITATION INSTRUCTOR:

Please read carefully. Good luck & Go Gators!!!

Statistics 433 Practice Final Exam: Cover Sheet and Marking Sheet

University of Ottawa

Our true, holographic blueprint of the human matrix system

MA EXAM 1 INSTRUCTIONS VERSION 01 September 13, Section # and recitation time

MA EXAM 3 INSTRUCTIONS VERSION 01 April 18, Section # and recitation time

Computer Science Introductory Course MSc - Introduction to Java

YORK UNIVERSITY. Faculty of Science Department of Mathematics and Statistics. MATH C Test #1. October 23, 2013.

Fall 2017 Exam 1 MARK BOX HAND IN PART NAME: PIN:

Fall 2018 Exam 1 NAME:

MA EXAM 3 INSTRUCTIONS VERSION 01 April 17, Section # and recitation time

Read this before starting!

ECEN 651: Microprogrammed Control of Digital Systems Department of Electrical and Computer Engineering Texas A&M University

Blocking Synchronization: Streams Vijay Saraswat (Dec 10, 2012)

CS Exam 1 Study Guide and Practice Exam

Test 3 Version A. On my honor, I have neither given nor received inappropriate or unauthorized information at any time before or during this test.

CSE 355 Test 2, Fall 2016

MA EXAM 2 INSTRUCTIONS VERSION 01 March 10, Section # and recitation time

EECS 70 Discrete Mathematics and Probability Theory Fall 2015 Walrand/Rao Final

MA EXAM 1 INSTRUCTIONS VERSION 01 FEBRUARY 8, Section # and recitation time

Read this before starting!

University of Ottawa

Programming Language Concepts, CS2104 Lecture 3

Voortgezet Programmeren

Sharing Objects. Pieter van den Hombergh. Fontys Hogeschool voor Techniek en Logistiek. February 15, 2017

Compiling Techniques

Physics 101. Hour Exam I Fall Last Name: First Name Network-ID Discussion Section: Discussion TA Name:

Student s Printed Name:

Read this before starting!

1 Your full name (PLEASE PRINT) First hour test page 1 of 5 September 27, 2007 Your scheduled Tuesday quiz section (please circle) B hr E hr

by Keith Yoder illustrated by Wallace Keller

Error Detection and Correction: Small Applications of Exclusive-Or

Fall 2016 Exam 1 HAND IN PART NAME: PIN:

CISC 4090: Theory of Computation Chapter 1 Regular Languages. Section 1.1: Finite Automata. What is a computer? Finite automata

STUDENT NAME: STUDENT SIGNATURE: STUDENT ID NUMBER: SECTION NUMBER RECITATION INSTRUCTOR:

Binary Search Trees. Motivation

University of Toronto Department of Electrical and Computer Engineering. Final Examination. ECE 345 Algorithms and Data Structures Fall 2016

'XNH8QLYHUVLW\ (GPXQG73UDWW-U6FKRRORI(QJLQHHULQJ. EGR 224 Spring Test II. Michael R. Gustafson II

Physics 101 Hour Exam 1 March 3, 2014

MA Exam 1 Fall 2015 VERSION 01

The University of British Columbia. Mathematics 300 Final Examination. Thursday, April 13, Instructor: Reichstein

Models of Computation. by Costas Busch, LSU

University of Florida EEL 3701 Fall 2014 Dr. Eric. M. Schwartz Department of Electrical & Computer Engineering Wednesday, 15 October 2014

CSE 211. Pushdown Automata. CSE 211 (Theory of Computation) Atif Hasan Rahman

E23: Hotel Management System Wen Yunlu Hu Xing Chen Ke Tang Haoyuan Module: EEE 101

Hour Examination # 3

Construction of Static Single-Assignment Form

Advanced Implementations of Tables: Balanced Search Trees and Hashing

Techniques of Java Programming

Transcription:

Université d Ottawa Faculté de génie École de science informatique et de génie électrique University of Ottawa Faculty of engineering School of Electrical Engineering and Computer Science Identification Introduction to Computing II (ITI1121) FINAL EXAMINATION Instructors: Nour El-Kadri, Guy-Vincent Jourdan, and Marcel Turcotte April 2016, duration: 3 hours Last name: First name: Student #: Seat #: Signature: Section: A or B or C Instructions 1. This is a closed book examination. 2. No calculators, electronic devices or other aids are permitted. (a) Any electronic device or tool must be shut off, stored and out of reach. (b) Anyone who fails to comply with these regulations may be charged with academic fraud. 3. Write your answers in the space provided. (a) Use the back of pages if necessary. (b) You may not hand in additional pages. 4. Do not remove pages or the staple holding the examination pages together. 5. Write comments and assumptions to get partial marks. 6. Beware, poor hand-writing can affect grades. 7. Wait for the start of the examination. Marking scheme Question Maximum Result 1 15 2 15 3 25 4 15 5 10 Total 80 All rights reserved. No part of this document may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording or otherwise without prior written permission from the instructors.

April 2016 ITI1121 Page 2 of 16 Question 1 Multiple-choice questions (15 marks) A. Practices such as the one depicted by the following program should be avoided. Assume that s designates a valid instance of some stack implementation and that value is of the correct type. True or False boolean done = f a l s e ; while (! done ) { t r y { v a l u e = s. pop ( ) ; catch ( EmptyStackException e ) { done = t rue ; B. A method that throws unchecked exceptions must declare them using the keyword throws, otherwise a compile-time error will be produced. True or False C. Good object oriented programming practices suggest that a programmer must always catch all the exceptions generated by the methods being called. True or False D. Consider two empty BinarySearchTree objects and the method add presented in class. Adding the same elements, but in different order into the two trees, will always lead to trees of the same height. True or False. E. Consider the binary search tree below. For each series of nodes, circle the corresponding traversing strategy (pre-order, in-order, post-order, other): Order of traversal: 6-2-1-4-3-5-7-9-8 Pre-order In-order Post-order Other Order of traversal: 1-3-5-4-2-8-9-7-6 Pre-order In-order Post-order Other Order of traversal: 1-2-3-4-5-6-7-8-9 Pre-order In-order Post-order Other

April 2016 ITI1121 Page 3 of 16 F. Consider 4 implementations of a list: ArrayList: a simple array implementation, which has an instance reference variable to the array, as well as an instance variable containing the number of elements in the array. CircularArrayList: a circular array implementation, which has an instance reference variable to the array, as well as instance variables for the current front and rear elements in the array. LinkedList: a (singly) linked list implementation, which has an instance reference variable to the current head Node of the list. DoublyLinkedList: a doubly linked list implementation, which has an instance reference variable to the current head Node of the list and an instance reference variable to the current tail Node of the list, and doubly linked Nodes. For each of the six methods below, indicate for each implementation if the method can be Fast (that is, its execution time is constant) or if it will be Slow (that is, proportional to the number of elements in the list). Note that both array implementations are based on dynamic arrays, and thus can accommodate any number of elements. However, the array is not automatically shrunk. void addfirst(e elem) void addlast(e elem) void add(e elem, int pos) E get(int pos) void removefirst() void removelast() ArrayList CircularArrayList LinkedList DoublyLinkedList

April 2016 ITI1121 Page 4 of 16 Question 2 (15 marks) As we have done for Question 1 of Assignment 4, reverse engineer the memory diagram below: you need to provide the implementation of all the classes, instance variables, constructors and the main method that will lead to that memory state. Hint: You need a class Book and a class Author, as well as a main method.

April 2016 ITI1121 Page 5 of 16

April 2016 ITI1121 Page 6 of 16 Question 3 (25 marks) We have seen in class two main ways of implementing a list: using an array or using linked elements. We have also seen that both approaches each have strengths and weaknesses. In some cases, it could be useful to be able to switch between the two approaches, based on what the application is currently doing. Our goal is to provide a hybrid implementation. That implementation provides both an array implementation and a linked-list implementation, albeit not at the same time. The linked-list implementation uses a singly-list approach (with no dummy node). The array implementation uses a simple array (no circular array). In order to support both implementations, our HybridList class will have the following instance variables: headnode is a reference pointing at the first Node of a non-empty list. headarray is a reference variable pointing to the array. currentsize is a primitive variable which holds the current number of elements in the list. currentcapacity is a primitive variable which holds the current capacity for the array. Finally, isarray is primitive variable used to record whether the object is currently using an array or a linked-list to store the list s elements. The code below shows the beginning of the implementation. p u b l i c c l a s s H y b r i d L i s t <E> implements L i s t <E> { p r i v a t e s t a t i c c l a s s Node<T> { p r i v a t e T v a l u e ; p r i v a t e Node<T> n e x t ; p r i v a t e Node ( T value, Node<T> n e x t ) { t h i s. v a l u e = v a l u e ; t h i s. n e x t = n e x t ; p r i v a t e Node<E> headnode ; p r i v a t e E [ ] headarray ; p r i v a t e i n t c u r r e n t S i z e = 0 ; p r i v a t e i n t c u r r e n t C a p a c i t y = 5 0 ; p r i v a t e boolean i s A r r a y = f a l s e ; p u b l i c boolean isempty ( ) { return c u r r e n t S i z e == 0 ; / /...

April 2016 ITI1121 Page 7 of 16 You need to provide the implementation of 2 methods: the method public void toarray(), which changes the storage of the current list elements from linked list to array, the method public void tolinkedlist(), which changes the storage of the current list elements from array to linked list. In both cases, if the implementation is already of the right kind (already using an array when toarray() is called, or already using a linked list when tolinkedlist() is called), the method does nothing. In these methods, you do not worry about memory management. In particular, you do not need to scrub the memory of the old storage mechanism when switching to the new one. For your implementation of the methods, you cannot use the methods of the class HybridList, accordingly these methods are not shown on the listing 1. Your code needs to manipulate the instance variables directly, headnode, headarray, currentsize, currentcapacity and isarray. You can assume that currentsize is always correct. However, you cannot assume that the value stored in currentcapacity is up-to-date when the method toarray() is called, so you have to take care of this. Note that neither toarray() to tolinkedlist() return any value. representation of the list. They simply change the internal The following sample code shows a program using that class. In this sample program, the hybrid list is first used as a LinkedList. In that form, a series of elements are added. It is then switched to an ArrayList, and the elements are accessed through the method get(). p u b l i c s t a t i c void main ( S t r i n g [ ] a r g s ) { H y b r i d L i s t <I n t e g e r > h L i s t = new H y b r i d L i s t <I n t e g e r > ( ) ; h L i s t. t o L i n k e d L i s t ( ) ; / / h L i s t i s now a L i n k e d L i s t f o r ( i n t i =0; i <100; i ++) { h L i s t. a d d F i r s t ( i ) ; h L i s t. t o A r r a y ( ) ; / / h L i s t i s now an Array f o r ( i n t i = 0 ; i < h L i s t. s i z e ( ) ; i ++) { System. o u t. p r i n t l n ( h L i s t. g e t ( i ) ) ; In the next 2 pages, provide your code for both methods. 1 The method isempty() is shown, so you can use this one.

April 2016 ITI1121 Page 8 of 16 / / c o n t i n u i n g c l a s s H y b r i d L i s t <E> i m p l e m e n t s L i s t <E> p u b l i c void t o A r r a y ( ) { / / End o f t o A r r a y

April 2016 ITI1121 Page 9 of 16 / / c o n t i n u i n g c l a s s H y b r i d L i s t <E> i m p l e m e n t s L i s t <E> p u b l i c void t o L i n k e d L i s t ( ) { / / End o f t o L i n k e d L i s t

April 2016 ITI1121 Page 10 of 16 Question 4 (15 marks) Write the static method roll(stack<e> s, int n) that transforms the stack designated by s such that the n bottom elements are now on the top of the stack. The relative order of the top and bottom elements must remain unchanged. Stack <S t r i n g > s ; s = new LinkedStack <S t r i n g > ( ) ; s. push ( "a" ) ; s. push ( "b" ) ; s. push ( "c" ) ; s. push ( "d" ) ; s. push ( "e" ) ; System. o u t. p r i n t l n ( s ) ; r o l l ( s, 2 ) ; System. o u t. p r i n t l n ( s ) ; Executing the above Java test program displays the following: [a, b, c, d, e] [c, d, e, a, b] Make sure to handle all exceptional situations appropriately. Since you do not know the size of the stack, you cannot use arrays for temporary storage. Instead, you must use stacks. You can assume the existence of the class LinkedStack, which implements the interface Stack. Stack is an interface. p u b l i c i n t e r f a c e Stack <E> { boolean isempty ( ) ; E peek ( ) ; E pop ( ) ; void push ( E elem ) ;

April 2016 ITI1121 Page 11 of 16 p u b l i c c l a s s R o l l { p u b l i c s t a t i c <E> void r o l l ( Stack <E> s, i n t n ) { / / End o f r o l l / / End o f R o l l

April 2016 ITI1121 Page 12 of 16 Question 5 (10 marks) Consider the class SinglyLinkedList outlined below: p u b l i c c l a s s S i n g l y L i n k e d L i s t <E> implements L i s t <E> { p r i v a t e s t a t i c c l a s s Node<E> { p r i v a t e E v a l u e ; p r i v a t e Node<E> n e x t ; p r i v a t e Node ( E value, Node<E> n e x t ){ t h i s. v a l u e = v a l u e ; t h i s. n e x t = n e x t ; p r i v a t e Node<E> head ; / / c l a s s c o n t i n u e s a f t e r t h a t Consider the following code for the method mystery1 in the class SinglyLinkedList p u b l i c boolean mystery1 ( E o ) { i f ( o == n u l l ) { throw new N u l l P o i n t e r E x c e p t i o n ( "Null parameter" ) ; i f ( head == n u l l ) { return f a l s e ; boolean r e s u l t = mystery1 ( head, o ) ; i f ( head. v a l u e. e q u a l s ( o ) ) { head = head. n e x t ; return t rue ; e l s e { return r e s u l t ; p r i v a t e boolean mystery1 ( Node<E> p, E o ) { i f ( p. n e x t == n u l l ) { return f a l s e ; boolean r e s u l t = mystery1 ( p. next, o ) ; i f ( p. n e x t. v a l u e. e q u a l s ( o ) ) { p. n e x t = p. n e x t. n e x t ; return t rue ; e l s e { return r e s u l t ;

April 2016 ITI1121 Page 13 of 16 A. Let list be an instance of the class SinglyLinkedList containing the following list: [A,B,C,A,B,C,A,B,C]. What will the following code print out? (Note: the method tostring() of the class SinglyLinkedList, not shown here, simply prints the values of the elements in the list). S i n g l y L i n k e d L i s t <S t r i n g > l i s t = new S i n g l y L i n k e d L i s t <S t r i n g > ( ) ; l i s t. add ( "A" ) ; l i s t. add ( "B" ) ; l i s t. add ( "C" ) ; l i s t. add ( "A" ) ; l i s t. add ( "B" ) ; l i s t. add ( "C" ) ; l i s t. add ( "A" ) ; l i s t. add ( "B" ) ; l i s t. add ( "C" ) ; System. o u t. p r i n t l n ( l i s t ) ; System. o u t. p r i n t l n ( l i s t. mystery1 ( "A" ) ) ; System. o u t. p r i n t l n ( l i s t ) ; System. o u t. p r i n t l n ( l i s t. mystery1 ( "C" ) ) ; System. o u t. p r i n t l n ( l i s t ) ; System. o u t. p r i n t l n ( l i s t. mystery1 ( "A" ) ) ; System. o u t. p r i n t l n ( l i s t ) ; Write your answer below. The answer to the first call to System.out.println(list); is already provided. [A, B, C, A, B, C, A, B, C]

April 2016 ITI1121 Page 14 of 16 Now consider the following code for the method mystery2 in the class SinglyLinkedList. Make sure to spot the subtle differences between the methods mystery1 and mystery2. p u b l i c boolean mystery2 ( E o ) { i f ( o == n u l l ) { throw new N u l l P o i n t e r E x c e p t i o n ( "Null parameter" ) ; i f ( head == n u l l ) { return f a l s e ; boolean r e s u l t = mystery2 ( head, o ) ; i f ( head. v a l u e. e q u a l s ( o ) ) { i f ( r e s u l t ) { head = head. n e x t ; return t rue ; e l s e { return r e s u l t ; p r i v a t e boolean mystery2 ( Node<E> p, E o ) { i f ( p. n e x t == n u l l ) { return f a l s e ; boolean r e s u l t = mystery2 ( p. next, o ) ; i f ( p. n e x t. v a l u e. e q u a l s ( o ) ) { i f ( r e s u l t ) { p. n e x t = p. n e x t. n e x t ; return t rue ; e l s e { return r e s u l t ;

April 2016 ITI1121 Page 15 of 16 B. Let list be an instance of SinglyLinkedList containing the following list: [A,B,C,A,B,C,A,B,C]. What will the following code print out? S i n g l y L i n k e d L i s t <S t r i n g > l i s t = new S i n g l y L i n k e d L i s t <S t r i n g > ( ) ; l i s t. add ( "A" ) ; l i s t. add ( "B" ) ; l i s t. add ( "C" ) ; l i s t. add ( "A" ) ; l i s t. add ( "B" ) ; l i s t. add ( "C" ) ; l i s t. add ( "A" ) ; l i s t. add ( "B" ) ; l i s t. add ( "C" ) ; System. o u t. p r i n t l n ( l i s t ) ; System. o u t. p r i n t l n ( l i s t. mystery2 ( "A" ) ) ; System. o u t. p r i n t l n ( l i s t ) ; System. o u t. p r i n t l n ( l i s t. mystery2 ( "C" ) ) ; System. o u t. p r i n t l n ( l i s t ) ; System. o u t. p r i n t l n ( l i s t. mystery2 ( "A" ) ) ; System. o u t. p r i n t l n ( l i s t ) ; Write your answer below. The answer to the first call to System.out.println(list); is already provided. [A, B, C, A, B, C, A, B, C]

April 2016 ITI1121 Page 16 of 16 (blank space)