Faculty of Science FINAL EXAMINATION

Similar documents
Faculty of Science FINAL EXAMINATION

QUIZ 2 Introduction to Computer Science (COMP 250) Mon. March 2, 2009 Professor Michael Langer

Faculty of Science FINAL EXAMINATION COMP-250 A Introduction to Computer Science School of Computer Science, McGill University

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

CS171 Final Practice Exam

Prelim 2, CS2110. SOLUTION

CS171 Final Practice Exam

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

Prelim 2 Solution. CS 2110, April 26, 2016, 5:30 PM

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

Prelim 2 Solution. CS 2110, November 19, 2015, 5:30 PM Total. Sorting Invariants Max Score Grader

Prelim 2 Solution. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

Prelim 2. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

Data Structure and Algorithm, Spring 2013 Midterm Examination 120 points Time: 2:20pm-5:20pm (180 minutes), Tuesday, April 16, 2013

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

Prelim 2. CS 2110, April 26, 2016, 5:30 PM Total Question True/False Complexity Heaps Trees Graphs Max Score Grader

Total Score /1 /20 /41 /15 /23 Grader

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

Prelim 2 Solution. CS 2110, April 26, 2016, 7:30 PM

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

COMP 250 Fall recurrences 2 Oct. 13, 2017

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

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

Data Structures Question Bank Multiple Choice

CS 373: Combinatorial Algorithms, Spring 1999

McGill University Faculty of Science School of Computer Science. MIDTERM EXAMINATION - Sample solutions. COMP-250: Introduction to Computer Science

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

UNIVERSITY REGULATIONS

CS210 (161) with Dr. Basit Qureshi Final Exam Weight 40%

Midterm Exam Fundamentals of Computer Graphics (COMP 557) Thurs. Feb. 19, 2015 Professor Michael Langer

Second Examination Solution

CLO Assessment CLO1 Q1(10) CLO2 Q2 (10) CLO3 Q4 (10) CLO4 Q3a (4)

CS1800 Discrete Structures Final Version B

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total

Prelim 2 SOLUTION. 5:30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs

COMP 250 Fall priority queues, heaps 1 Nov. 9, 2018

CPSC 311: Analysis of Algorithms (Honors) Exam 1 October 11, 2002

COMP 250 Midterm #2 March 11 th 2013

ECE250: Algorithms and Data Structures Midterm Review

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

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

Largest Online Community of VU Students

Topics for CSCI 151 Final Exam Wednesday, May 10

CS171 Midterm Exam. October 29, Name:

Prelim One Solution. CS211 Fall Name. NetID

& ( D. " mnp ' ( ) n 3. n 2. ( ) C. " n

21# 33# 90# 91# 34# # 39# # # 31# 98# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# #

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit.

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Student number: Datenstrukturen & Algorithmen page 1

implementing the breadth-first search algorithm implementing the depth-first search algorithm

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

Draw a diagram of an empty circular queue and describe it to the reader.

Solutions to Exam Data structures (X and NV)

COMP251: Background. Jérôme Waldispühl School of Computer Science McGill University

We've tried to include the common errors and grading standard for every question.

CS301 - Data Structures Glossary By

Selection, Bubble, Insertion, Merge, Heap, Quick Bucket, Radix

Midterm Exam 2 CS 455, Spring 2015

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

12 Abstract Data Types

CSE 2320 Section 002, Fall 2015 Exam 2 Time: 80 mins

CS126 Final Exam Review

OF VICTORIA EXAMINATIONS- DECEMBER 2010 CSC

CS302 Data Structures using C++

CSL 201 Data Structures Mid-Semester Exam minutes

COMP Data Structures

Prelim 2, CS :30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs

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

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures

NET/JRF-COMPUTER SCIENCE & APPLICATIONS. Time: 01 : 00 Hour Date : M.M. : 50

( ) + n. ( ) = n "1) + n. ( ) = T n 2. ( ) = 2T n 2. ( ) = T( n 2 ) +1

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Spring 2016

You have 90 minutes to complete the exam of InformatikIIb. The following rules apply:

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Section 1: True / False (2 points each, 30 pts total)

CS 112 Final May 8, 2008 (Lightly edited for 2012 Practice) Name: BU ID: Instructions

Computer Programming II C++ (830)

SOLUTIONS. COMP103 Introduction to Data Structures and Algorithms

( ). Which of ( ) ( ) " #& ( ) " # g( n) ( ) " # f ( n) Test 1

Part 1: Multiple Choice Circle only ONE answer. Each multiple choice question is worth 3.5 marks.

U.C. Berkeley CS170 : Algorithms, Fall 2013 Midterm 1 Professor: Satish Rao October 10, Midterm 1 Solutions

Graphs & Digraphs Tuesday, November 06, 2007

COMP 250 Fall inheritance Nov. 17, 2017

Matriculation number:

Computer Programming II Python

CMSC351 - Fall 2014, Homework #2

CS/ENGRD2110: Final Exam

INSTRUCTIONS TO CANDIDATES

MIDTERM EXAMINATION Douglas Wilhelm Harder EIT 4018 x T09:30:00P1H20M Rooms: RCH-103 and RCH-302

COMP 250 Fall Homework #4

CSE 332 Spring 2014: Midterm Exam (closed book, closed notes, no calculators)

Recitation 9. Prelim Review

) $ f ( n) " %( g( n)

