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

Similar documents
You must bring your ID to the exam.

Com S 227 Spring 2013 Topics and review problems for Final Exam Wednesday, April 8, 7:00 9:00 pm in Hoover 2055 and 1213 ** You must bring your ID **

You do not have to memorize methods from the Java API. You should know

** You must bring your ID **

Com S 227 Spring 2018 Topics and review problems for Exam 1 Monday, February 19, 6:45 pm Exam locations by last name:

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

APCS Semester #1 Final Exam Practice Problems

You do not have to memorize methods from the Java API. You should know how to use String,

CSE 1223: Exam II Autumn 2016

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

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

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

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

CSCI 136 Data Structures & Advanced Programming. Lecture 14 Fall 2018 Instructor: Bills

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam

CS 314 Exam 2 Spring 2016

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

MERGESORT & QUICKSORT cs2420 Introduction to Algorithms and Data Structures Spring 2015

CSE 143: Computer Programming II Winter 2019 HW6: AnagramSolver (due Thursday, Feb 28, :30pm)

Final Examination CS 125 Introduction to Computer Science Fall Hours

ASSIGNMENT 5 Data Structures, Files, Exceptions, and To-Do Lists

Merge Sort. Algorithm Analysis. November 15, 2017 Hassan Khosravi / Geoffrey Tien 1

CSE 143, Winter 2009 Sample Midterm Exam #2

CSE143X: Computer Programming I & II Programming Assignment #9 due: Monday, 11/27/17, 11:00 pm

CIS Introduction to Computer Programming Spring Exam 1

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

SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 1 EXAMINATIONS 2015/2016 CI101 / CI177. Programming

CSE 143: Computer Programming II Summer 2017 HW5: Anagrams (due Thursday, August 3, :30pm)

Full file at

CS150 Sample Final. Name: Section: A / B

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

Object-oriented Programming and Software Engineering CITS1001. Multiple-choice Mid-semester Test

Lecture 11: Recursion (hard)

3D Graphics Programming Mira Costa High School - Class Syllabus,

CS150 Sample Final Solution

CS150 - Sample Final

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

About this exam review

The examination is open-book, and you may make use of any texts, handouts, or course notes. You may not, however, use a computer of any kind.

Arrays. Eng. Mohammed Abdualal

cs1114 REVIEW of details test closed laptop period

Pace University. Fundamental Concepts of CS121 1

Recursion. Recursion [Bono] 1

CISC 181 Lab 2 (100 pts) Due: March 7 at midnight (This is a two-week lab)

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

5. PLEASE TAKE HOME the question bundle, but turn in 2 paper sheets: The scantron AND the paper where you wrote your programming question solution!

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Chapter 7: Arrays CS 121. April 9, Department of Computer Science College of Engineering Boise State University. Chapter 7: Arrays CS / 41

CS 177 Week 15 Recitation Slides. Review

Arrays and ArrayLists. David Greenstein Monta Vista High School

CISC220 Lab 2: Due Wed, Sep 26 at Midnight (110 pts)

SEMESTER 1, 2011 EXAMINATIONS. CITS1200 Java Programming FAMILY NAME: GIVEN NAMES:

CSE 143 Section Handout #13 Practice Midterm #5

ASSIGNMENT 5 Objects, Files, and a Music Player

Name CIS 201 Midterm II: Chapters 1-8

CSE 143 Lecture 10. Recursion

CMPSCI 187 / Spring 2015 Postfix Expression Evaluator

AP COMPUTER SCIENCE A: SYLLABUS

AP Programming - Chapter 17 Lecture page 1 of 5

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

CISC 181 Lab 2 (100 pts) Due: March 4 at midnight (This is a two-week lab)

Introduction to Programming Using Java (98-388)

Computer Programming

COMP-202: Foundations of Programming. Lecture 26: Review; Wrap-Up Jackie Cheung, Winter 2016

