CSE142 Final
Winter 2010
Name of Student_______________________________________________________________
Section (e.g., AA)___________________ TA_____________________________________
This is an open-book/open-note exam. Space is provided for your answers. Use
the backs of pages if necessary. The exam is divided into eleven questions
with the following points:
# Problem Area Points Score
-----------------------------------------------------
1 Expressions 5 _____
2 Array Simulation 10 _____
3 Inheritance 6 _____
4 Token-Based File processing 10 _____
5 Line-Based File Processing 9 _____
6 Arrays 10 _____
7 ArrayList 10 _____
8 Critters 15 _____
9 Arrays 15 _____
10 Programming 10 _____
11 Art (bonus) 1 _____
----------------------------------
Total 100 _____
In general the exam is not graded on style and you do not need to include
comments, although all fields of a class should be declared private. You do
not have to include any import statements. Do not abbreviate any code that you
write (e.g., S.o.p versus System.out.print) and do not abbreviate any answer
asking for sample output (show the complete output).
You are NOT to use any electronic devices while taking the test, including
calculators. Anyone caught using an electronic device will receive a 10 point
penalty.
Do not begin work on this exam until instructed to do so. Any student who
starts early or who continues to work after time is called will receive a 10
point penalty.
If you finish the exam early, please hand your exam to the instructor and exit
quietly through the front door.
1. Expressions, 5 points. For each expression in the left-hand column,
indicate its value in the right-hand column. Be sure to list a constant of
appropriate type (e.g., 7.0 rather than 7 for a double, Strings in quotes).
Expression Value
2 + 3 * (2 + 3) + 17/4 __________
67 % 10 % 5 * 6 - 3 % 6 __________
7.5 * 2 + 19/2/2.0 - 0.5 - 1/2 __________
4 + 2 * 2 + "2 * 2" + 2 * 2 + 4 __________
43/4/3 * 5.0/2 + 5/2 __________
2. Array Simulation, 10 points. You are to simulate the execution of a method
that manipulates an array of integers. Consider the following method:
public static void mystery(int[] list) {
for (int i = 2; i < list.length; i++) {
list[i] = list[i] - list[i - 1];
}
}
In the left-hand column below are specific lists of integers. You are to
indicate in the right-hand column what values would be stored in the list
after method mystery executes if the integer list in the left-hand column
is passed as a parameter to mystery.
Original List Final List
------------------------------------------------------------
{8, 4} ____________________________
{20, 30, 40} ____________________________
{1, 2, 3, 4, 5} ____________________________
{5, 5, 5, 5} ____________________________
{10, 20, 30, 40, 50} ____________________________
3. Inheritance, 6 points. Assume the following classes have been defined:
public class A extends D {
public String toString() {
return "a";
}
public void method1() {
System.out.println("a 1");
}
}
public class B extends C {
public String toString() {
return "b";
}
}
public class C extends D {
public void method1() {
System.out.println("c 1");
}
}
public class D {
public String toString() {
return "d";
}
public void method1() {
System.out.println("d 1");
}
public void method2() {
System.out.println("d 2");
}
}
Consider the following code fragment:
D[] elements = {new C(), new A(), new D(), new B()};
for (int i = 0; i < elements.length; i++) {
System.out.println(elements[i]);
elements[i].method1();
elements[i].method2();
System.out.println();
}
What output is produced by this code? (you may write the output as a series
of 3-line columns in order from left to right)
4. Token-Based File Processing, 10 points. Write a static method called
switchData that takes as a parameter a Scanner containing a label followed
by a sequence of integers and that prints to System.out the same information
with each successive pair of integers switched in order. For example,
suppose that a Scanner called data contains the following tokens:
Jan 1 2 3 4 5 6
Here the label is "Jan". The label will always be a single word that
appears at the beginning. After the label, we have a series of six
integers. If we make the following call:
switchData(data);
the method should produce the following output:
Jan 2 1 4 3 6 5
Notice that the first pair of integers (1, 2) has been switched (2, 1), and
the second pair of integers (3, 4) has been switched (4, 3), and so on.
This first example involved sequential integers to make the switching more
obvious, but this won't always be the case. You also shouldn't assume that
you have an even number of integers. If there is an odd number of integers,
then the final value should not be moved. For example, if the Scanner had
instead contained these tokens:
Feb 38 14 79 4 -3
then the method would have produced the following output:
Feb 14 38 4 79 -3
There will always be a one-word label, but the list of integers might be
empty, in which case the method simply prints the label on a line by itself.
Your method should produce a complete line of output. In other words, if it
is called n times, it will produce n lines of output. You may assume that
the input is legal (a one-word label followed by 0 or more integer values).
5. Line-Based File Processing, 9 points. Write a static method called
analyzeParagraphs that takes as a parameter a Scanner containing a text file
and that produces output that describes the paragraph structure of the file,
returning the maximum number of lines in any given paragraph. Each
paragraph in the input file will be terminated by the text "" on a line
by itself. For example, consider the following input file:
This is an example of an input file
with four different paragraphs.
The second paragraph is the longest
with three lines, so your method should
return 3 when processing this file.
The third paragraph was empty. This one is just short.
The method should count the number of lines in each paragraph and report
that information to System.out. For example, if the input above is stored
in a Scanner called input and we make the following call:
int max = analyzeParagraphs(input);
It should produce the following output:
2-line paragraph
3-line paragraph
0-line paragraph
1-line paragraph
It would assign max the value 3 because the method returns the maximum
number of lines in any given paragraph. You must exactly reproduce the
format of this output. You may assume that the input file has no blank
lines, that it contains at least one paragraph, and that each paragraph is
terminated by a line containing just "
".
6. Arrays, 10 points. Write a static method called sameGap that takes an array
of integers as a parameter and that returns whether or not the gap between
successive values in the array is always the same. The gap between
successive values is defined to be the absolute value of the difference
between the two values. For example, consider the following sequence:
[1, 4, 7, 10, 13, 10, 13, 10, 7, 4]
Each successive pair of values has a gap of 3. The first pair of values is
1 and 4, which are 3 apart. The second pair of values is 4 and 7, which are
3 apart. The third pair of values is 7 and 10, which are 3 apart. Notice
that sometimes the first value is larger, as in the pair of values 13
followed by 10 or 10 followed by 7. These still have a gap of 3 because we
use the absolute value of the difference.
Your method should return true if passed an array with fewer than 2 values.
Below are examples of arrays and the value that should be returned for each:
Contents of array Value returned
passed to sameGap by sameGap
--------------------------- --------------
{} true
{42} true
{10, 15} true
{1, 4, 7, 10, 13, 10, 13, 10, 7, 4} true
{1, 4, 10, 13, 10, 13, 10, 7, 4} false
{2, 4, 6, 8, 10} true
{2, 4, 6, 8, 10, 11} false
{1, 2, 3, 4, 5, 6} true
{1, 2, 3, 2, 3, 4, 5, 6} true
{1, 2, 3, 2, 4, 5, 6} false
{3, 3, 3} true
You may use the Math.abs method to find the absolute value.
7. ArrayList, 10 points. Write a static method called split that takes an
ArrayList of integer values as a parameter and that replaces each value in
the list with a pair of values, each half the original. If a number in the
original list is odd, then the first number in the new pair should be one
higher than the second so that the sum equals the original number. For
example, if a variable called list stores this sequence of values:
[18, 7, 4, 24, 11]
The number 18 is split into the pair (9, 9), the number 7 is split into (4,
3), the number 4 is split into (2, 2), the number 24 is split into (12, 12)
and the number 11 is split into (6, 5). Thus, the call:
split(list);
should cause list to store the following sequence of values afterwards:
[9, 9, 4, 3, 2, 2, 12, 12, 6, 5]
You may assume that all numbers in the list are nonnegative. Recall that
the primary methods for manipulating an ArrayList are:
add(E value) appends value at end of list
add(int index, E value) inserts given value at given index, shifting
subsequent values right
clear() removes all elements of the list
get(int index) returns the value at given index
remove(int index) removes and returns value at given index,
shifting subsequent values left
set(int index, E value) replaces value at given index with given value
size() returns the number of elements in list
8. Critters, 15 points. Write a class called Eagle that extends the Critter
class. The instances of the Eagle class switch back and forth between red
and blue in an increasing pattern. They should be red for one move and then
blue for one move. Then they should be red for two moves and then blue for
two moves. Then they should be red for three moves and then blue for three
moves. Then they should be red for four moves and then blue for four moves.
And so on, each time increasing the number of moves at a particular color by
one. They should be displayed using a less-than character followed by a
greater-than character (<>). They should always hop whenever possible. If
they can't hop, they should turn right if there is a wall in front of them
and otherwise they should infect.
9. Arrays, 15 points. Write a static method called reverseSublist that takes
as parameters an array of integers and a "from index" (inclusive) and "to
index" (exclusive) and that returns a new array containing the specified
sublist in reverse order. For example, suppose that an array called list
stores the following values:
[0, 1, 2, 3, 4, 5, 6, 7]
and we make the following call:
int[] result = reverseSublist(list, 2, 5);
The sublist specified goes from index 2 (inclusive) to index 5 (exclusive).
In other words, it includes the values [2, 3, 4] from the list. The method
should construct and return a new array that contains this sequence of
values in reverse order:
[4, 3, 2]
This first example used an array of sequential integers, but the array might
contain any values. If the list instead stores these values:
[34, 9, -8, 17, 4, 32, 9]
and we make the following call:
int[] result = reverseSublist(list, 1, 5);
the method should instead return an array containing this sequence:
[4, 17, -8, 9]
You may assume that the two index values are legal:
0 <= (from index) <= (to index) <= (array length)
If the from-index and to-index are equal, your method should return an array
of length 0.
Your method should not change the array passed as a parameter. You are not
allowed to solve this problem using an ArrayList and you are not allowed to
call any methods of the Arrays class or the Collections class.
10. Programming, 10 points. Write a static method called count that takes as
parameters a target string and a source string and that returns a count of
the number of occurrences of the target string in the source string. For
example, the following call:
count("i", "Mississippi")
should return 4 because there are 4 occurrences of the string "i" in the
string "Mississippi". Your method should ignore case when comparing
strings. For example, the call:
count("iss", "MISSISSIPPI")
should return 2 because there are two occurrences of "iss" in
"MISSISSIPPI". Your method should consider all possible starting positions
for the target string. For example, given the following call:
count("EE", "EeEeE")
Your method should return the value 4 because there are 4 different
locations where the string "ee" occurs in the string "EeEeE" (starting at
index 0, index 1, index 2, and index 3).
You may assume that the target string is not empty. Recall that the String
class has the following useful methods:
charAt(index) character at a specific index
endsWith(text) whether or not the string ends with some text
equals(text) whether or not the string equals given text
equalsIgnoreCase(text) whether or not the string equals given text
ignoring case
indexOf(text) index of a particular string (-1 if not found)
length() number of characters in the string
startsWith(text) whether or not string starts with some text
substring(start, stop) characters from start index (inclusive) to
stop index (exclusive)
substring(start) characters from start index (inclusive) to end
of string
toLowerCase() a new string with all lowercase letters
toUpperCase() a new string with all uppercase letters
11. Art, 1 point (bonus). You've programmed lions, bears, giants, and huskies,
but what kind of critter would your TA be? Give an artistic rendering of
how you see your TA fitting into the unforgiving world of the critter
simulation. You may either write a poem or draw a picture. If your answer
below indicates at least 1 minute of effort, you will receive full credit,
although your TA would probably appreciate it if you put in a little more
effort.