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

Similar documents
Faculty of Science FINAL EXAMINATION

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

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

Exam Duration: 2hrs and 30min Software Design

Give one example where you might wish to use a three dimensional array

Inheritance and Interfaces

COMP 250 Fall inheritance Nov. 17, 2017

Questions Answer Key Questions Answer Key Questions Answer Key

Section 05: Solutions

Object-Oriented Concepts

Section 05: Solutions

Lecture 5: Methods CS2301

Examination Questions Midterm 1

COMP 250 Winter 2011 Reading: Java background January 5, 2011

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Practice for Chapter 11

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

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

AP Computer Science Summer Work Mrs. Kaelin

Class, Variable, Constructor, Object, Method Questions

Computer Science II (20082) Week 1: Review and Inheritance

COMP 250. Lecture 6. doubly linked lists. Sept. 20/21, 2017

TA office hours are over after this week, but Dan and Maja will still be around for the next month

Pace University. Fundamental Concepts of CS121 1

CSE 142 Su01 Final Exam Sample Solution page 1 of 7

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

CS 455 Midterm 2 Spring 2018 [Bono] Apr. 3, 2018

Nested Loops. A loop can be nested inside another loop.

AP Computer Science Unit 1. Writing Programs Using BlueJ

CS 314 Exam 2 Spring

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Computer Science II (20073) Week 1: Review and Inheritance

AP Computer Science Unit 1. Programs

COMP 250 Winter generic types, doubly linked lists Jan. 28, 2016

Questions Answer Key Questions Answer Key Questions Answer Key

McGill University School of Computer Science COMP-202A Introduction to Computing 1

COMP 250 Midterm #2 March 11 th 2013

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Java Fundamentals (II)

Computer Science 1 Ah

1.00 Introduction to Computers and Engineering Problem Solving Quiz 1 March 4, 2005

Introduction to Java Unit 1. Using BlueJ to Write Programs

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

Last Name: Circle One: OCW Non-OCW

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

CSE 143 Final Part 1, August 18, 2011 Sample Solution

BM214E Object Oriented Programming Lecture 8

Chapter 3 Function Overloading

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

AP Computer Science Unit 1. Writing Programs Using BlueJ

QUEEN MARY, UNIVERSITY OF LONDON DCS128 ALGORITHMS AND DATA STRUCTURES Class Test Monday 27 th March

Inf1-OOP. OOP Exam Review. Perdita Stevens, adapting earlier version by Ewan Klein. March 16, School of Informatics

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

CS18000: Problem Solving and Object-Oriented Programming

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

CS 455 Final Exam Fall 2012 [Bono] Dec. 17, 2012

What does this print?

CS 455 Midterm Exam 2 Fall 2016 [Bono] November 8, 2016

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;

Chair of Software Engineering. Languages in Depth Series: Java Programming. Prof. Dr. Bertrand Meyer. Exercise Session 3

Prelim 1 Solutions. CS 2110, March 10, 2015, 5:30 PM Total Question True False. Loop Invariants Max Score Grader

COMP 250 Fall Recursive algorithms 1 Oct. 2, 2017

Topic 10: The Java Collections Framework (and Iterators)

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ June Exam

Questions Answer Key Questions Answer Key Questions Answer Key

1 Inheritance (8 minutes, 9 points)

ESC101 : Fundamental of Computing

CIS 265/506 Exam1 Spring 2012 Prof. V. Matos Exam Last Name First Name:

Inf1-OP. Inf1-OP Exam Review. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. March 20, School of Informatics

Chapter 4 Defining Classes I

TeenCoder : Java Programming (ISBN )

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

Interfaces & Generics

Algorithm Analysis. CENG 707 Data Structures and Algorithms

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

CMSC 132: Object-Oriented Programming II. Inheritance

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

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

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) Midterm Examination

CS100B: Prelim 3 Solutions November 16, :30 PM 9:00 PM

Fall 2017 Mentoring 9: October 23, Min-Heapify This. Level order, bubbling up. Level order, bubbling down. Reverse level order, bubbling up

1B1b Classes in Java Part I

INSTRUCTIONS TO CANDIDATES

Midterm Examination COMP 304B 2004: Object-oriented Design

AP CS Unit 7: Interfaces Exercises Assume all code compiles unless otherwise suggested.

Activity 4: Methods. Content Learning Objectives. Process Skill Goals

Java Comparable interface

Algorithm Efficiency and Big-O

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

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