CmpSci 187: Programming with Data Structures Spring 2015

Public-Service Announcement

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

(a) Assume that in a certain country, tax is payable at the following rates:

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

UMBC CMSC 331 Final Exam

CSE 143 Sample Midterm Exam #1

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009

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

CS2 Practical 1 CS2A 22/09/2004

LECTURE 17. Array Searching and Sorting

CS 2110 Summer 2011: Assignment 2 Boggle

CSE 143, Winter 2009 Final Exam Thursday, March 19, 2009

CSE 143. Lecture 9: introduction to recursion reading: 12.1

Practice Midterm Exam #1

CITS1001 week 4 Grouping objects

CSE373: Data Structure & Algorithms Lecture 18: Comparison Sorting. Dan Grossman Fall 2013

CS 200 Objects and ArrayList Jim Williams, PhD

CS201 Discussion 7 MARKOV AND RECURSION

Final Exam. COMP Summer I June 26, points

Bjarne Stroustrup. creator of C++

Lists. Arrays. CPSC 233: Introduction to Data Structures, Lists 1. James Tam. James Tam

We have written lots of code so far It has all been inside of the main() method What about a big program? The main() method is going to get really

STUDENT LESSON A12 Iterations

CSE 143 Sample Midterm Exam #4

XC Total Max Score Grader

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

Lecture 13: Two- Dimensional Arrays

Lab 5: Java IO 12:00 PM, Feb 21, 2018

THE UNIVERSITY OF WESTERN AUSTRALIA. School of Computer Science & Software Engineering CITS1001 OBJECT-ORIENTED PROGRAMMING AND SOFTWARE ENGINEERING

Lesson 3: Accepting User Input and Using Different Methods for Output

Midterm Exam 2 CS 455, Fall 2013

Building Java Programs

Practical Questions CSCA48 Week 6

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz

Transcription:

Com S 227 Fall 2016 Topics and review problems for Exam 2 Thursday, November 10, 6:45 pm Locations, by last name: (same locations as Exam 1) A-C Curtiss 0127, first floor only D-N Hoover 2055 O-Z Troxel 1001 General information This will be a 70-minute, timed, pencil-and-paper written exam. No books, no notes, no electronic devices, no headphones, no collaboration. The problems will primarily involve writing Java code or reading and interpreting Java code. Summary of Exam topics You must bring your ID to the exam. The exam covers everything done in lectures roughly through Wednesday, November 3 and in labs through lab 9. The exam will concentrate on topics covered since the first exam. Most problems will require loops, arrays, or lists. However, since the subject matter is cumulative, knowledge of everything from the first exam is assumed. So if you had any trouble with Exam 1 you may also want to refer to the review sheet for that exam. The list below is an overview of the main topics. Numbers in parentheses refer to the current textbook. If you have an older edition of the book, see http://www.cs.iastate.edu/~cs227/section_number_mapping.pdf. You will not be tested on JUnit or the Eclipse debugger. The exam does not cover interfaces. Loops (Sections 6.1 6.3 and 6.7 6.8) Arrays (7.1, 7.3) Two-dimensional arrays (7.6) ArrayLists (7.7, 7.3) Array, list and string algorithms counting, searching, etc. (6.7, 7.3) Wrapper classes (7.7) Reading and writing text files (11.1) Using Scanner to parse text (11.2) Recursion (13.1, 13.2) Sorting o selection sort algorithm (14.1) o merge sort (14.4) You do not have to memorize methods from the Java API. You should know System.out.print and System.out.println. You should know how to use String, Scanner, Math, Random, File, and ArrayList<E>, but for specific methods from these classes that might be needed, we ll provide you with the minimal one-sentence descriptions from the API. You should know how to read and write text files and how to read input from System.in.

