CS 307 Midterm 2 Spring 2008

Similar documents
CS 307 Midterm 2 Fall 2008

CS 307 Midterm 2 Fall 2009

CS 307 Midterm 2 Spring 2011

CS 307 Midterm 2 Fall 2010

CS 314 Exam 2 Spring

CS 314 Midterm 2 Spring 2013

CS 314 Midterm 2 Fall 2012

CS 314 Midterm 1 Fall 2011

CS 314 Exam 2 Spring 2018

CS 314 Midterm 1 Fall 2012

Points off Total off Net Score. CS 314 Final Exam Spring 2016

CS 307 Final Spring 2009

CS 314 Exam 2 Fall 2017

CS 314 Final Fall 2012

Points off A 4B 5 Total off Net Score. CS 314 Final Exam Spring 2015

CS 314 Exam 1 Spring 2018

CS 307 Midterm 1[corrected] Spring 2008

CS 314 Exam 1 Fall 2016

CS 307 Final Fall 2009

CS 307 Final Spring 2008

CS 307 Final Spring 2011

Points off Total off Net Score. CS 314 Final Exam Fall 2016

CS 314 Midterm 1 Spring 2014

CS 314 Exam 1 Fall 2017

CS 314 Exam 1 Spring 2017

CS 314 Exam 2 Spring 2016

CS 307 Final Spring 2010

CS 307 Midterm 1 Spring 2009

Points off Total off Net Score. CS 314 Final Exam Spring 2017

Points off Total off Net Score. CS 314 Final Exam Spring Your Name Your UTEID

CS 307 Midterm 1 Fall 2007

CS 314 Final Fall 2011

CS 314 Exam 1 Spring 2015

CS 314 Exam 1 Spring 2016

CS 314 Final Spring 2013 SOLUTION - SOLUTION - SOLUTION - SOLUTION - SOLUTION - SOLUTION - SOLUTION

CS 312 Final Fall 2016

Week 4, Wednesday (Spring 2015). Dr. Yoder. Sec 051. Page 1 of 5

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

CS 312 Midterm 2 Fall 2013

CS 312 Final Fall 2013

Topic 11 Linked Lists

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

Midterm Exam 2 CS 455, Fall 2014

1. Expressions - 1 point each. -1 for missing or extra " OR.0. differences in capitalization of true and false okay. No limit on points off.

University Interscholastic League. Computer Science Competition

Lecture 13: Two- Dimensional Arrays

Final Examination CS 125 Introduction to Computer Science Fall Hours

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

CS 455 Midterm 2 Fall 2017 [Bono] Nov. 7, 2017

CSE 143, Winter 2010 Midterm Exam Wednesday February 17, 2010

CSE 143 SAMPLE MIDTERM SOLUTION

Midterm Exam 2 CS 455, Spring 2011

Midterm Exam 2 CS 455, Spring 2014

CSE 142 Su01 Final Exam Sample Solution page 1 of 7

LINKED LISTS cs2420 Introduction to Algorithms and Data Structures Spring 2015

Outline. runtime of programs algorithm efficiency Big-O notation List interface Array lists

CS 200 Objects and ArrayList Jim Williams, PhD

CS 312 Final Fall Your Name SOLUTION SOLUTION SOLUTION SOLUTION SOLUTION. Your UTEID SOLUTION SOLUTION SOLUTION SOLUTION SOLUTION _

CS 312 Final Fall 2015

CS 305j Midterm 2 Fall 2006

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

CS 312 Exam 2 Fall KG Kris Megan Roman Sonika

Final Exam. COMP Summer I June 26, points

Array Based Lists. Collections

Implementing a List in Java. CSE 143 Java. Just an Illusion? List Interface (review) Using an Array to Implement a List.

Computer Science 136. Midterm Examination

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

Solution to Test of Computer Science 203x Level Score: / 100 Time: 100 Minutes

Prelim 1. CS2110, October 2, 2014, 5:30 PM Extra Total Question TrueFalse Multiple Object Oriented

You must bring your ID to the exam.

Question Points Score

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

CIS 110 Introduction to Computer Programming Spring 2016 Final Exam

CS 455 Final Exam Spring 2015 [Bono] May 13, 2015

CSE 143: Computer Programming II Spring 2015 Midterm Exam Solutions

CIS 110 Introduction to Computer Programming. February 29, 2012 Midterm

Implementing a List in Java. CSE 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List.

