CS 180 Fall 2006 Final Exam: Recitation Start Time
CS 180 Fall 2006 Final Exam: Recitation Start Time
CS 180 Fall 2006 Final Exam: Recitation Start Time
There are 30 multiple choice questions. Each one is worth 3 points. There are 5 program-
ming questions worth a total of 110 points.
Answer the multiple choice questions on the bubble sheet given and the programming
questions on the exam booklet.
Fill in the Instructor, Course, Signature, Test, and Date blanks. For “Instructor” put your
Recitation Instructor’s last name. For “Course” put CS 180. For “Test” put Final.
Fill in the bubbles that correspond to your name, section and Student ID in the bubble
sheet. For your section number, use 0830, 0930, 1030, 1130, ... – based on the start time of
your Friday recitation. Consult the following list:
For your student ID, use the 10 digit ID number on your student ID card. DO NOT USE
YOUR SOCIAL SECURITY NUMBER!
Exams without names will be graded as zero. Only the answers on the bubble sheet will
be counted. The questions will be discarded.
(a) "arachobia"
(b) "arnophobia"
(c) "arhobia" ******
(d) "rachobia"
(a) If a method throws a RuntimeException, the use of the try/catch block is optional. ******
(b) The FileIOException class is a subclass of RuntimeException.
(c) In general, handling of RuntimeException should be done at compile time.
(d) In general, RuntimeException must be caught with a try/catch block.
5. Which of the following types cannot be used as the parameter for a switch statement?
(a) char
(b) boolean ******
(c) byte
(d) int
1
6. What is the output when you try to compile and run the following code?
(a) 12-1
(b) 1
(c) 12
(d) The code does not compile ******
(a) 0
(b) the sum of 100 integers
(c) NullPointerException ******
(d) the code will not compile
2
8. What is the output of the following code segment?
char x = ’A’;
while(x != ’D’){
switch(x){
case ’A’:
System.out.println(x);
x = ’D’;
case ’B’:
System.out.println(x);
x = ’C’;
break;
case ’C’:
System.out.println(x);
x = ’D’;
default:
continue;
}
}
(a) A ******
D
C
(b) A
D
C
D
(c) A
D
(d) A
10. A class which implements the ActionListener interface must implement which method?
3
11. Given the following method and class signatures:
try {
doStuff();
} catch(A a) {
a.printStackTrace();
} catch(B b) {
b.printStackTrace();
} catch(C c) {
c.printStackTrace();
} finally {
System.out.println("I love exceptions!");
}
(a) The catch blocks for exceptions of type B and C are unreachable. ******
(b) A finally block cannot be used with multiple catch blocks.
(c) B and C are not exception classes since they do not extend class Exception and therefore cannot
be caught.
(d) No one loves exceptions and therefore the finally block fails to compile.
public class A {
public static int doStuff(double x, double y) {
return (int)(x/y);
}
x = A.doStuff(y,x);
System.out.print("x="+x+", y="+y);
}
}
4
13. What is the result of the following code within a single class where all relevant code has been shown?
14. After execution of the following code, what will be the values of x, y and z?
int x, y, z;
y = 1;
z = 5;
x = 0 - (++y) + z++;
(a) x = 4, y = 2, z = 6
(b) x = 4, y = 1, z = 5
(c) x = 3, y = 2, z = 6 ******
(d) x = -7, y = 1, z = 5
5
16. Given a class Reindeer with the following signature:
The following code throws an exception when it attempts to write 9 Reindeer objects to a file.
Why?
Reindeer r;
for(int i = 0; i < 9; i++){
r = new Reindeer("Reindeer" + (i + 1));
outObjectStream.writeObject(r);
}
(a) The Reindeer class does not implement the Serializable interface ******
(b) Only 8 Reindeer objects are written
(c) The file Santa.txt is not a data file and we can only write objects to data files
(d) We should use a DataOutputStream instead of an ObjectOutputStream
17. When writing data to a file using a FileOutputStream, at what point is the data actually written
to the file?
(a) I only
(b) III only
(c) II and III ******
(d) II only
19. Which of the following is true about an abstract method inherited into a class C?
6
20. Based on the class definition below, what can be inferred about the following class B:
21. Suppose the class Undergraduate extends the class Student which extends the class Person.
Given the following variable declaration:
I. p = ug;
II. p = new Undergraduate();
III. ug = new Student();
IV. ug = p;
V. s = new Person();
22. Given the following definition of Bird and Chicken, which of the given statements will not com-
pile?
7
23. Which of the following are true regarding the use of generics and parameterized types in Java?
(a) I and II
(b) II and III
(c) I, II, and III ******
(d) I and III
24. What type of relationship exists between someMeth in classes A and someMeth in class B?
class A
{
private void someMeth()
{
System.out.println( "from class A" );
}
}
class B extends A
{
public void someMeth( String x )
{
System.out.println( "from class B: " + x );
}
}
25. Which of the following statements is true regarding Vectors with no specified parameterized type?
(a) If a parameterized type is not specified the code will not compile
(b) No parameterized type is needed because Java will use the Object class as a parameterized type
******
(c) A parameterized type is needed because Java needs to know how to allocate memory
(d) No parameterized type is needed because Vectors default to storing String objects
8
26. Given the following definitions, which assignments are legal?
class Box<T>{}
class SuperBox<T> extends Box<T>{}
(a) 0
(b) 17 ******
(c) This recursion is incorrect in some way.
(d) 72
9
29. Given the functions below, what is the sequence: f(1), f(2), f(3), f(4), ...
(a) 1,2,6,24,...
(b) This recursion is incorrect in some way. ******
(c) 1,2,3,6,...
(d) 1,2,6,16,...
30. what is the output of a call to the printNums() method? Assume inFile has been properly linked to the
file whose content is shown below and outFile has been properly linked to some output file.
15
23
21
19
(a) 15 23 21 19 19 21 23 15 ******
(b) 19 21 23 15 19 21 23 15
(c) 15 23 21 19
(d) 19 21 23 15
10
The version of your test is A. Please FILL IN CIRCLE (A) for the TEST FORM field on the BUBBLE
SHEET directly under the DATE field and turn in your exam booklet and answer sheet to the stack labeled
(A) in the front of the classroom. Thank you.
11
This page is left blank intentionally.
12
Part II. Programming Questions (110 points total):
1. (20 points) Write a class, call it GradesCount, to read a list of grades from the keyboard (integer
numbers in the range 0 to 100). Prompt the user with “Please enter a grade between 0 to 100 or -1
to quit: ” each time before reading the next integer. Store each grade in a A, B, C, D or F Vector as
follows: 90 to 100 = A, 80 to 89 = B, 70 to 79 = C, 60 to 69 = D, and 0 to 59 = F. (Hint: You cannot
store ints as Vector elements, but you can store Integers.)
Output the total number of grades entered, the number of A, B, C, D and F, and a list of the A’s. For
example, if the input is...
38
86
92
55
83
42
90
-1
13
Solution for programming question 1:
import java.util.*;
14
2. (20 points) Santa’s list gets longer every year and he is having trouble keeping track of what he is
going to bring each boy and girl. This year he has gone high-tech and kept a list of names along with
whether the child was good or bad on his computer. However, he now needs to know how many toys
and how many lumps of coal he needs to buy and for which children. He has hired you to write a
program that will read in his list (it’s saved in a file called List.txt) and produce another text file called
ShoppingList.txt that he can use for delivering gifts and to give the elves so they know how to pack
his toy bag.
The List.txt file is formatted so that each line has the following format:
Gender will be either an “F” or a “M” and Status will be either “Good” or “Bad”
An example List.txt file would be formatted as follows:
The ShoppingList.txt file should be formatted such that each line has “lastName, firstName toyName”
where toyName is “Coal” if the status of the child is “Bad”, toyName is “Pony” if the gender of the
child is “F” and the status of the child is “Good”, and the toyName should be “Bicycle” if the gender
of the child is “M” and the status of the child is “Good”. In addition, the last three lines of the file
should print the number of lumps of coal, bicycles, and ponies to buy.
An example resulting ShoppingList.txt file for the above List.txt should be:
Lumps of Coal: 1
Bicycles: 1
Ponies: 1
In order to do this you should create a complete class called SantasHelper which will read List.txt
and produce ShoppingList.txt. Do not make any assumptions about the length of the list, but you can
assume that it is formatted correctly. If the List.txt file does not exist, you should catch any exception
that might be thrown.
15
Solution for programming question 2:
import java.util.*;
import java.io.*;
class SantasHelper{
int numPonies;
int numBicycles;
int numLumps;
SantasHelper(){
numPonies = 0;
numBicycles = 0;
numLumps = 0;
}
try{
sh.readList();
}
catch(FileNotFoundException e){
System.out.println(e.getMessage());
}
}
while(scanner.hasNext()){
gender = scanner.next();
firstName = scanner.next();
lastName = scanner.next();
status = scanner.next();
if(status.equals("Bad")){
16
outStream.println(lastName + ", " + firstName + " Coal");
numLumps++;
}
else if(gender.equals("F")){
outStream.println(lastName + ", " + firstName + " Pony");
numPonies++;
}
else{
outStream.println(lastName + ", " + firstName + " Bicycle");
numBicycles++;
}
}
17
3. (20 points) Several design properties for a problem are presented below. Use these properties in
order to write all the necessary classes and/or interfaces for a solution to the problem. Focus on
class structure and interaction. You may implement your solution however you wish, but you will be
graded on the appropriateness of your solution to the requirements. Note the use of capitalization and
parentheses for clarification. You may use whatever constructors or additional methods you wish.
You must define a structure that can represent Animals. Animals have two behaviors; they can speak()
and they can move(). By default, when an animal moves, the text ”This animal moves forward” is
displayed. By default, when an animal speaks, the text ”This animal speaks” is displayed. A general
Animal should not be able to be instantiated.
Define also two classes, Goose and Lynx, that are Animals. Both Goose and Lynx behave such that
where ”animal” is displayed in speak() or move(), ”goose” or ”lynx” is displayed by the appropriate
classes.
Finally, any instance of Goose can fly(), just as any Flying object can. An Airplane is also a Flying
object. Define the Airplane class such that it is Flying and make sure that any instance of Goose is
also Flying. The specific behaviors when instances of either class fly() are up to you. Instances of
either Goose or Airplane should be able to be stored in a variable of type Flying.
18
Solution for programming question 3:
interface Flying
{
public void fly();
}
19
class Airplane implements Flying
{
public void fly()
{
System.out.println( "This " + getClass().getName() +
" soars, engines running.");
}
}
20
4. (25 points) PART 1: In computer science, a Stack is a LIFO (last in, first out) data structure. Objects
most recently inserted into a stack are the first objects removed from the stack. One way to design
a stack data structure is to build it as a chain of node objects (like a linked list). The first part of
this question involves designing an appropriate node class. Your node class should take advantage of
parameterized types such that any type of object can be placed inside the node. Minimally, you should
have appropriate class variables, constructors, and a set of accessors/mutators for each of your class
variables. Fill in the Node class on the next page:
21
Solution for programming question 4 part 1:
22
PART 2: Now, write a class Stack. Your class should have a parameterized type such that any type
of item can be placed in the Stack. You should also use your Node class as defined above to hold the
contents of the stack. Minimally, your Stack class must have the following:
23
Solution for programming question 4 part 2:
public T pop() {
if (stack == null) return null;
T popped = stack.getItem();
stack = stack.getNext();
return popped;
}
public T peek() {
if (stack == null) return null;
return stack.getItem();
}
}
24
5. (25 points) A palindrome is a word or phrase that reads the same forward or backwards. Write a
recursive method that returns a boolean value indicating if its only String argument is a palindrome or
not. The method must be recursive and have the following signature:
In testing for palindrome you should ignore upper/lower case as well as whitespace. That is, the
following phrases should be legal palindromes.
Place the isPalindrome method in a class named Palindrome. Write a main method in Palindrome
which prompts for a string from the keyboard, test if it is a palindrome, and print out the result. Below
is a sample output.
25
Solution for programming question 5:
import java.util.*;
} // end Palindrome
26