How to prepare The most important thing you can do to prepare for an exam like this is to practice solving problems and writing code. You should write your solutions first on paper (since that is the format of the exam). Then check your work by typing up what you ve written in Eclipse. We encourage you to post and discuss your sample solutions in Piazza. You will need to write quickly and accurately, and to recognize correctly written code without the help of Eclipse. You will not be expected to write comments or documentation or define symbolic constants for numbers. (However, including brief comments can sometimes help us interpret what you were trying to do, in case you have errors.) More practice Remember that there are many, many more problems to practice on in the textbook. Each chapter has a number of self-check questions at the ends of the sections and a section of Review exercises at the end of each chapter. Another entertaining way to practice writing Java code is the interactive site http://codingbat.com/java. There are lots of problems involving arrays and strings. (However, note that codingbat.com has no problems involving ArrayList or Scanner.) Some practice problems You are encouraged to post your sample solutions on Piazza for discussion. 1) Some loop and array examples. Write a static method for each that has all needed information as parameters. (You don t have to create an entire class.) a) Given an array of doubles, return the average. b) Given a sentence, find and return the longest word. c) Given a string, return the string with all spaces and non-alphabetic characters replaced by the character #, e.g. Hello, world! becomes Hello##world# (You can use the static method Character.isAlphabetic(char c) to determine whether a given character is alphabetic.) d) Interest is added to the balance of a savings account each month. Write a method that, given an annual interest rate and an initial balance, determines how many months it takes for the balance to double. e) Given an ArrayList of Integers, determine whether they are in increasing order. f) Given a string, return the index of the first vowel (or -1 if there are none).

g) Given a string, determine whether any letter appears two or more times. h) Given an array of ints, reverse its contents (the method must modify the given array and returns void). i) Given an array of integers, determine whether the array is a permutation of the numbers 0 through n 1, where n is the length of the array. (A permutation means that each number appears exactly once.) j) Given a number n, print out a reverse diagonal line of n stars: k) Given a 2D array of doubles, return a 1D array whose ith entry is the average of the ith column. l) Given a 2D array of ints, find the column with the maximum sum. m) Given positive integers w and h and an int[] array arr of length w h, return a 2d array with h rows and w columns that contains the numbers in arr, listed left-to-right and top-to-bottom. n) Given an integer n, return the smallest prime number that is larger than n. (A number is prime if it is positive and has no divisors other than 1 and itself.) o) Given an array of positive integers, "collapse" the array to remove duplicates, and fill in the unused cells at the end with zeros. For example, given the array [5, 4, 5, 6, 4, 2], after this method executes the array should be [5, 4, 6, 2, 0, 0]. The method modifies the given array, and returns void. p) Given an array of positive integers, return a new array containing the same numbers, in the same order, but without duplicates. For example, given the array [5, 4, 5, 6, 4, 2], the method returns [5, 4, 6, 2]. q) Given an instance of Random, generate 10000 random values between 0 and 99, inclusive, and return a 100-element array containing the frequencies. (E.g., the ith cell of the result contains the number of times i was generated.) r) Given a string, return a new string with the words in the opposite order. (E.g. given "He's dead, Jim", return "Jim dead, He's".)

2) Write a program that will remove all the //-style comments from a Java file. Your program should prompt the user to enter the name of the input file. The output file should have the same name as the input file but should end with the extension.out instead of.java. The output file should be the same as the input file except that all //-style comments are removed. (You can assume that the sequence // does not occur inside any String literals within the program.) 3) Trace the execution of the call mystery(12,0) and show all output that is produced. public static void mystery(int x, int y) while (x > 0) if (x % 2 == 0) y = y + 1; else x = x + 2; x = x - y; System.out.println(x + ", " + y); 4) Given the array: int[] test = 6, 7, 4, 3, 5, 2, 7, 9, 8; Trace the execution of the call enigma(test) and show contents of the array after the method returns. public static void enigma (int[] arr) int i = 0; for (int count = 0; count < arr.length; count += 1) if (arr[i] % 2!= 0) for (int j = i; j < arr.length - 1; j += 1) arr[j] = arr[j + 1]; arr[arr.length - 1] = 0; else i += 1;