CSE 143 Sample Midterm Exam #4

Question: Total Points: Score:

1. ArrayList and Iterator in Java

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

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

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

Sample Midterm Exam #2

Arrays and ArrayLists. David Greenstein Monta Vista High School

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

Introduction to Computer Science Midterm 3 Fall, Points

CSE 143 SAMPLE MIDTERM

I. True/False: (2 points each)

Programming II (CS300)

COM S 211/ENGRD 211 May 15, 2003

COMP103 Test. 5 Sept, 2007

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

CS314 Exam 2 - Spring Suggested Solution and Criteria 1

Arrays. COMS W1007 Introduction to Computer Science. Christopher Conway 10 June 2003

I. True/False: (2 points each)

1. Answer as shown or -1 unless question allows partial credit. No points off for minor differences in spacing, capitalization, commas, and braces.

Spring 2012 Final Examination 3 hours CS 125 Introduction to Computer Science

Arrays.

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

Transcription:

Points off 1 2 3 4 Total off Net Score Exam Number: CS 307 Midterm 2 Spring 2008 Name UTEID login name TA's Name: Mario Ruchica Vishvas (Circle One) Instructions: 1. Please turn off your cell phones and other electronic devices. 2. There are 4 questions on this test. 3. You have 2 hours to complete the test. 4. You may not use a calculator on the test. 5. When code is required, write Java code. 6. When writing methods, assume the preconditions of the method are met. 7. In coding question you may add helper methods if you wish. 8. After completing the test please turn it in to one of the test proctors and show them your UTID. 1. (2 points each, 30 points total) Short answer. Place you answers on the attached answer sheet. If the code contains a syntax error or other compile error, answer compile error. If the code would result in a runtime error / exception answer Runtime error. If the code results in an infinite loop answer Infinite loop. Recall that when asked for Big O your answer should be the most restrictive correct Big O function. For example Selection Sort has an average case Big O of O(N^2), but per the formal definition of Big O it is correct to say Selection Sort also has a Big O of O(N^3) or O(N^4). I want the most restrictive correct Big O function. (Closest without going under.) A. What is the output of System.out.println( a(7) ); public int a(int n){ if( n > 10 ) return 1; else return 2 + a(n + 1); B. What is the output of System.out.println( b(4) ); public int b(int val){ if( val <= 1 ) return val; else return val + b(val - 1) + b(val - 2); CS 307 Midterm 2 Spring 2008 1

