Question 1. [5 points] What output is printed by the following program (which begins on the left and continues on the right)?

Similar documents
Question 2. [5 points] State a big-o upper bound on the worst case running time of the given

CS 201, Fall 2016 Dec 2nd Exam 3

CS 201, Fall 2016 Oct 28th Exam 2. Question 1. [5 points] Consider the following static method:

CS 201, Fall 2016 Sep 28th Exam 1

public class Q1 { public int x; public static void main(string[] args) { Q1 a = new Q1(17); Q1 b = new Q1(39); public Q1(int x) { this.

Project #1 rev 2 Computer Science 2334 Fall 2013 This project is individual work. Each student must complete this assignment independently.

public static<e> List<E> removeoccurrences(list<e> origlist, E remove) {

Project 1 Computer Science 2334 Spring 2016 This project is individual work. Each student must complete this assignment independently.

Project #1 Computer Science 2334 Fall 2008

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

C Sc 227 Practice Final Summer 13 Name 200 pts

DUKE UNIVERSITY Department of Computer Science. Test 1: CompSci 100

Input from Files. Buffered Reader

C Sc 227 Practice Final Summer 12 Name 200pt

C Sc 227 Practice Test 2 Section Leader Your Name 100pts. a. 1D array b. PriorityList<E> c. ArrayPriorityList<E>

Question 2. [12 points] Write a sequence of statements that does the following:

1. Is it currently raining in Tucson (4pts) a) Yes b) No? c) Don't know d) Couldn't know (not in Tucson)

COMP-202: Foundations of Programming. Lecture 13: Recursion Sandeep Manjanna, Summer 2015

Lecture 4: Exceptions. I/O

Assignment: TwoMethods Submitted to WebCat

CSE143 Midterm Summer Name of Student: Section (e.g., AA): Student Number:

Midterm Exam 2 CS 455, Fall 2014

This exam is open book. Each question is worth 3 points.

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

CS165 Practice Final Exam

AP CS Unit 7: Interfaces. Programs

Computer Science 1 Ah

CS Week 11. Jim Williams, PhD

CS 314 Midterm 2 Spring 2013

Question 0. (1 point) Write the correct ID of the section you normally attend on the cover page of this exam if you have not already done so.

CS 307 Midterm 2 Fall 2009

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

Computer Science E-119 Fall Problem Set 1. Due before lecture on Wednesday, September 26

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

I. True/False: (2 points each) On your bubble form fill out a for true and b for false.

Building Java Programs

CS165 Practice Final Exam Answer Key

CS 307 Midterm 2 Spring 2008

G51PGP Programming Paradigms. Lecture OO-4 Aggregation

Data Structures Brett Bernstein. Lecture 1: Introduction and a Review/Enrichment of CS 101

9. Java Errors and Exceptions

You will not be tested on JUnit or the Eclipse debugger. The exam does not cover interfaces.

Computer Science E-119 Practice Midterm

CSE 1223: Exam II Autumn 2016

You must bring your ID to the exam.

University of Palestine. Mid Exam Total Grade: 100

Searching in General

File IO. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 20

Informatik II. Andreas Bärtschi, Andreea Ciuprina, Felix Friedrich, Patrick Gruntz, Hermann Lehner, Max Rossmannek, Chris Wendler FS 2018

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

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

CS 200 File Input and Output Jim Williams, PhD

AP COMPUTER SCIENCE A DIAGNOSTIC EXAM. Multiple Choice Section Time - 1 hour and 15 minutes Number of questions - 40 Percent of total grade - 50

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Consider the following class (which begins on the left and continues on

Fundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm

Implement factorial to return n! that is defined as n * n-1 * n-2 * n-3 * 2 * 1. 1! And 0! Are both defined as 1. Use recursion, no loop.

Final Exam. COMP Summer I June 26, points

Recursion. Chapter Simple Recursion. Goals Trace recursive algorithms Implement recursive algorithms

CSCI-140 Midterm Review October 10, 2015 Presented by the RIT Computer Science Community

Lambda-Related Methods Directly in Lists and Maps

There are three questions on this exam. You have 2 hours to complete it. Please indent your program so that it is easy for the grader to read.

CSE 143 Section Handout #13 Practice Midterm #5

6. Java Errors and Exceptions. Errors, runtime-exceptions, checked-exceptions, exception handling, special case: resources

6. Java Errors and Exceptions

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

Exception Handling CSCI 201 Principles of Software Development

CS61B Lecture #3. Homework: Please see Homework #1 on the lab page. Last modified: Wed Sep 10 20:34: CS61B: Lecture #3 1

CSC122A, Spring 2005 Computer Science II Final Exam

ICOM 4015 Advanced Programming Laboratory. Chapter 1 Introduction to Eclipse, Java and JUnit

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

1. What is the difference between a compiler and an interpreter? Also, discuss Java s method.

ArrayList, Hashtables, Exceptions, and Files

CS 170 Exam 2. Version: A Fall Name (as in OPUS) (print): Instructions:

cd h: mkdir -p CS101 cd CS101 curl -O unzip zipfile cd CS101_Exam4

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

7. Java Input/Output. User Input/Console Output, File Input and Output (I/O)

CS211 Computers and Programming Matthew Harris and Alexa Sharp July 9, Boggle

CS 101, Spring 2016 March 22nd Exam 2

Midterm Logistics. Midterm tonight, 7PM 10PM Exam location by last name:

Computer Science E-119 Fall Problem Set 3. Due before lecture on Wednesday, October 31

CS 340 Spring 2019 Midterm Exam

CS 314 Final Fall 2012

CSE 331 Spring 2018 Midterm

1. (9 points) Recall the definition of the ListNode class: public class ListNode { int data; ListNode next; }

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

CS126 Final Exam Review

CS 206 Introduction to Computer Science II

CS 61B Discussion 5: Inheritance II Fall 2014

Priority queues. Priority queues. Priority queue operations

CSE 143 Sp03 Midterm 2 Sample Solution Page 1 of 7. Question 1. (2 points) What is the difference between a stream and a file?

Instructions. This exam has 7 questions, worth 10 points each. You have 50 minutes.

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

Simple File Input And Output

Midterm Exam 2 CS 455, Spring 2015

Simple File Input And Output

1.204 Lecture 7. Data structures: graphs, sets. Graphs and Networks. Graphs can be directed or undirected <0, 1> (0, 1) <1, 0>

CS 101, Spring 2014 April 1st Exam 2 Question 1. [3 points] What output is printed by the following code?

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2012

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

Transcription:

CS 201, Summer 2015 August 13th Final Exam Name: Question 1. [5 points] What output is printed by the following program (which begins on the left and continues on the right)? public class Q1 { public static void f( String a, String b) { String temp = a; a = b; b = temp; public static void main( String[] args) { String p = "Boy"; String q = "Howdy"; f(p, q); System.out.println(p + " " + q); Question 2. [5 points] What output is printed by the following program (which begins on the left and continues on the right)? public class Q2 { public String s; public Q2(String s) { this.s = s; public static void g( Q2 a, Q2 b) { String tmp = a.s; a.s = b.s; b.s = tmp; public static void main( String[] args) { Q2 x = new Q2("Oh"); Q2 y = new Q2("Yeah"); g(x, y); System.out.println( x.s + " " + y.s);

Question 3. [5 points] What output is printed by the following program (which begins on the left and continues on the right)? public class Q3 { public static void f( int[] a, int [] b) { int[] tmp = a; a = b; b = tmp; public static void main( String[] args) { int[] x = new int[]{1, 2; int[] y = new int[]{3, 4; f(x, y); System.out.printf("%d %d\n", x[0], y[0]); Question 4. [5 points] Complete the following static method. It should return the index of the last occurrence of val in the given array (arr). As a special case, if arr does not contain any elements equal to val, it should return -1. Here are some example JUnit tests (which assume that the findlastoccurrence method is in a class called Q4): String[] sarr = new String[]{"A", "B", "A", "C", "A", "B"; assertequals(5, Q4.findLastOccurrence(sarr, "B")); assertequals(4, Q4.findLastOccurrence(sarr, "A")); assertequals(3, Q4.findLastOccurrence(sarr, "C")); assertequals(-1, Q4.findLastOccurrence(sarr, "D")); Hint: Think about how to compare val to the elements of the array. public static<e> int findlastoccurrence(e[] arr, E val) {

Question 5. [5 points] Consider the following method: public static int countlinesinfile(string filename) { try { FileReader fr = new FileReader(fileName); BufferedReader br = new BufferedReader(fr); int count = 0; while (br.readline()!= null) { count++; br.close(); return count; catch (IOException e) { return -1; (a) Explain how it is possible that this method might open a file without closing it. (b) Explain how to modify the method so that the file is guaranteed to be closed (if it is opened).

Question 6. [5 points] What output is printed by the following code? Integer a = new Integer(42); Integer b = new Integer(42); if (a == b) { System.out.println("first"); if (a.equals(b)) { System.out.println("second"); Question 7. [5 points] What is the big-o upper bound on the worst-case running time of the following method? The problem size N is the length of the array passed as a parameter to the method. Explain your answer briefly. public static void int mystery(int[] a) { int sum = 0; for (int j = 0; j < a.length; j++) { for (int i = 0; i < a.length; i++) { if (a[i] * a[j] == 1000000) { return -1; else { sum += a[i] * a[j]; return sum;

Question 8. [5 points] Consider the following method (which begins on the left and continues on the right: public static void prependevens( List<Integer> list) { List<Integer> evens = new LinkedList<Integer>(); // get all even values // and put them in evens list for (Integer v : list) { if (v % 2 == 0) { evens.add(v); // prepend the even values // to the list for (Integer e : evens) { // add at index 0, prepending e to // become the first element of list list.add(0, e); Let the problem size N be the number of elements in the parameter (list). (a) What is the big-o upper bound of the running time if the list parameter is an instance of ArrayList? Explain briefly. (b) What is the big-o upper bound of the running time if the list parameter is an instance of LinkedList? Explain briefly.

Question 9. [5 points] Consider the following classes: public class IntBox { private int val; public IntBox(int val) { this.val = val; public int getval() { return val; public class IntPair extends IntBox { private int val2; public IntPair( int val, int val2) { TODO public int getval2() { return val2; Below, specify what code could be substituted for TODO so that the assertions in the following JUnit test code will succeed: IntPair p = new IntPair(17, 42); IntPair p2 = new IntPair(23, 79); assertequals(17, p.getval()); assertequals(42, p.getval2()); assertequals(23, p2.getval()); assertequals(79, p2.getval2()); Note that you are only specifying statements to replace TODO. You may not modify either class in any way.

Question 10. [5 points] Complete the following static method. It should return the number of elements of the given list which compare as either greater than the value max or less than the value min. The element type E is guaranteed to implement the Comparable interface. The following JUnit test demonstrates the method: Hints: List<Integer> list = Arrays.asList(41, 33, 26, 19, 34, 32, 32, 44, 19, 10); // Count number of elements greater than 35 or less than 15 // (should count just the elements 41, 44, and 10) int count = Q10.countElementsOutsideRange(list, 35, 15); assertequals(3, count); Think about how the Comparable interface will help you compare the list elements to the max and min values public static<e extends Comparable<E>> int countelementsoutsiderange(list<e> list, E max, E min) {

Programming Questions To get started, use a web browser to download the zipfile as specified by your instructor. Import it as an Eclipse project using File Import... General Existing Projects into Workspace Archive file. Important: You may use the following resources: The textbook The lecture notes posted on the course web page Your previous labs and assignments Do not open any other files, web pages, etc. Question 11. [10 points] In the class Q11, implement the findnthlargest method. Given an array list and an integer n, it should return the nth largest value in the array, as determined by the given comparator object. For example: if n is 1, then the overall largest value in the array should be returned, if n is 2, then the second-largest value should be returned, etc. Hint: Use either the Arrays.sort or Collections.sort method in your implementation. Validate your method by running the Q11Test class as a JUnit test. Make sure that all of the tests pass. Question 12. [15 points] In the class Q12, complete the definition of the generic method called collate. Given a List object, the method should rearrange its contents so that the elements with even indices are placed at the beginning of the list, and the elements with odd indices are placed at the end of the list. In other words, if a list containing elements A B C D E F is passed to the method, the list should be re-arranged so that the elements are A C E B D F. Hints/specifications: The list is generic, and the elements have type E The method must modify the list passed as the parameter Suggested approach: create a temporary list, places the result values in it (in the correct order), then clear the original list and add the result values to it The clear method will clear a list (cause it to become empty), and the addall can be used to add all values in a source collection to a destination collection Validate your method by running the Q12Test class as a JUnit test. Make sure that all of the tests pass. Question 13. [15 points] Using recursion, complete the definition of the method insertcommas in the class Q13. Given a string as a parameter, the method should return a string in which a comma is inserted between each pair of adjacent characters in the original string. For example, if the string Hello is passed to the method, it should return the string H,e,l,l,o.

Hints: Your solution must be recursion: do not use a loop Define an appropriate base case When solving a subproblem recursively, make sure it works towards a base case Think about how to extend the solution to the subproblem so that it is a solution to the overall problem The charat method returns the character at a specified index (0 for the first character in the string) The substring method returns a substring with all characters from a specified start index (inclusive) to a specified end index (exclusive); if the end index is omitted, the returned substring contains all characters from the start index to the end of the string Use string concatenation to build the result string Validate your method by running the Q13Test class as a JUnit test. Make sure that all of the tests pass.