AP CS Unit 7: Interfaces Exercises 1. Select the TRUE statement(s).

COMP 250 Fall recurrences 2 Oct. 13, 2017

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

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc.

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

Implements vs. Extends When Defining a Class

Transcription:

QUIZ 2 Introduction to Computer Science (COMP 250) Mon. March 2, 2009 Professor Michael Langer STUDENT NAME: ID: The exam consists of five questions. There are a total of 10 points. You may use the back of the sheet, if necessary. No electronic devices (calculators) or notes are allowed. 1. (2 points) (a) State the formal definition of f(n) is O(g(n)). (b) Let f(n) = n 2 + 3n + 5. Use the definition of (a) to show that f(n) is O(n 2 ). (one point for each) (a) There exist two constants c and n 0 such that f(n) cg(n) for all n > n 0. We only gave 0.5 points if the quantifiers are ambiguous. For any c,.. vs There exists a c,... mean very different things. (b) f(n) n 2 + 3n 2 + 5n 2 = 9n 2 for n 1, so c = 9, n 0 = 1 works. Many students derived an answer by dividing both sides of n 2 + 3n + 5 cn 2 by n 2 to get 1 + 3 n + 5 n 2 c and observed that c = 9 works using essentially the same idea as above. Some students, however, concluded that c = 1 works. It doesn t. For example, f(1) = 9 which is not less that 1.

2. (2 points) Give the output of the Tower of Hanoi algorithm for the following: move a tower of height n = 3 from position start = 1 to position finish = 2. It is sufficient for you to give a sequence of (start,finish) pairs, indicating the move at each step, where start and finish are in {1, 2, 3. (1,3) (2,3) (3,1) (3,2) If the solution you gave has more than 7 steps but works (it obeys the constraint that you never have largeer on smaller, and you get all the disks from 1 to 2), then you get 1 point. You need to use 7 steps because the question specifically asks for the Tower of Hanoi algorithm (which uses 7 steps for n = 3).

3. (2 points) State a recurrence relation for the running time t(n) of: (a) decimal to binary conversion (of a positive integer n) (b) mergesort (you may assume the number of elements n is a power of 2) You will not get extra points for solving the recurrences. (1 point for each) (a) We ignored floors and ceilings and accepted anything of the form: t(n) = c + t( n 2 ). (b) t(n) = n + 2t( n 2 ) We gave 0.5 points if you wrote t(n) = 1 + 2t( n 2 ) or t(n) = n + t( n 2 ).

4. (2 points) Consider the Java code: public void display( ArrayList<E> list ){ E e; for (int i = 0; i < list.size(); i++){ e = list.get(i); System.out.println( e.tostring() ); (a) Give an expression for the running time t(n) of this method, where n is the number of elements in the list, i.e. the value returned by method size(). You may write 1 for any operation that takes constant time. (b) Would your answer change if list were declared to be type LinkedList<E>? If so, how? If not, why not? (a) get(i) can be done in constant time for an array and e.tostring() is constant time too. We loop through a list of n elements. Thus, It is fine if you write t(n) = cn t(n) O(n) [If you were not given this mark, let me know.] (b) For a linked list, get(i) requires time proportional to the size of the list. Thus, n(n + 1) t(n) = 1 + 2 + 3 + + n = 2 which is Θ(n 2 ). You get the point if you note that you need to search from the beginnning of the list each time and that this makes it Θ(n 2 ).

5. (2 points) (a) What is the output of the code below, when you run class B? Hint: the output will not change if you rename the s variable within classes A or B. (b) Can the variable v in main be declared as type I or A? Briefly explain. public interface I { public String m(); public abstract class A implements I{ private String s = "A: "; public String m(string s_arg){ return s + s_arg; public class B extends A{ private String s = "B: "; public String m() { return s; public static void main(string[] args){ B v = new B(); System.out.println( v.m( " test ")); (a) A: test Why? The m() method from class A must be used here since the method m in main is passed a String argument and no such method signature exists in B. Thus, B must inherit this method from its superclass A. Without the hint, you might have been confused as to whether or not inheritance implies that the m(string..) code from A also belongs to class B. With the hint, you should see that the code does not belong to B. Thus, class A s m method is used, and this refers to the string s from class A. (b) The object referenced by variable v invokes an m method with String argument. This method is found in A so there would be no problem in declaring v to be type A. However, there is only one m method in interface I and this does not have a String argument. Thus, the compiler gives an error.