Introduction to Computing II (ITI 1121) Final Examination

Similar documents
Introduction to Computing II (ITI 1121) Final Examination

Introduction to Computer Science II (ITI 1121) Final Examination

Introduction to Computer Science II (ITI 1121) Midterm Examination

Introduction to Computer Science II (CSI 1101) Final Examination

Introduction to Computing II (ITI 1121) FINAL EXAMINATION

Introduction to Computer Science II (CSI 1101) Midterm Examination

Introduction to Computer Science II (CSI 1101)

ITI Introduction to Computing II

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Introduction to Computer Science II (ITI 1121) Midterm Examination

Introduction to Computing II (ITI 1121) Midterm Examination

Introduction to Computing II (ITI 1121) Midterm Examination

Stacks. Stacks. Main stack operations. The ADT Stack stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) principle.

Name CPTR246 Spring '17 (100 total points) Exam 3

ITI Introduction to Computing II

ITI Introduction to Computing II

CSE 143 SAMPLE MIDTERM

COS 226 Algorithms and Data Structures Fall Midterm

Largest Online Community of VU Students

CSCE 2014 Final Exam Spring Version A

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

Computer Science 136 Exam 2

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

SOLUTIONS (INCOMPLETE, UNCHECKED)

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

Introduction to Computer Science II (CSI 1101) Midterm Examination

This examination has 11 pages. Check that you have a complete paper.

ITI Introduction to Computing II

CS 3114 Data Structures and Algorithms DRAFT Project 2: BST Generic

COE 312 Data Structures. Welcome to Exam II Monday November 23, Instructor: Dr. Wissam F. Fawaz

Summer Final Exam Review Session August 5, 2009

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

ITI Introduction to Computing II

CS24 Week 8 Lecture 1

Topic 14. The BinaryTree ADT

9/26/2018 Data Structure & Algorithm. Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

CS 231 Data Structures and Algorithms Fall DDL & Queue Lecture 20 October 22, Prof. Zadia Codabux

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

COMP 250 Midterm #2 March 11 th 2013

CS 216 Exam 1 Fall SOLUTION

CS260 Intro to Java & Android 03.Java Language Basics

Final Exam. Kenneth J. Goldman December 18, Name: Student ID Number: Signature:

JAVA Programming Language Homework I - OO concept

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.

JAVA MOCK TEST JAVA MOCK TEST II

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

Dartmouth College Computer Science 10, Fall 2014 Solution to Final Exam A

Introduction to Computer Science II (CSI 1101) Final Examination

CSCI Lab 9 Implementing and Using a Binary Search Tree (BST)

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

12 Abstract Data Types

York University AK/ITEC INTRODUCTION TO DATA STRUCTURES. Final Sample II. Examiner: S. Chen Duration: Three hours

3. Java - Language Constructs I

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

In this lab we will practice creating, throwing and handling exceptions.

COMP 103. Data Structures and Algorithms

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

Programming overview

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

tree nonlinear Examples

ITI Introduction to Computing II

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Declarations and Access Control SCJP tips

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

ITI Introduction to Computing II

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

F I N A L E X A M I N A T I O N

Final Exam Data Structure course. No. of Branches (5)

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring Instructions:

Inheritance and Interfaces

CSE 143 SAMPLE MIDTERM SOLUTION

CSIS 10B Lab 2 Bags and Stacks

Largest Online Community of VU Students

ITI Introduction to Computing II

Name: After line 1: After line 3: After line 5: After line 8: After line 11:

Chapter 20: Binary Trees

Computer Science E-119 Fall Problem Set 4. Due prior to lecture on Wednesday, November 28

ITI Introduction to Computing II

SOLUTIONS. COMP103 Introduction to Data Structures and Algorithms

CS171 Midterm Exam. October 29, Name:

ITI Introduction to Computing II

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

CS 2334: Programming Structures and Abstractions: Exam 1 October 3, 2016

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

Largest Online Community of VU Students

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

Computer Science 1 Ah

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

Module 6: Binary Trees

Chapter 1: Introduction to Computers, Programs, and Java

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

Selected Java Topics

Examination Questions Midterm 1

EXAMINATIONS 2012 MID YEAR. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

Multiple choice questions. Answer on Scantron Form. 4 points each (100 points) Which is NOT a reasonable conclusion to this sentence:

CSE 214 Computer Science II Heaps and Priority Queues

Transcription:

Introduction to Computing II (ITI 1121) Final Examination Instructor: Marcel Turcotte April 2010, duration: 3 hours Identification Student name: Student number: Signature: Instructions 1. 2. 3. 4. 5. 6. This is a closed book examination; No calculators or other aids are permitted; Write comments and assumptions to get partial marks; Beware, poor hand writing can affect grades; Do not remove the staple holding the examination pages together; Write your answers in the space provided. Use the back of pages if necessary. You may not hand in additional pages. Marking scheme Question 1 2 3 4 5 6 7 Total Maximum 10 10 15 15 12 13 25 100 Result