The divide and conquer strategy has three basic parts. For a given problem of size n,

CS161 - Final Exam Computer Science Department, Stanford University August 16, 2008

Final Examination. Algorithms & Data Structures II ( )

SOME TYPICAL QUESTIONS, THEIR EXPECTED ANSWERS AND THINGS

CSE 373 Final Exam 3/14/06 Sample Solution

Transcription:

Faculty of Science FINAL EXAMINATION COMPUTER SCIENCE COMP 250 INTRODUCTION TO COMPUTER SCIENCE Examiner: Prof. Michael Langer April 27, 2010 Associate Examiner: Mr. Joseph Vybihal 9 A.M. 12 P.M. Instructions: The exam is 6 pages, including this cover page. There are 9 questions, worth a total of 30 points. Answer all questions in the exam book. You may keep the exam question sheet. No calculators, notes, or books are allowed. 1

COMP 250 1. (3 points) (a) Convert the decimal number 231 to binary. (b) What is the value of the hash function, h(i) = i mod 16 for i = 231? You may express your answer in binary. (c) How many multiplications are used to compute 13 231 using the recursive method discussed in class i.e. power(13,231)? Note: The right side of 13 231 = 13 115 13 115 13 is two multiplications. 2. (2 points) State the formal definition of f(n) is O(g(n)) and use it to prove f(n) is O(g(n)) for the following: f(n) = (n + 237) 3 + 4n log n + 3 Hint: You may assume log n n for all n. g(n) = n 3. 3. (2 points) (a) Show the final state of a queue, after the following sequence of operations is finished: add(a), add(b), add(c), add(d), remove(), add(e), add(f), remove(), remove(), add(g), add(h) Assume the queue is initially empty. Be sure to indicate the front of the queue. (b) Answer the same question as (a), but now use a stack instead of a queue. Here, indicate the top of the stack. 2

COMP 250 4. (3 points) State without proof the solution of the following recurrence relations. For (a)-(d), assume t(0) = 1. For (e),(f), assume t(1) = 1 and n is a power of 2. (a) t(n) = 1 + t(n 1) (b) t(n) = c + t(n 1) (c) t(n) = n + t(n 1) (d) t(n) = c t(n 1) (e) t(n) = c + t( n) 2 (f) t(n) = n + 2 t( n) 2 5. (2 points) Consider a mergesort algorithm that partitions a set of n comparable elements into three equal-size subsets, sorts each of them, and then merges the three sorted subsets. You may assume n is a power of 3. (a) Give a recurrence relation that describes the number of operations used for this algorithm as a function of n. (b) Solve this recurrence relation. Be sure to show the steps used to obtain your solution. 6. (3 points) Consider the following array of characters: which defines a min-heap. minheap = [b, c, m, h, d, p, w] (a) What is the result of applying the heap s removemin() operation? Your answer must be a new array with six elements. (b) Give a binary search tree of height 2 that contains the original characters of minheap, i.e. before the operation in (a). (c) Apply the binary search tree operation remove(h) to your tree in (b). 3

