CS 177 Spring 2005 Exam I



CS 180 Fall 2005 Exam II

There are 20 multiple choice questions. Each one is worth 2 points. There are 4 programming questions. Each one is worth 15 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 Exam 2.

08:30 recitation in REC 122: Matt Carlson

08:30 recitation in BRNG B254: Andy Scharlott

09:30 recitation in BRNG B268: Nick Sumner

11:30 recitation in REC 308: Rimma Nehme

01:30 recitation in UNIV 103: Alvin Law

Fill in the bubbles that correspond to your name, section and student ID in the bubble sheet. For your section number, use 0830, 0930,1130, or 0130 -- based on the start time of your Friday recitation.

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.

For the programming questions create a Java class that would compile without error. Comments are nice, but are not required.

Recitation Start Time: _________________________________

Student Last Name: __________________________________

Student First Name: __________________________________

Multiple Choice Questions (2 points each):

1. You have a string idCard that contains the value "54632". You have an Integer (wrapper class) object glump:

Integer glump = Integer.valueOf(idCard);

Which of the following puts the integer 54632 into a variable named blimp?

a. int blimp = glump;

b. int blimp = Integer.valueOf(idCard);

c. int blimp = intValue(glump);

d. int blimp = glump.intValue;

2. public class Student extends Person {

private int studentNumber;

public Student() {

studentNumber = 0;

}

...

}

Which is added automatically by the Java compiler as the first statement in the Student() Constructor?

a. name = "No name yet";

b. Person();

c. super();

d. nothing is added automatically

3. In a class method definition, this (as in this.getName()) stands for...?

a. the name of the calling object

b. the name of the parameter object

c. the name of the data member

d. the name of the method

4. Which best describes overloading and overriding?

a. In overloading, methods must have different signatures. In overriding, methods must have the same signature.

b. In overloading, methods must have the same signature. In overriding, methods must have different signatures.

c. In overloading and overriding, methods must have different signatures.

d. In overloading and overriding, methods must have the same signature.

5. Which of the following terms mean "determine which method will be called at run-time."

a. Neither (c) nor (d)

b. Both (d) and (c)

c. Polymorphism

d. Dynamic binding

6. What does the following code print out?

int[] a = {1, 2, 3, 4};

for (int k = 1; k 2)

throw new Exception("Error");

}

catch (Exception e)

{

System.out.println(e.getMessage());

return;

}

finally

{

System.out.println("Done");

}

a. Error

b. Error

Done

c. Done

d. Nothing will be printed

14. Suppose that you want to create a new text file to store some data from a program. Which one of these statements is most likely to appear in your program?

a. ObjectOutputStream oos = new ObjectOutputStream(

new FileOutputStream( "filename.txt" ) );

b. PrintWriter pw = new PrintWriter(

new ObjectOutputStream( "filename.txt" ) );

c. FileOutputStream fos = new FileOutputStream( "filename.txt" );

d. PrintWriter pw2 = new PrintWriter(

new FileOutputStream( "filename.txt" ) );

15. What is the difference between an Abstract class and Interface?

a. Interfaces provide a form of multiple inheritance. A class can extend only one other class.

b. Interfaces are limited to public methods and constants with no implementation. Abstract classes can have a partial implementation, protected parts, static methods, etc.

c. A Class may implement several interfaces. But in case of abstract class, a class may extend only one abstract class.

d. All of the above

16. A class Shape and its subclass Square both have a method area() which was written by the programmer as part of the class definition. If sq refers to an object of type Square, what will the following code do?

sq.area();

a. The area() method defined in Square will be called.

b. The area() method defined in Shape will be called.

c. The compiler will complain that area() has been defined twice.

d. Overloading will be used to pick which area() is called.

17. How can you tell if you have reached the end of a binary file when reading input?

a. A java.io.EOFException is thrown

b. A special sentinel value like null is returned

c. input.isEndOfFile() returns true

d. Either a or b

18. What is the result of attempting to compile and run the following program?

abstract class D

{

String s1 = "D";

String getS1()

{

return s1;

}

}

class E extends D

{

String s1 = "E";

String getS1()

{

return s1;

}

}

class F

{

public static void main (String[] s)

{

D x = new E();

System.out.print(x.s1 + x.getS1());

}

}

a. Prints: DD

b. Prints: DE

c. Prints: ED

d. Run-time error

19. What is the output of the following program?

import java.util.StringTokenizer;

public class Agrarian

{

public static void main( String[] args )

{

StringTokenizer st = new StringTokenizer(

"Peter Piper picked a peck.", "p " );

while ( st.hasMoreTokens() )

System.out.print( st.nextToken() );

}

}

a. Peter Pier icked a eck.

b. eterierickedaeck.

c. PeterPierickedaeck.

d. None of these

20. Given the following class definitions, what will the FileMaker program do?

import java.io.*;

class Wings

{

}

class Arthur implements Serializable

