CSE 142, Summer 2009 Final Exam, Friday, August 21, 2009
CSE 142, Summer 2009 Final Exam, Friday, August 21, 2009
CSE 142, Summer 2009 Final Exam, Friday, August 21, 2009
Name: ___________________________________________
Student ID #: ___________________
Good luck!
1 of 9
1. Array Mystery (15 points)
Consider the following method:
Indicate in the right-hand column what values would be stored in the array after the method executes
if the integer array in the left-hand column is passed as a parameter to it.
Original Contents of Array Final Contents of Array
2 of 9
2. Parameter and References Simulation (12 points)
The program below produces 4 lines of output. What are they?
Output:
3 of 9
3. Inheritance Mystery (15 points)
Assume that the following classes have been defined:
Given the classes above, what output is produced by the following code?
4 of 9
4. File Processing (16 points)
Write a static method named that accepts as its parameters a for an input file and an
array of s representing a list of keywords in a search.
Your method will read lines from its input and it should return the line number of the
first line in the file that contains one or more words from the keywords array. (The line numbers are 1-based, so the
first line of the file is line 1.) If none of the keywords are found in the file, your method should return a -1. The search
should be case-insensitive, so if "banana" is a keyword, the line "baNAna split" would be considered a line that
contains the keyword. Your method should also match whole words only, so if the word "ball" is a keyword, the line
"soccer or football" should not be considered a match for that keyword.
For example, consider the following input file saved in :
The following table shows some calls to your method and their expected results.
Array Returned Value
returns
returns
returns
returns
Assume that none of the words in the keywords array contain spaces, i.e. all keywords are non-null, non-empty single
whole words.
5 of 9
5. Array Programming (16 points)
Write a method which takes an array of integers bids and an integer price as parameters. The
method returns the element in the bids array that is closest in value to price without being larger than price. For
example, if bids stores the elements , then should
return , since 250 is the bid closest to 280 without going over 280. If all bids are larger than price, then your
method should return -1.
The following table shows some calls to your method and their expected results:
Arrays Returned Value
returns
returns
returns
returns
returns
You may assume there is at least 1 element in the array, and you may assume that the price and the values in bids will
all be greater than or equal to 1. Do not modify the contents of the array passed to your method as a parameter.
6 of 9
6. Critters (16 points)
Write a class that extends the class from Homework 8.
s move in an increasing NESW square pattern: 1 move north, 1 move east, 1 move west, 1 move south,
then 2 moves north, 2 moves east, etc., with the square pattern growing larger and larger indefinitely. If a
runs into a piece of food, the eats the food and immediately restarts the NESW
pattern. The size of the movement is also reset back to 1 move in each direction again, and the
increasing square pattern continues as before until another piece of food is encountered.
Here is a sample movement pattern of a :
north 1 time, east 1 time, south 1 time, west 1 time
north 2 times, east 2 times, south 2 times, west 2 times
north 3 times, east 3 times, south 3 times, west 3 times
(runs into food)
north 1 time, east 1 time, south 1 time, west 1 time
north 2 times, east 1 time
(runs into food)
north 1 time
(runs into food)
north 1 time, east 1 time, south 1 time, west 1 time
north 2 times, east 2 times, south 2 times, west 2 times
(etc.)
Write your complete class below. All other aspects of besides eating and movement
behavior use the default critter behavior. You may add anything needed to your class (fields, constructors, etc.) to
implement this behavior appropriately.
7 of 9
7. Array Programming (10 points)
Write a static method that accepts a sorted array of integers a1 as a parameter and returns a new array that
contains only the unique values of a1. The values in the new array should be ordered in the same order they originally
appeared in. For example, if a1 stores the elements , then
should return a new array with elements .
The following table shows some calls to your method and their expected results:
Array Returned Value
returns
returns
returns
returns
returns
returns
Do not modify the contents of the array passed to your method as a parameter.
You may assume the array contains at least one element.
8 of 9
X. Extra Credit (+1 point)
If your TA was a Critter, how would that Critter behave and why?
(Anything you write will get the +1 extra point.)
9 of 9