CS 180 Fall 2006 Final Exam, Exams of Programming Languages

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

Pre 2010

Uploaded on 05/11/2023

stagist
stagist 🇺🇸

4.1

(27)

265 documents

1 / 27

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 180 Fall 2006 Final Exam
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:
08:30 recitation in LWSN B134: Elizabeth Blythe
09:30 recitation in LILY G401: Matt Carlson
10:30 recitation in LILY G424: Matt Carlson
10:30 recitation in CIVL 1266: Alvin Law
11:30 recitation in REC 122: Alvin Law
12:30 recitation in LILY G424: Isuru Ranaweera
01:30 recitation in REC 308: Isuru Ranaweera
02:30 recitation in LWSN B134: Nick Sumner
03:30 recitation in REC 226: Nick Sumner
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.
Recitation Start Time
Recitation TAs Name
Student Last Name
Student First Name
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Partial preview of the text

Download CS 180 Fall 2006 Final Exam and more Exams Programming Languages in PDF only on Docsity!

CS 180 Fall 2006 Final Exam

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:

08:30 recitation in LWSN B134: Elizabeth Blythe

09:30 recitation in LILY G401: Matt Carlson

10:30 recitation in LILY G424: Matt Carlson

10:30 recitation in CIVL 1266: Alvin Law

11:30 recitation in REC 122: Alvin Law

12:30 recitation in LILY G424: Isuru Ranaweera

01:30 recitation in REC 308: Isuru Ranaweera

02:30 recitation in LWSN B134: Nick Sumner

03:30 recitation in REC 226: Nick Sumner

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.

Recitation Start Time

Recitation TA’s Name

Student Last Name

Student First Name

Part I. Multiple Choice Questions (3 points each):

  1. Which of the following characteristics of an object-oriented programming language restricts behavior so that an object can only perform actions that are defined for its class?

(a) Dynamic Binding (b) Polymorphism (c) Inheritance (d) Encapsulation ******

  1. What is the value of the String S after the following line?

String S = (new String("arach")).substring(0,2) + (new String("nophobia")).substring(3);

(a) "arachobia" (b) "arnophobia" (c) "arhobia" ****** (d) "rachobia"

  1. When would you use a private constructor?

(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

  1. Which of the following is true about RuntimeException and its subclasses?

(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.

  1. Which of the following types cannot be used as the parameter for a switch statement?

(a) char (b) boolean ****** (c) byte (d) int

  1. 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

  1. What are valid arguments to the instanceof operator?

(a) a class object and a class type ****** (b) any primitive type (c) boolean type only (d) class types only

  1. A class which implements the ActionListener interface must implement which method?

(a) void handle( ActionEvent e ) (b) void actionPerformed( ActionEvent e ) ****** (c) void eventDispatched( AWTEvent e ) (d) String getActionCommand( ActionEvent e )

  1. Given the following method and class signatures:

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.

  1. What is the output of the following program?

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=

  1. Given a class Reindeer with the following signature:

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

  1. When writing data to a file using a FileOutputStream, at what point is the data actually written to the file?

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

  1. Why would a class be declared as abstract?

(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

  1. Which of the following is true about an abstract method inherited into a class C?

(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

  1. Based on the class definition below, what can be inferred about the following class B:

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.

  1. Suppose the class Undergraduate extends the class Student which extends the class Person. Given the following variable declaration:

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

  1. Given the following definition of Bird and Chicken, which of the given statements will not com- pile?

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

  1. Given the following definitions, which assignments are legal?

class Box{} class SuperBox extends Box{}

I. Box b = new Box(); II. Box b = new SuperBox(); III Box b = new SuperBox();

(a) I, II, III (b) II only ****** (c) I and III only (d) I only

  1. Given the function below, what is the value of f( 8, 9 )?

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

  1. Given the function below, what is the value of g( 3 )?

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 ******

  1. Given the functions below, what is the sequence: f(1), f(2), f(3), f(4), ...

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,...

  1. 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

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):

  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 - 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...

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

  1. (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 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")){

  1. (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.

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."); } }