COMP 250 7. (5 points) (a) Using pseudocode, write a recursive algorithm findroot(node), whose input is a node in a tree and whose output is the root node of the tree. For example, in the trees below, the input could be any node and the output would always be node a. Assume that each node in the tree has a field parent, and that the input node is valid, i.e. it references a node in the tree. (b) Consider an algorithm that transforms a tree such that all non-root nodes in the path from the input node to the tree root will have depth 1 in the transformed tree. The algorithm is called using: flatten(node, root). An example of the transformation is shown below (left to right). For this example, what is the input node that explains the transformation? (c) Using pseudocode, write a recursive algorithm flatten(.. node and root are valid. ). Assume that the inputs (d) What might be the advantage of calling flatten after you call findroot? a a b c d b c d g i e f g h e f h j i j 4

8. (3 points) COMP 250 The figure below shows a set of rectangles whose overlap relationships can be represented using a directed graph. Each rectangle is represented in the graph by one vertex, and whenever one rectangle overlaps another rectangle, there is a directed edge. For the example below, the graph would contain the edges {(C, G), (C, E),..., etc. F G GH A D C B E (a) Give the adjacency list for this graph, such that the vertices are ordered alphabetically. (b) Give the ordering of vertices visited in a breadth first traversal of the graph, starting from vertex C. NOTE: In this question and the next, there is only one answer since you must order the vertices alphabetically. (c) Give the ordering of vertices visited in a depth first traversal, again starting from C. 5

COMP 250 9. (7 points) (a) What is the difference between overriding and overloading? (b) When you write an equals() and hashcode() method for a class, what (if any) are the required relationships between the outputs of these method? Next, consider the Java code below. This code does not do anything meaningful. Rather, it is used to test your understanding of inheritance and polymorphism. Which (if any) of the instructions (c)-(g) of Test generate compiler errors? What is the output of the program after removing those lines (if any) that cause compilation errors? Be sure to mark which instructions produce which output. public class A { public int n = 3; A( ){ System.out.println("A"); // constructor public void foo() { System.out.println( n ); public class B extends A{ public int n; B() { // constructor B(int m){ // constructor System.out.println( "B" ); this.n = m; int foo(int n) { System.out.println(this.n) ; return n; public class Test { public static void main(string[] args) { int n = 2; A a = new A(); // (c) a.foo(); // (d) a.foo( 7 ); // (e) B b = new B( 11 ); // (f) n = b.foo( 4 ); // (g) 6

Solutions 1. (a) 11100111 (b) 0111 (or 7). (c) This one was not an easy question. 13 231 = 13 115 13 115 13 13 115 = 13 57 13 57 13 13 57 = 13 28 13 28 13 13 28 = 13 14 13 14 13 14 = 13 7 13 7 13 14 = 13 7 13 7 13 7 = 13 3 13 3 13 3 = 13 13 13 So, you need 13 multiplications (pure coincidence that the number happens to be 13). Marking scheme: 1 point for each of (a),(b),(c). If part (a) was wrong, and part (b) had the last 4 bits of your answer in part (a), then I gave the point. Part (c) was difficult, and we gave the point if you gave an answer close to 13 and explained your reasoning. 2. We want to show there exist n 0 > 0 and c > 0 such that, for all n > n 0, We try to find an upper bound: (n + 237) 3 + 4n log n + 3 < cn 3. (n + 237) 3 + 4n log n + 3 < (n + 237n) 3 + 4n 3 + 3n 3, for all n > 0 So take c = 238 3 + 4 + 3 and n 0 = 1. = (238 3 + 4 + 3)n 3 Marking scheme: 1 point for the definition. You needed to provide quantifiers ( for all.., there exists... ). You lost 0.5 points out of 1 for each quantifier you left out. 1 point for a correct proof in (b). There are other ways to do it. I provided the one I think is the simplest. 3. (a) d,e,f,g,h (b) a,b,c,g,h (with h on top) One point for each. 4. (a) t(n) = n + 1 (b) t(n) = nc + 1 7

(c) (d) t(n) = n(n+1 + 1 2 t(n) = c n (e) t(n) = c log n + 1 (f) t(n) = n log n + n 0.5 points for each, and we ignored the 1 s. i.e. you got the 0.5 point even if you left them out. 5. (a) This is similar to 4(f), but now we split into 3 rather than 2. t(n) = 3t( n 3 ) + n. (b) Each of the three sets of size n 3 need to be merged (n steps). needs to be sorted, and then the three (now) sorted sets 6. (a) c d m h w p (b) h / \ c p / \ / \ b d m w (c) Either of the following are ok: t(n) = n + 3t( n 3 ) = n + 3( n 3 + 3t(n 9 )) = n + n + 9 t( n 9 )) = 2n + 3 2 t( n 3 2 )) = : = n log 3 n + 3 log 3 n n t( 3 ) log 3 n = n log 3 n + nt(1) = n log 3 n + n m d / \ / \ c p c p / \ \ / / \ b d w b m w 8

7. (a) findroot(node){ if (node.parent == null) return node else return findroot(node.parent) Many students left out the second return statement but no marks were taken off. (b) The input node is i. Some students interpreted the question incorrectly and forgot that the two end vertices of the path belong to the path. Such students answered j. From Assignment 4, it should have been very clear in your mind that a path from a node to the root includes both the node and the root. We were generous and gave 0.5 points instead of 1 point if you wrote j, provided that the algorithm you gave was correct for the (mis)interpretation. (c) flatten(node,root){ if (node!= root) { flatten( node.parent, root) node.parent = root Many students used a temporary variable to point to node.parent (like when swapping two elements) but wrote Java commands like Node tmp = new Node(); This is nonsense. First, the question asks for pseudocode, not Java code. Second, even if it did ask for Java code, there is no need to create new nodes. Such students typically lost a point for writing this. (And if you do this in COMP 251, you will be penalized more heavily.) This part was worth 2 points. (Note: some students who wrote j in (b) gave an algorithm here that would yield j at depth 1, which is inconsistent with the original question. (d) The point of this question was that the next time you want to find the root of the input node, you will only have to follow one reference, since the node is at depth 1. This question was interpreted in so many different ways that we threw out the question (and gave everyone the point). Marking scheme: 1 point for (a,b,d). 2 points for c. 8. Some students misinterpreted overlap as a symmetric relation: A overlaps B is the same as B overlaps A. This would (incorrectly) give you an undirected graph. Note: a similar question was in the graph exercises, so students who got this wrong evidently did not do those exercises. Anyhow, we decided to only take 1 mark off for this (out of 3), since part of the question was to determine whether you know how to do a depth-first and breadth first search, and you can demonstrate this even if you misinterpreted the question as just described. Here I give solutions for the correct interpretation only. 9

(a) A - B B - C - E, F, G D - E E - H F - D G - D H - B, F (b) C, E, F, G, H, D, B (c) C, E, H, B, F, D, G Marking scheme: 1 point for each of (a-c). 9. (a) Overriding means that a method in a child class has the same signature as a method in a parent class. Overloading is when multiple methods share the same name but have different signatures. (These could be in the same class or different classes). 0.5 points for each. The key is to say in which ones the signatures are same or different. (b) If a.equals(b) is true, then a.hashcode() == b.hashcode() is true. (Equivalently, if a.hashcode() == b.hashcode() is false, then a.equals(b) is false. If a.equals(b) is false, then we cannot say anything about a.hashcode() == b.hashcode(), i.e. the latter could be true or false. See lecture 29, page 4. The compilation error occurs in (e). The problem is that the A class has no foo method with an argument, and a is declared to be of class A. Output: A from (c) 3 from (e) A from (f) <--- many student missed this (and lost 0.5 points) B from (f) 11 from (g) Marking scheme: 7 points in total. 1 point for each of (a)-(g). 10