CSE142 Sample Final
Summer 2015
Name of Student_______________________________________________________________
Section (e.g., AA)___________________ TA_____________________________________
This 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 _____
----------------------------------
Total 100 _____
This is a closed-book/closed-note exam. Space is provided for your answers.
There is a "cheat sheet" at the end that you can use as scratch paper or to
write answers. You can also request scratch paper from a TA. You are not
allowed to access any of your own papers during the exam.
In general the exam is not graded on style and you do not need to include
comments, although the Critter class has special requirements you must follow.
You are limited to the constructs described in chapters 1 through 10 of the
textbook, which means that you are not allowed to use break statements or to
have a return from a void method. You are not allowed to use methods from the
Arrays class or other methods that aren't included on the cheat sheet. 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. 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
38 % 10 / 3 + 5/3 __________
23.0 / 2 - 3 * 0.25 __________
8 - 3 + "=" + 8 / 3 + 8 + 3 __________
(34 / 10 * 2.0 + 5) / 2 __________
5 % 8 + 8 % 5 - 17 % 4 __________
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 = 1; i < list.length - 1; i++) {
if (list[i] > i + 1) {
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
-----------------------------------------------------------------
{2, 3, 1} _________________________________
{2, 6, 5, 2} _________________________________
{3, 9, 7, 9} _________________________________
{2, 4, 5, 6, 8} _________________________________
{1, 5, 8, 4, 8, 9} _________________________________
3. Inheritance, 6 points. Assume the following classes have been defined:
public class C extends B {
public void method2() {
System.out.println("c 2");
}
}
public class B {
public String toString() {
return "b";
}
public void method1() {
System.out.println("b 1");
}
public void method2() {
System.out.println("b 2");
}
}
public class A extends D {
public void method1() {
System.out.println("a 1");
}
public void method2() {
System.out.println("a 2");
}
}
public class D extends C {
public String toString() {
return "d";
}
}
Consider the following code fragment:
B[] 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? Write the output as a series of
3-line columns in order from left to right (do not label columns or rows).
4. Token-Based File Processing, 10 points. Write a static method called
reportRunningSum that takes as a parameter a Scanner holding a sequence of
real numbers and that prints to System.out the running sum of the numbers
followed by the maximum running sum. In other words, the n-th number that
you report should be the sum of the first n numbers in the Scanner and the
maximum that you report should be the largest such value that you report.
For example if the Scanner contains the following data:
3.25 4.5 -8.25 7.25 3.5 4.25 -6.5 5.25
your method should produce the following output:
running sum = 3.25 7.75 -0.5 6.75 10.25 14.5 8.0 13.25
max sum = 14.5
The first number reported is the same as the first number in the Scanner
(3.25). The second number reported is the sum of the first two numbers in
the Scanner (3.25 + 4.5). The third number reported is the sum of the first
three numbers in the Scanner (3.25 + 4.5 + -8.25). And so on. The maximum
of these values is 14.5, which is reported on the second line of output.
You are to exactly reproduce the format of this output. You may assume that
there is at least one number to read. You may not construct any extra data
structures to solve this problem.
5. Line-Based File Processing, 9 points. Write a static method called
reportBlankLines that takes a Scanner containing an input file as a
parameter and that reports to System.out the line numbers of any blank lines
and that reports the total number of blank lines in the file. For example,
given the following input file:
Remember that a file
can have blank lines
like the one below:
A blank line:
is read as a String
of length 0
by Scanner
Your method should print the following output:
line 4 is blank
line 6 is blank
line 9 is blank
total blank lines = 3
Notice that each blank line produces a line of output and that there is a
final line of output reporting the total number of blank lines. Also notice
that lines are numbered starting with 1 (first line is line 1, second line
is line 2, and so on). You are to exactly reproduce this format. You may
not construct any extra data structures to solve this problem.
6. Arrays, 10 points. Write a static method called switchPairs that switches
the order of elements in an array of integers in a pairwise fashion. Your
method should switch the order of the first two values, then switch the
order of the next two, switch the order of the next two, and so on. For
example, suppose that a variable called list stores the following:
[12, 4, 8, 7, 9, -3]
This list has three pairs: (12, 4), (8, 7), and (9, -3). Thus, the call:
switchPairs(list);
should leave the list with these values:
[4, 12, 7, 8, -3, 9]
Notice that each pair has been switched. If there are an odd number of
values in the list, the final element should not be moved. For example, if
the original list had been:
[12, 4, 8, 7, 9, -3, 42]
It would again switch pairs of values, but the final value (42) would not be
moved, yielding this list:
[4, 12, 7, 8, -3, 9, 42]
You may not construct any extra data structures to solve this problem.
7. ArrayList, 10 points. Write a static method called oddsToBack that takes an
ArrayList of integer values as a parameter and that moves all odd numbers to
the back of the list, preserving their relative order. For example, if a
variable called list stores this sequence of values:
[7, 2, 8, 9, 4, 13, 7, 1, 9, 10]
then the following call:
oddsToBack(list);
should leave the list with the following values:
[2, 8, 4, 10, 7, 9, 13, 7, 1, 9]
Notice that the list begins with the even values in their original order
followed by the odd values in their original order. You may not construct
any extra data structures to solve this problem. You must solve it by
manipulating the ArrayList you are passed as a parameter. See the cheat
sheet for a list of available ArrayList methods.
8. Critters, 15 points. Write a class called Iguana that extends the Critter
class. The instances of the Iguana class always infect when an enemy is in
front of them and otherwise randomly choose between turning left and turning
right, with each choice being equally likely. Their appearance changes over
time. Each Iguana initially displays as a less-than followed by a dash
followed by a greater-than ("<->"). Then as each Iguana chooses a move, it
changes its appearance to match that move. If its most recent move was an
infect, it displays as "". If its most recent move was to turn left, it
displays as "". And if its most recent move was to turn right, it
displays as "". Its color should alternate between blue and red. It
should be blue initially when it is displayed as "<->" and then it should
alternate between the two colors (red after the first move, blue after the
second move, etc.).
Use a Random object to make random choices but each Iguana should construct
only one such object. As in assignment 8, fields must be declared private,
fields initialized to a non-default value must be set in a constructor, and
all updates to fields must occur in the getMove method.
9. Arrays, 15 points. Write a static method called splitPairs that takes an
array of integers as a parameter and that returns a new array containing the
result of splitting successive pairs of numbers so that the first values
from each pair appear first followed by the second values from each pair.
For example, suppose that a variable called list stores the following:
[3, 8, 4, 9, 7, 2]
This list has three pairs: (3, 8), (4, 9), and (7, 2). Thus, the call
splitPairs(list) should return the following array:
[3, 4, 7, 8, 9, 2]
Notice that this list contains the first values from each pair (3, 4, 7)
followed by the second values from each pair (8, 9, 2). If there is an
extra value that is not part of a pair, then it should be included with the
first set of values in the new array. For example, if list stores:
[7, 5, 3, 2, 8, 4, 6]
then the call splitPairs(list) should return:
[7, 3, 8, 6, 5, 2, 4]
The value 6 in the original list is not part of a pair. Notice that the new
array has the first values from each pair (7, 3, 8) followed by 6 followed
by the second values from each pair (5, 2, 4). The method should not
construct any extra data structures other than the array to be returned and
it should not alter its parameter.
10. Programming, 10 points. Write a static method called acronym that takes as
a parameter a string containing a phrase and that returns a string that has
the acronym for the phrase. For example, the following call:
acronym("self-contained underwater breathing apparatus")
should return "SCUBA". The acronym is formed by combining the capitalized
first letters of each word in the phrase. Words in the phrase will be
separated by some combination of dashes and spaces. There might be extra
spaces or dashes at the beginning or end of the phrase. The string passed
as a parameter will not contain any characters other than dashes, spaces,
and letters, and is guaranteed to contain at least one word. Below are
several sample calls.
Method Call Value Returned
----------------------------------------------- --------------
acronym(" automatic teller machine ") "ATM"
acronym("personal identification number") "PIN"
acronym("computer science") "CS"
acronym("merry-go-round") "MGR"
acronym("All my Children") "AMC"
acronym("Troubled Assets Relief Program") "TARP"
acronym("--quite-- confusing - punctuation-") "QCP"
acronym(" loner ") "L"
You are allowed to construct extra data structures to solve this problem (an
array, ArrayList, Scanner, String, etc), but you may use only methods listed
on the cheat sheet.
Stuart Reges
Last modified: Fri Sep 16 16:16:07 PDT 2011