April 2010 ITI 1121 Page 2 of 18 Question 1: (10 marks) For each statement, you must circle the correct answer, either true or false. A. In Java, total, ToTal and TOTAL are all different identifiers. true false B. A single class may implement multiple interfaces. true false C. In Java, a subclass can only extend one parent class. true false D. A child class is allowed to define a method with the same name and parameter list as a method in the parent class. true false E. An abstract class must contain abstract methods. true false F. Unchecked exceptions must be caught or propogated, or a program will not compile. true false G. A breadth-first search uses a stack as a supporting data structure. true false H. It is possible to implement a stack and a queue in such a way that all the operations take a constant amount of time. true false I. A program with infinite recursion will act similarly to a program with an infinite loop. true false J. In a tree, a node that does not have any children is called a leaf. true false

April 2010 ITI 1121 Page 3 of 18 Question 2: (10 marks) A. The Java Standard Edition Development Kit includes a program that can package several Java files into a single file. What is the name of this program? (a) zip (b) tar (c) compress (d) jar (e) WinZip B. Which of the following statements and declarations is not valid? (a) next() = next + 1; (b) a = b / c % d; (c) int money, dollars = 0, cents = 0; (d) float pi = 3.1415F; (e) all of the above C. The following Java program: (a) will result in a compile-time error (b) will result in a run-time error (c) displays true (d) displays false (e) displays 5 == 5 public class Test { public static void incr( int value ) { value = value + 1; public static void incr( Integer value ) { value = value + 1; public static void main( String[] args ) { int i1; Integer i2; i1 = 5; incr( i1 ); i2 = 4; incr( i2 ); System.out.println( i1 == i2 );

April 2010 ITI 1121 Page 4 of 18 Question 2: (continued) D. A class declared as final. (a) cannot be changed (b) cannot have subclasses (c) cannot have superclasses (d) has several abstract methods (e) cannot be used in a program E. To invoke a parents constructor in a subclass, we use the method. (a) abstract (b) construct (c) parent (d) super (e) extends F. A variable is shared among all instances of a class. (a) static (b) final (c) public (d) private (e) none of the above G. Which of the following refers to a set of data and the operations that are allowed on that data? (a) abstract data type (b) generic (c) collection (d) bag (e) none of the above

April 2010 ITI 1121 Page 5 of 18 Question 2: (continued) H. Which of the following is an advantage of an array-based implementation over a linked-based implementation? (a) the structure grows and shrinks dynamically in the array-based implementation (b) the underlying structure is a fixed size array-based implementation (c) the elements can be accessed directly in an array-based implementation (d) all of the above (e) neither a, b, nor c I. In a binary search tree, the elements in the right subtree of the root are the root element. (a) greater than (b) less than (c) greater than or equal to (d) less than or equal to (e) equal to J. When removing an element from a binary search tree that is a leaf, will ensure that the resulting tree is still a binary search tree. (a) replacing it with its only child (b) replacing it with its inorder successor (c) simply deleting it (d) all of the above (e) neither a, b, nor c

April 2010 ITI 1121 Page 6 of 18 Question 3: (15 marks) A. Write out the order of the elements that are contained in a stack after the following operations are performed. Make sure to indicate which element is the top element. s.push( new Integer( 8 ) ); s.push( new Integer( 6 ) ); Integer num = s.pop(); s.push( new Integer( 3 ) ); s.push( new Integer( 4 ) ); s.push( new Integer( 15 ) ); s.push( new Integer( 12 ) ); s.pop(); s.pop(); s.pop(); s.push( new Integer( 19 ) ); B. Draw the binary search tree that results from inserting the following elements, in that order, when using the method add presented in class: 12, 16, 9, 1, 15, 13.

April 2010 ITI 1121 Page 7 of 18 C. Give the outcome of the program. public class Test { public static int bar( int i ) { if ( i == 2 ) { throw new Exception( "oups!" ); return i; public static int foo() { int result; result = bar( 1 ); try { result = bar( 2 ); catch ( Exception e ) { result = bar( 3 ); return result; public static void main( String[] args ) { System.out.println( foo() );

April 2010 ITI 1121 Page 8 of 18 D. The class Sequence implements a linked list with additional methods to write efficient recursive list processing methods outside of the implementation of the list. Here are the characteristics of the class Sequence. boolean isempty(); returns true if and only if this list is empty; Sequence<E> split(); returns the tail of this sequence, this sequence now contains a single element. It throws IllegalStateException if the Sequence was empty when the call was made; void join( Sequence<E> other ); appends other at the end of this sequence, other is now empty; deletefirst() removes the first element of this Sequence. public static <E> int april2010( Sequence<E> l ) { int result = 0; if (! l.isempty() ) { Sequence<E> tail = l.split(); result = 1 + april2010( tail ); if ( result % 2 == 0 ) { l.deletefirst(); result = 0; l.join( tail ); return result; Let s designate a Sequence containing the following elements, in that order, {0,1,2,3,4, what will the content of s after the call april2010( s )?

April 2010 ITI 1121 Page 9 of 18 Question 4: (15 marks) In the class SinglyLinkedList below, write a recursive (instance) method that returns a list of all the positions where a given object is found. The method must be implemented following the technique presented in class for implementing recursive methods inside the class. Briefly, there will be a public method that initiates the first call to the recursive private method. Let l designate a list containing the elements {A, B, A, A, C, D, A, then a call l.indexofall(a) would return the following list of positions {0, 2, 3, 6. public class SinglyLinkedList<E> { private static class Node<F> { private F value; private Node<F> next; private Node( F value, Node<F> next ) { this.value = value; this.next = next; // Instance variable private Node<E> first; // Instance methods public void addfirst( E item ) {... public void addlast( E item ) {... // Complete the implementation of the method below public indexofall( E elem ) { // End of indexofall

April 2010 ITI 1121 Page 10 of 18 // Complete the implementation of the method below private indexofallrec( ) { // End of indexofallrec // End of SinglyLinkedList

April 2010 ITI 1121 Page 11 of 18 Question 5: (12 marks) In the partial implementation of the class CircularQueue below, complete the implementation of the instance method void rotate( int n ). The method rotate( int n ) removes n elements from the front of the queue and adds them to the rear of the queue. The elements are added at the rear of the queue in the same order as they are removed from the front of the queue. For example, given a queue q containing the elements A, B, C, D, E, where the element A is at the front of the queue, following the method call q.rotate( 2 ), the content of the queue will be C, D, E, A, B ; CircularQueue uses the circular array technique seen in class, i.e. the front and rear of the queue are allowed to wrap around the boundaries of the array; The instance variable front designates the cell of the array that contains the front element of the queue or -1 if the queue is empty; The instance variable rear designates the cell of the array that contains the rear element of the queue or -1 if the queue is empty; The array elems is used to store the elements of this queue; If the value of the parameter is negative, the method must throw the exception IllegalArgumentException; For your implementation of the method rotate, you are not allowed to use any instance method, in particular, you cannot use the methods enqueue and dequeue. Your solution must make explicit use of the variables front and rear. Complete the implementation of the methods rotate( int n ) on the next page. public class CircularQueue<E> implements Queue<E> { private E[] elems; private int front; private int rear; public CircularQueue( int capacity ) { if ( capacity < 0 ) { throw new IllegalArgumentException( "negative number" ); elems = ( E[] ) new Object[ capacity ]; front = -1; rear = -1;

April 2010 ITI 1121 Page 12 of 18 public void rotate( int n ) { // End of CircularQueue

April 2010 ITI 1121 Page 13 of 18 Question 6: (13 marks) Implement the method rotate, as described for Question 5, but this time for the implementation of a queue based on linked elements. LinkedQueue uses linked elements to store the values of this queue; The instance variable front designates the front element of the queue or has value null if the queue is empty; The instance variable rear designates the rear element of the queue or is null if the queue is empty; If the value of the parameter is negative, the method must throw the exception IllegalArgumentException; For your implementation of the method rotate, you are not allowed to use any instance method, in particular, you cannot use the methods enqueue and dequeue. Your solution must explicitly change the structure of the list by changing the links of the list (the references next). Complete the implementation of the methods rotate( int n ) on the next page. public class LinkedQueue<E> implements Queue<E> { private static class Elem<F> { private F value; private Elem<F> next; private Elem( F value, Elem<F> next ) { this.value = value; this.next = next; private Elem<E> front; private Elem<E> rear; public LinkedQueue() { front = rear = null;

April 2010 ITI 1121 Page 14 of 18 public void rotate( int n ) { // End of LinkedQueue

April 2010 ITI 1121 Page 15 of 18 Question 7: (25 marks) The UML diagram below shows a hierarchy of classes to represent postal codes of different countries. PostalCode + getcode() : String + isvalid() : boolean CanadianPostalCode USZipCode Knowing that: All postal codes have a method getcode that returns the code (of type String) represented by this instance; All postal codes have a method isvalid that returns true if this code is valid and false otherwise; A Canadian postal code is valid if positions 0, 2 and 5 are letters, positions 1, 4 and 6 are digits, and the position 3 is a blank character; A valid US zip code consists of two letters, followed by a blank character, followed by 5 digits. Write an implementation for the classes PostalCode, CanadianPostalCode and USZip- Code. Make sure to include the instance variables and necessary constructors. Appendix A lists some of the methods of the classes String and Character. // Implement the class PostalCode here

April 2010 ITI 1121 Page 16 of 18 Question 7: (continued) // Implement the class CanadianPostalCode here

April 2010 ITI 1121 Page 17 of 18 Question 7: (continued) // Implement the class USZipCode here

April 2010 ITI 1121 Page 18 of 18 A Appendix The class String includes the following methods. char charat(int pos): Returns the character at the specified index. int length(): Returns the length of this string. The class Character includes the following methods. static boolean isdigit(char ch): Determines if the specified character is a digit. static boolean isletter(char ch): Determines if the specified character is a letter. static boolean iswhitespace(char ch): Determines if the specified character is white space according to Java.