5) Write a static method getpassword that will read a user s password from System.in. The user has to enter the password twice. The method should iterate the following steps as many times as necessary until the user successfully enters two values that match: 1. prompt the user and read the password 2. prompt the user and read the password again 3. check that the second entry matches the first The method has no parameters and should return the entered password as a String. 6) Suppose that a file contains lines with a name and phone number having the format name, xxx-xxx-xxxx a) Create a class Contact suitable for storing a name and phone number (the phone number may be stored as a String). It should include an appropriate constructor and the methods: String getname() String getphonenumber() // returns phone number as String int[] getphonenumberarray() // returns phone number as an array of 10 ints b) Create a class ContactDirectory suitable for storing a list of Contacts. The ContactDirectory should include the methods: // adds the given contact to the directory void addcontact(contact c) // add all contacts from a file of the above form void addfromfile(string filename) throws FileNotFoundException // returns phone number name, or void if name is not in the list String lookup(string name) 7. a) Given the method mystery below, determine the output printed by the call mystery(10). (It might be helpful to sketch the call stack as you go.) public static void mystery(int x) if (x == 0) System.out.println("pooh"); else if (x % 2 == 0) System.out.println(x); mystery(x / 2); else mystery(x - 1);

b) What happens when you call mystery(-1)? Explain. 8. Rewrite the method mystery() from the previous problem so that it produces the same results as the given version for all positive numbers, but does not use recursion. 9. One way to efficiently calculate integer powers is by repeated squaring: x p = 1, if p is zero 2, if p is even x p 2 x x p!1 2 2, if p is odd (For example, to calculate 2 10, first calculate 2 5 and then square the result.) Write a recursive function pow(x, p) that uses this strategy. (Assume that p >= 0.) 10. A child named Beatrice is jumping along on a floor consisting of rectangular tiles. She can jump one tile, two tiles, or three tiles at a time. Write a recursive method to determine the number of ways she can cross n tiles. 11. Write a method that, given a directory (as a File object), returns a list of names of the files beneath it whose names end with ".java" (within it, within its subdirectories, and so on). It might help to define a recursive helper method of the form private void findjavafiles(file file, ArrayList<String> results) Note that the class java.io.file includes the following methods: String getname() returns the name of this File boolean isdirectory() returns true if this File represents a directory File[] listfiles() returns an array of all items (files and directories) in this File; returns null if this File is not a directory 12. There are many variations of the mergesort algorithm having different strategies to reduce memory usage and array copying. Suppose that you are given a merge method with the following declaration: / Merges two sorted subarrays of a given array, storing the result back in the given array. That is, when the method is called, arr[start] through arr[mid] is already sorted, and arr[mid + 1] through arr[end] is already sorted When the method returns, arr[start] through arr[end] is sorted. / private static void merge(int[] arr, int start, int end, int mid) Suppose that you also have the public method:

public static void mergesort(int[] arr) mergesortrec(arr, 0, arr.length - 1); Write the following recursive helper method that will sort a given subarray using the merge sort algorithm: / Performs a recursive merge sort of the subarray consisting of arr[start] through arr[end]. / private static void mergesortrec(int[] arr, int start, int end) Note: This problem is NOT asking you to rewrite the merge() method! You can find a sample solution for the problem above, along with two other variations of mergesort, in the March 30 entry here (see MergeSortAlt2.java): http://web.cs.iastate.edu/~smkautz/cs227s16/topics.html 13. Suppose you also have a method that performs a selection sort on a specified subarray: / Performs a selection sort on the elements arr[start] through arr[end] without modifying the others / selectionsortrange(int[] arr, int start, int end) Modify the base case for the merge sort algorithm so that for any subarray of length 5 or less, it uses a selection sort instead of making a recursive call. 14. Write the method selectionsortrange of the problem above.