



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The final exam for the CS 180 course in Fall 2006. It contains 30 multiple-choice questions and 5 programming questions. The multiple-choice questions are worth 3 points each, while the programming questions are worth a total of 110 points. instructions on how to fill in the bubble sheet and exam booklet, including the Instructor, Course, Signature, Test, and Date blanks. The document also lists the recitation start times, TA's names, and corresponding section numbers. The document concludes with sample questions and answers.
Typology: Exams
1 / 27
This page cannot be seen from the preview
Don't miss anything!




















Recitation Start Time
Recitation TA’s Name
Student Last Name
Student First Name
Part I. Multiple Choice Questions (3 points each):
(a) Dynamic Binding (b) Polymorphism (c) Inheritance (d) Encapsulation ******
String S = (new String("arach")).substring(0,2) + (new String("nophobia")).substring(3);
(a) "arachobia" (b) "arnophobia" (c) "arhobia" ****** (d) "rachobia"
(a) When you get bored with public (b) If you want to disallow instantiation of that class from outside that class ****** (c) If you want to protect your class’s members from outside modification (d) Never, it’s not allowed
(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.
(a) char (b) boolean ****** (c) byte (d) int
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
(a) a class object and a class type ****** (b) any primitive type (c) boolean type only (d) class types only
(a) void handle( ActionEvent e ) (b) void actionPerformed( ActionEvent e ) ****** (c) void eventDispatched( AWTEvent e ) (d) String getActionCommand( ActionEvent e )
public class A extends Exception {...} public class B extends A {...} public class C extends B {...} public void doStuff() throws A,B,C
The following code does not compile. Why?
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); }
public static void main() { float x = 6.0; int y = 11;
x = A.doStuff(y,x);
System.out.print("x="+x+", y="+y); } }
(a) x=1, y= (b) this program does not compile ****** (c) x=6.0, y= (d) x=1.0, y=
class Reindeer throws HoHoHoException{...}
The following code throws an exception when it attempts to write 9 Reindeer objects to a file. Why?
File outFile = new File("Santa.txt"); FileOutputStream outFileStream = new FileOutputStream(outFile); ObjectOutputStream outObjectStream = new ObjectOutputStream(outFileStream);
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
I. Immediately after the write function is called II. When the data buffer is full III. When the close function is called
(a) I only (b) III only (c) II and III ****** (d) II only
(a) Because it doesn’t make logical sense to instantiate it ****** (b) So that it can be used as an interface (c) So that it cannot be inherited from (d) Because it has no abstract methods
(a) It must be defined in C before C can be instantiated ****** (b) None of these is true (c) It always forces C to become abstract (d) It overrides any method in C with the same name
public class B
(a) Class T extends B. (b) B is a bounded parameterized type restricted to be of type T which is of type A or a subclass of A. (c) T is a bounded parameterized type restricted to be of type A or a subclass of A. ****** (d) Class B extends A.
Person p = new Person(); Student s = new Student(); Undergraduate ug = new Undergraduate();
Which of the following assignments are legal?
I. p = ug; II. p = new Undergraduate(); III. ug = new Student(); IV. ug = p; V. s = new Person();
(a) III and IV (b) I and IV (c) I and II ****** (d) II, III and V
abstract class Bird implements Livestock {} class Chicken extends Bird {}
(a) Bird bird = new Chicken(); (b) Livestock livestock = new Chicken(); (c) Bird bird = new Bird(); ****** (d) None of these will compile
class Box
I. Box
(a) I, II, III (b) II only ****** (c) I and III only (d) I only
private int f( int x, int y ) { if( x == 0 ) { return y; } else { return f( x - 1, y + 1 ); } }
(a) 0 (b) 17 ****** (c) This recursion is incorrect in some way. (d) 72
private int g( int num ) { if( num <= 1 ) { return 1; } else { return 3*g( num-1 ) + g( num-2 ); } }
(a) This recursion is incorrect in some way. (b) 43 (c) 4 (d) 13 ******
int f( int num ) { if ( num <= 2 ) return num; else return num*g(num-1); }
int g( int num ) { if ( num <= 2 ) return num; else return (num-1)*f(num+1); }
(a) 1,2,6,24,... (b) This recursion is incorrect in some way. ****** (c) 1,2,3,6,... (d) 1,2,6,16,...
15 23 21 19
public static void printNums() { int n; String line; line = inFile.readLine(); if (line != null) { //If not EOF .. n = Integer.valueOf(line).intValue(); outFile.print(n + " "); printNums(); outFile.print(n + " "); } }
(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
This page is left blank intentionally.
Part II. Programming Questions (110 points total):
then the output should be:
Total number of grades = 7 Number of A = 2 Number of B = 2 Number of C = 0 Number of D = 0 Number of F = 3 The A grades are: 92, 90
Gender FirstName LastName Status
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:
M Jack Frost Bad F CindyLou Who Good M Rudolph Rednose-Reindeer Good
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:
Frost, Jack Coal Who, CindyLou Pony Rednose-Reindeer, Rudolph Bicycle
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.
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; }
public static void main(String args[]) throws IOException{ SantasHelper sh = new SantasHelper();
try{ sh.readList(); } catch(FileNotFoundException e){ System.out.println(e.getMessage()); } }
public void readList() throws IOException, FileNotFoundException{
String lastName, firstName, status, gender; File list; File outFile; int i = 0;
list = new File("List.txt"); outFile = new File("ShoppingList.txt");
FileOutputStream outFileStream = new FileOutputStream(outFile); PrintWriter outStream = new PrintWriter(outFileStream);
Scanner scanner = new Scanner(list);
while(scanner.hasNext()){ gender = scanner.next(); firstName = scanner.next(); lastName = scanner.next(); status = scanner.next(); if(status.equals("Bad")){
Solution for programming question 3:
abstract class Animal { private final String name; public Animal( String name ) { this.name = name; } public Animal() { this( "animal" ); } public void speak() { System.out.println( "This " + name + " speaks" ); } public void move() { System.out.println( "This " + name + " moves forward" ); } }
class Lynx extends Animal { public Lynx() { super( "lynx" ); } }
interface Flying { public void fly(); }
class Goose extends Animal implements Flying { public Goose() { super( "goose" ); } public void fly() { System.out.println( "This " + getClass().getName() + " soars, wings flapping."); } }