C. What is the output when method c is called? public void c(){ int[] data = {5, 3, 2, 6, 1, -5, 3; System.out.println( helper(data, 0) ); public int helper(int[] list, int p){ if( p == list.length - 1 ) return list[p]; else return Math.max( helper(list, p + 1), list[p] ); D. What is the worst case Big O of method d? N = nums.length. public int d(int[] nums){ int total = 0; for(int i = 0; i < nums.length; i++) for(int j = nums.length - 1; j >= 0; j--) if( nums[i] == nums[j] ) total++; return total; E. What is the Big O of method e? N = nums.length. public int e(int[] nums){ int total = 0; for(int i = 0; i < nums.length; i++) total += nums[i]; int limit = nums.length / 2; for(int j = 0; j < limit; j++) total += nums[j] * nums[j]; return total; F. What is the Big O of method f? N = mat.length // pre: mat is a square matrix, mat.length > 0 public int f(int[][] mat){ int row = 1; int col = 1; int total = 0; while( row < mat.length ){ total += mat[row][col]; row *= 3; col *= 3; return total; CS 307 Midterm 2 Spring 2008 2

G. What is the best case Big O of method g? N = numbers.length public static void g(int[] numbers) { int limit = numbers.length; for (int pass = 1; pass < limit; pass++) { for (int i = 0; i < limit - pass; i++) { if (numbers[i] > numbers[i + 1]) { int temp = numbers[i]; numbers[i] = numbers[i + 1]; numbers[i + 1] = temp; H. What is the Big O of method h? Method compute is O(N) where N is the length of the array sent as a parameter to the method. N = table.length. // table is a square matrix public int h(int[][] table){ int answer = 0; for(int row = 0; row < table.length; row++) for(int col = row; col < table.length; col++) answer += compute(table[row], row, col); return answer; I. What is the Big O of method helper? N = list.length. public int helper(int[] list, int p){ if( p == list.length - 1 ) return list[p]; else return Math.max( helper(list, p + 1), list[p] ); J. What is the Big O of the following code? N = list.size() public double empty(arraylist<string> list){ int total = 0; int size = list.size(); while( list.size()!= 0 ){ total += list.get(0).length(); list.remove(0); return 1.0 * total / size; CS 307 Midterm 2 Spring 2008 3

K. Consider the following methods from the Java ArrayList class. Method Summary void add(e o) Appends the specified element to the end of this list. void add(int index, E element) Inserts the specified element at the specified position in this list. E get(int index) Returns the element at the specified position in this list. E remove(int index) Removes the element at the specified position in this list. Return the element that is removed from the list. E set(int index, E element) Replaces the element at the specified position in this list with the specified element. Returns the element that was previously at index. int size() Returns the number of elements in this list. What is output when method k is called? public void k(){ ArrayList<String> lt = new ArrayList<String>(); lt.add( "C" ); lt.add(0, "A"); lt.add(1, "B"); lt.add(0, "D"); lt.set(1, "E" ); lt.add( lt.remove(2) ); for(int i = 0; i < lt.size(); i++) System.out.print( lt.get(i) ); L. What is output when method el is called? The LinkedList class implements the same methods as the ArrayList class. public void el(){ LinkedList<Integer> list = new LinkedList<Integer>(); for(int i = 0; i < 5; i++) list.add( i ); for(int j = list.size() - 2; j >= 0; j--) list.set( j, list.get(j) + list.get( j + 1 ) ); for(int i = 0; i < list.size(); i++) System.out.print( list.get(i) + " " ); CS 307 Midterm 2 Spring 2008 4

M. What is output when method m is called? Assume the Stack class implements a traditional stack. public void m(){ Stack<Integer> st = new Stack<Integer>(); for(int value = 10; value >= -3; value -= 2) st.push( value ); for(int i = 0; i < 3; i++) System.out.print( st.pop() + " " ); N. A method is O(N 3 ). It takes 1 second for the method to complete on a data set with 5,000 items. What is the expected time for the method to complete with a data set of 20,000 items? O. Method quicksort implements the Quicksort algorithm. It takes 10 seconds for the method to complete given a data set of 1,000,000 integers in random order. What is the expected time for the method to complete given a data set of 2,000,000 items in random order? P. (Extra Credit 1 point) If a computer account had the user id of student what would be the password? CS 307 Midterm 2 Spring 2008 5

2. (Implementing data structures, 25 points). Write an instance method for a LinkedList class named issorted. The method returns true if the data in the LinkedList is in sorted in ascending order, false otherwise. A LinkeList with 0 or 1 elements is considered to have its elements in sorted, ascending order. This LinkedList class uses singly linked nodes. Each node stores a single int. This LinkedList class contains a reference to the first node in the list and the last node in the list as well as a size variable. The last node in the list's next reference is set to null. When the list is empty first and last are set to null. Examples: [].issorted() returns true [12].isSorted() returns true [1, 1, 1].isSorted() returns true [1, 2, 5].isSorted() returns true [2, 4, 6, 5, 10, 11].isSorted() returns false [5, -1].isSorted() returns false You may not use any other methods in the LinkedList class, but you may create your own helper methods if you wish. You solution must be O(1) space. In other words you can use an auxiliary array or ArrayList. public class Node { public Node(int value, Node next) public int getdata() public Node getnext() public void setvalue(int value) public void setnext(node next) public class LinkedList { private Node first; // points to the first node in the list private Node last; // points to the last node in the list private int size; // size of list // when size == 0, first = last = null // pre: none // post: return true if the elements in this list are // sorted in ascending order, false otherwise public boolean issorted(){ // complete this method on the next page CS 307 Midterm 2 Spring 2008 6

// pre: none // post: return true if the elements in this list are // sorted in ascending order, false otherwise public boolean issorted(){ CS 307 Midterm 2 Spring 2008 7

3. (Implementing Data Structures 25 points) Consider an array based list class like the one we developed in class. The class is named GenericList. The class uses a native array of Objects as its internal storage container. The internal storage container may have extra capacity and thus its length will be greater than or equal to the length of the list that is being represented. Complete an instance method that swaps the first half of the list with the second half of the list. The relative order of elements in each half is unchanged. If the list has an odd number of elements the location of the middle element is not altered. Your method may not use any other methods in the GenericList class, but you may create your own helper methods if you wish. Your solution must be O(1) space. You may not use any other Java classes in your solution. Here are some examples of the expected behavior of the method on various lists: [].swaphalves() -> [] // An empty list is unchanged. [1].swapHalves() -> [1] // A list with one element is unchanged. [1, 2].swapHalves() -> [2, 1] // [5, 4, 7].swapHalves() -> [7, 4, 5] // Middle element unchanged. [5, 4, 7, 8].swapHalves() -> [7, 8, 5, 4] // Notice relative order [5, 4, 7, 8, 3].swapHalves() -> [8, 3, 7, 5, 4] [5, 4, 7, 8, 3, 12].swapHalves() -> [8, 3, 12, 5, 4, 7] public class GenericList{ private Object[] mycontainer; // internal storage container // mycontainer is never set to null private int size; // number of elements in list // pre: none // post: first and second halves of the list are swapped // per the question statement. public void swaphalves(){ // Complete this method on the next page. // // Complete this method on the next page. // // Complete this method on the next page. // // Complete this method on the next page. // // Complete this method on the next page. // // Complete this method on the next page. CS 307 Midterm 2 Spring 2008 8

// pre: none // post: first and second halves of the list are swapped // per the question statement. public void swaphalves(){ CS 307 Midterm 2 Spring 2008 9

4. (Recursion, 20 points) Assume a 2 dimensional array of integers is used to represent a map of the plants that make up a forest. Each cell of the 2d array represents one unit of area. (The actual units, square meters, square feet, is irrelevant for this problem.) The integer represents the dominate plant growing in that cell (or unit area). Write a method that, given a starting location in the map, determines how large a contiguous (connected) area the dominate plant at the specified cell occupies. A given cell has at most four neighboring cells: the cells above, below, to the left, and to the right. These correspond to the directions north, south, east and west. Diagonal cells are not considered to be connected. Here is an example. Assume we have the following 5 by 6, two dimensional array of ints, representing a map of a forest. Each int is a code for the dominate plant in that cell of the forest. 0 1 2 3 4 5 0 2 5 2 5 9 1 1 5 2 2 5 5 5 2 5 6 7 5 9 9 3 1 5 7 5 7 5 4 5 5 5 5 5 5 row indices column indices If the specified coordinates are row 0 and column 0 the dominate plant in that cell is specified by a 2. The cell is a special case because it only has 2 neighbors, the cells to the right and below it. Neither of these cells (at (1,0) and (0,1) ) contain a 2, so the result here is 1. If the specified coordinates are row 0 and column 2 the dominate plant in that cell is again a 2. This cell has 3 neighboring cells. The cell to the south at (1, 2) also contains a 2. That cell, at (1, 2), has 4 neighboring cells. The one to the north at (0, 2) has already been visited so don't count that one again. But the cell to the west at (1, 2) has not been visited and has a 2 in it as well. Thus the cell starting at location (0, 2) is part of a contiguous area of 3 cells that contain the same plant. The result would be 3. The largest contiguous area in the example is outlined below. 0 1 2 3 4 5 0 2 5 2 5 9 1 1 5 2 2 5 5 5 2 5 6 7 5 9 9 3 1 5 7 5 7 5 4 5 5 5 5 5 5 row indices column indices CS 307 Midterm 2 Spring 2008 10

Here are some expected results if map equals the 2d array on the previous page. (Do not hard code a solution to the example map!) sizeofplantarea(map, 0, 0) -> 1 sizeofplantarea(map, 0, 1) -> 1 sizeofplantarea(map, 1, 0) -> 2 sizeofplantarea(map, 0, 2) -> 3 sizeofplantarea(map, 4, 0) -> 14 Hint: You may find it useful to create a helper method and use the specified method to kickoff the recursion. Complete the following method: // pre: map!= null, map is a rectangular matrix // row and col are both inbounds // post: Return the size of the contiguous growth that the // starts at the cell specified by row and col. // The elements of map are not altered as a result // of this method call. public int sizeofplantarea(int[][] map, int row, int col){ CS 307 Midterm 2 Spring 2008 11

// more room for question 4 if necessary CS 307 Midterm 2 Spring 2008 12

Scratch Paper CS 307 Midterm 2 Spring 2008 13

Name: TAs name: Answer sheet for question 1, short answer questions A. I. B. J. C. K. D. L. E. M. F. N. G. O. H. P. (extra credit) CS 307 Midterm 2 Spring 2008 14