{

Wings wings = new Wings();

}

public class FileMaker

{

public static void main( String[] args )

{

try

{

ObjectOutputStream oos = new ObjectOutputStream(

new FileOutputStream("theTick.txt") );

Arthur[] moth = new Arthur[50];

for ( int index = 0; index < moth.length; ++index )

moth[index] = new Arthur();

oos.writeObject( moth );

}

catch( Exception e )

{

System.out.println( e );

}

}

}

a. It will compile and execute without output

b. It will compile and run, displaying

"java.io.NotSerializableException: Wings"

c. It will compile, and crash during runtime on a

java.io.NotSerializableException

d. It will fail to compile, with an "Object not Serializable" error

Programming Questions (15 points each):

1. Create a class Automobile which has three fields: String make, String model, int year.

• Implement mutator and accessor methods for all the fields.

• Create a constructor for class Automobile that takes a String make, String model, and int year and calls the mutator methods setMake(make), setModel(model), and setYear(year) with those parameters.

Add two more constructors which MUST CALL the above constructor:

• Create a constructor for class Automobile which takes a String make and a String model. It should call the above constructor with those values and 0 as the year.

• Create a default Constructor for Class Automobile that calls the constructor above giving it the values "no make", "no model", and 0.

2. Java's Math class contains many useful static methods, two of which are a Math.floor(double) and Math.ceil(double). The Math.floor(double) method takes a double and returns the greatest double equal to an integer that is not greater than the input parameter. Similarly, the Math.ceil(double) method returns the smallest double equal to an integer that is not smaller than the input parameter. For example,

Math.floor(3.6) returns 3.0 Math.floor(3.0) returns 3.0

Math.ceil(3.6) returns 4.0 Math.ceil(3.0) returns 3.0

Now consider operations such as squareFloor(double[]) and squareCeil(double[]). The squareFloor operation is similar to the floor(double) method except it determines the greatest double equal to a square number not greater than the input parameter. Note: A “square number” is any number that is the squared result of any integer. Examples of a square number: 1.0, 4.0, 9.0, 16.0, 25.0, 36.0, … Analogously, the squareCeil operation determines the smallest double equal to a square number not smaller than the input parameter. Moreover, we would like to be able to modify many doubles in an array with a single function call, so our input parameter will be an array of doubles instead. For example, given an array, testArray, thus defined:

double[] testArray = {3.5, 6.5, 7.8, 16.1};

SquareMath.squareFloor(testArray) would return this array: {1.0, 4.0, 4.0, 16.0}

SquareMath.squareCeil(testArray) would return this array: {4.0, 9.0, 9.0, 25.0}

Write the class SquareMath which contains the two static methods squareFloor and squareCeil. Each method should take in a double array and performs the above stated operations to each of the values in the array. Furthermore, the input array should remain unchanged. Instead, each method should return a new array containing the desired values. You may assume that the input arrays contain only non-negative values.

Start your code on the next page

public class SquareMath

{ // start your code here

3. Given the Point class defined below:

public abstract class Point

{

protected int x, y; // coordinates of the point

public Point(){setPoint(0, 0);} // default constructor

public Point(int a, int b){setPoint(a, b);}

public void setPoint(int a, int b)

{

x = a;

y = b;

}

public int getX(){return x;}

public int getY(){return y;}

public String toString()

{

Return(“[” + x + “,” + y + “]”);

}

}

Create the class Circle which is a subclass of Point. Circle should satisfy the following:

• Circle should have a double instance variable representing the radius of the circle.

• Circle should have a default Constructor which sets the radius to 0.

• Circle should have a three parameter Constructor (int, int, double). The first parameter is the x coordinate of the center, the second parameter is the y coordinate of the center, the third coordinate is the radius.

• Circle should have the accessor method getRadius.

• Circle should have a mutator method setRadius.

• Circle should have a method that computes and returns the area which equals π r2

• Circle should have a method that computes and returns the circumference which equals 2 π r

• You can use Math.PI for π

• Circle should have a toString method that describes the circle.

Start your code on the next page.

// write Circle.java code below

4. There exist two different classes, Ray and Point. You do not need to know the details of these classes. You only need to know they have constructors of the form Ray( int[] ) and Point( int[] ) respectively. You should also know that both classes have their toString() method which will print out pertinent information about any given instance.

You have been given a text file, data.txt, of the following format, where each line starts with either the word Ray or Point, which is followed by 3 arbitrary integers, comma separated and enclosed in parentheses. Any number of spaces may also exist between the numbers.

Sample data.txt file:

Ray( 0, 5, 7 )

Point( 7, 2, 7 )

...

Ray( 7, 2, 4 )

You must write the DataParser class that will open this file, and, for each line read, create a Ray or Point object, as specified in the file, with the appropriate numbers from the file being used to initialize the object. You should also call each object’s toString() method to print out the object. Upon any errors, the program should simply exit.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download