CS 180 Spring 2002
Final Exam

Part 1. Multiple Choice Questions

Answer the following multiple choice questions on the bubble sheet given.

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

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

Exams without names will be graded as zero.

Only the answers on the bubble sheet will be counted. The questions will be discarded.

Each question counts for 1 point. There are 40 multiple choice questions in total.

(1) Java Byte Code is in a ...?
(a) .class file
(b) .java file
(c) .exe file
(d) .jbc file
(a)

(2) Given that int1 and int2 are ints and doub1 and doub2 are doubles, which of the following requires an explicit cast?
(a) doub1 = int2;
(b) doub1 = doub2;
(c) doub2 = int1/doub1;
(d) int2 = doub1/doub2;
(d)

(3) Given that int1 and int2 are ints and doub is a double, what happens in the following statement?
doub=int2/int1;
(a) Truncation will occur during division.
(b) No truncation occurs since the result is being stored in doub.
(c) int1 and int2 are truncated before division.
(d) The compiler refuses to do this without an explicit cast.
(a)

(4) If a and b are two ints and a>=b, what is the expression (a-(a/b)*b) always equal to?
(a) a%b
(b) a*b
(c) 2a
(d) 0
(a)

(5) In the statement
result = n * m++;
what happens?
(a) m is incremented and then result is computed as n*m
(b) result is computed as n*m and then m is incremented
(c) result is (n*m) + 1
(d) result is computed as n*(m+1) and then m is incremented
(b)

(6) In the statement
public static final double PI = 3.14159
PI cannot be changed because...?
(a) it is in capital letters
(b) of the word public
(c) of the use of the = sign
(d) of the word final
(d)

(7) If String name = "Thurston", what is name.substring(4)?
(a) "Thur"
(b) "r"
(c) "ston"
(d) "rston"
(c)

(8) If all variables are floats or doubles, how should you test if force is the same as mass*acceleration?
(a) if (force == mass * acceleration)
(b) if (force - mass * acceleration == 0.0)
(c) if (force.equals(mass * acceleration))
(d) if the difference between force and mass*acceleration is some very small amount
(d)

(9) Which is correct about switch(expression)?
(a) expression must be an int
(b) expression must be an int or String
(c) expression must be an int or char
(d) expression must be an int, float, or double
(c)

(10) Which loop(s) can execute the loop body zero times?
(a) while
(b) do-while
(c) for
(d) (a) and (c) but not (b)
(d)

(11) What is "short-circuit evaluation"?
(a) only evaluating as much of a Boolean expression as is necessary to determine its value
(b) evaluating the entire Boolean expression so that each sub-expression is evaluated
(c) the same as "complete evaluation"
(d) halting evaluating of a Boolean expression as soon as it is evident that it is true
(a)

(12) Class Box has an instance variable height and a method
public void setHeight (double amount)
{
height = amount;
}
Which line is equivalent to the statement in the method?
(a) height = this.amount;
(b) this.height = amount;
(c) this.height = this.amount;
(d) None of the above!
(b)

(13) Which is true about a method with return type void?
(a) It can end with return;
(b) It can end with return(expression);
(c) It can end with return(0);
(d) It cannot end with return;
(a)

(14) A variable schizo is declared as a float at the beginning of a method and is re-declared as a char later in the method. How does Java tell which type it is each time it appears?
(a) It depends upon an explicit cast ... that must be used in this situation.
(b) The above causes a syntax error. A variable name can be declared only once per method!
(c) It depends upon the type of data (float or char) being stored.
(d) It depends upon the last declaration just before the current appearance.
(b)

(15) What is the simplest way to explain parameter passing to Java methods?
(a) Primitives are call-by-reference. Class objects and arrays are call-by-value.
(b) Primitives are call-by-value. Class objects and arrays are call-by-reference.
(c) Primitives, class objects, and arrays are all call-by-reference.
(d) Primitives and arrays are call-by-value. Class objects are call-by-reference.
(b)

(16) Java provides a default constructor...?
(a) if you supply no constructors at all.
(b) if you do not create a default constructor, even if you supply any other constructors.
(c) only for classes whose superclass does not have a default constructor.
(d) always!
(a)

(17) double rating = new double[100];
What is the prefered way to loop through all 100 ratings?
(a) for (i=0; i<100; i++)
(b) for (i=1; i<=100; i++)
(c) for (i=0; i<=99; i++)
(d) for (i=0; i<rating.length; i++)
(d)

(18) Which of the following can result in one or more elements of the rating array in the question above being modified?
(a) possible = average (rating[i]);
(b) possible = average (rating[i], rating[j]);
(c) possible = average (rating);
(d) possible = average (rating.length);
(c)

(19) int[][] table = new int[10][6];
What does table.length return?
(a) rows * columns
(b) number of columns
(c) number of rows
(d) 16
(c)

(20) int[][] table = new int[10][6];
What does table[j].length return?
(a) depends on value of j
(b) 6
(c) 10
(d) 60
(b)

(21) If you are certain that you do NOT want any subclass method to override this one, what do you put in its header?
(a) final
(b) private
(c) static
(d) last
(a)

(22) public void villify() throws OvertimeException, UnderworkException, OutOfDataException
Which of the following can override villify() in a derived class?
(a) public void villify() throws OverflowException
(b) public void villify() throws OutOfDataException
(c) public void villify() throws OvertimeException, UnderworkException, OutOfDataException, OverflowException
(d) public void villify() throws OutOfDataException, OverflowException
(b)

(23) Why is it easier to use FileOutputStream and FileReader classes in text file I/O?
(a) They make I/O go much faster.
(b) PrintWriter and BufferedReader have no constructor methods.
(c) PrintWriter and BufferedReader cannot take file names as Strings as arguments for their constructors.
(d) FileOutputStream and FileReader cannot take file names as Strings as arguments for their constructors.
(c)

(24) Which of the following is the DataOutputStream method to write Strings to the output stream?
(a) writeUTF()
(b) writeString()
(c) writeStream()
(d) writeChars()
(a)

(25) Since most methods that read from binary files throw an EOFException, how do the while loops generally look that read from binary files?
(a) while (true)
(b) while (line != 0)
(c) while (line != null)
(d) while (line != EOFException)
(a)

(26) Vector v = new Vector(20, 3);
If this vector needs more locations, it will be increased from 20 to ...?
(a) 23
(b) 30
(c) 40
(d) 60
(a)

(27) Vector v = new Vector();
...starts the Vector with capacity...?
(a) 0
(b) 1
(c) 5
(d) 10
(d)

(28) Which of the following vectors will double its capacity as necessary?
(a) Vector v = new Vector(50,100);
(b) Vector v = new Vector(50,50);
(c) Vector v = new Vector(50,2);
(d) Vector v = new Vector(50);
(d)

(29) What is a base case in recursion?
(a) a call to this method
(b) a way to complete the method without executing a recursive call
(c) a call to another method which eventually calls this method
(d) a way to make the argument(s) different on each recursive call
(b)

(30) In BorderLayout
content.add(label1);
is equivalent to...?
(a) content.add(label1, BorderLayout.MAIN);
(b) content.add(label1, BorderLayout.NORTH);
(c) content.add(label1, BorderLayout.CENTER);
(d) content.add(label1, BorderLayout.EAST);
(c)

(31) public class Reform extends JFrame implements ActionListener
What method must appear in Reform?
(a) addActionListener()
(b) getActionCommand()
(c) actionPerformed()
(d) windowClosed()
(c)

(32) If you are converting a JApplet to an Applet, what becomes of...?
contentPane.add(stopButton);
(a) Jadd(stopButton);
(b) contentPane(stopButton);
(c) add(stopButton);
(d) addElement(stopButton);
(c)

(33) In the actionPerformed() method, you treat a JMenuItem exactly the same way you do a...?
(a) JMenu
(b) JMenuBar
(c) JMenuButton
(d) JButton
(d)

(34) ImageIcons can be added to all of the following except ...?
(a) labels
(b) buttons
(c) menu items
(d) panels
(d)

(35) The WindowAdapter interface requires you to define ...?
(a) only windowClosing()
(b) windowOpened() and windowClosed()
(c) only the method(s) you want to use
(d) all seven window methods
(c)

(36) setVisible(true) and setVisible(false) can be used to make labels and buttons visible or not. But, no change actually happens until after ...?
(a) paint();
(b) validate();
(c) repaint();
(d) update();
(b)

(37) What is the object g in front of all those g.drawThis() and g.fillThat()?
(a) JPanel object
(b) Container object
(c) ContentPane object
(d) Graphics context object
(d)

(38) The color of the outline of the g.drawThis() and the inside of the g.fillThat() are determined by the last...?
(a) g.Color()
(b) g.setColor()
(c) g.setContentPane()
(d) g.setDrawing()
(b)

(39) For any...
g.drawArc(x, y, width, height, beginAngle, sweep);
...there is always an equivalent statement that draws the same arc but in the other direction. Which is it?
(a) g.drawArc(x, y, width, height, beginAngle-sweep, sweep);
(b) g.drawArc(x, y, width, height, beginAngle, -1*sweep);
(c) g.drawArc(x, y, width, height, beginAngle+sweep, -1*sweep);
(d) g.drawArc(x, y, width, height, beginAngle+sweep, sweep);
(c)

(40) What method should be called to call paint() to update any graphics?
(a) paint();
(b) repaint();
(c) validate();
(d) paintover();
(b)



CS 180 Spring 2002
Final Exam

Part 2. Written Problems

Name: _________________________________

Section:________________________________

Student ID Number:______________________

Write your Name, Section, and Student ID Number. Without this information the exam will be graded as zero.

For your section number, use 0730, 0830, 1130, 1230, 0130, or 0230 -- based on the start time of your Friday recitation.

Each code segment is worth 20 points. Problems will be graded on the merit of correct ideas. Only the last few points will be reserved for syntactic correctness (perfect code). Thus, writing explanatory pseudo-code in place of parts you cannot figure out is preferable to leaving the question blank.

IMPORTANT:
(1) Assume that any variable names in bold are already declared for you.
(2) Unless otherwise stated, we only want code fragments. There is no need to write a main method or ask for input if we have already given it to you.
(3) Do not input from the user unless the problem specifically tells you to.

(1) Write some Java code to compute x - x/2 + x/3 - x/4 + x/5 + ... - x/20 where x is a value of type double. Use a loop of some type. In the loop you must use += and *=. (Notice the alternating + and - signs in this formula).

double total = 0.0; double value = x; for (int i=1; i<=20; i++) { total += value/i; value *= -1; }
(2) Create the class Box with three private integer instance variables for the length, width, and height. Create three public methods:
(a) setBox -- If cardboard is an object of Box, cardboard.setBox(30,7,41) would set the length, width, and height to 30, 7, and 41 respectively.
(b) toString -- If cardboard is the box above, cardboard.toString() would return the character string "30x7x41".
(c) changeBox -- If lunchBox is 15x12x27, lunchBox.changeBox(cardboard) would change cardboard to 15x12x27. Note that changeBox changes the argument, not the caller.

public class Box { private int length; private int width; private int height; public void setBox (int len, int wid, int ht) { length = len; width = wid; height = ht; } public String toString () { return (length + "x" + width + "x" + height); } public void changeBox (Box someBox) { someBox.length = this.length; someBox.width = this.width; someBox.height = this.height; } }
(3) The Tool class has a constructor Tool(int) used to initialize the length in inches of any tool. The Hammer class is a subclass of Tool. It has an instance variable of type double named power that indicates the amount of hitting force that the hammer can generate. Write the header (first line) of class Hammer and the Hammer constructor that initializes the Hammer's length and power. Be sure to make use of the Tool(int) constructor. Do not worry about declaring the class' data.

Also, write the Hammer class method display() that prints the Hammer's power and then invokes the overridden Tool method display() to print the rest of the information about the Hammer.

public class Hammer extends Tool { public Hammer(int length, double force) { super (length); power = force; } public void display() { System.out.println ("Hammer Power: " + power); super.display(); }
(4) A square array has n rows and n columns. A triangle array is the upper left corner of a square array in which the first row has n columns, the second row has n-1 columns, ..., the nth row has 1 column. Write a method named triangle that takes one integer parameter n and that creates and returns a triangle array of doubles of size n. Be sure to make a RAGGED array. Initialize each element to the row number. Thus, a call to triangle (4) should return the array:

1.0 1.0 1.0 1.0
2.0 2.0 2.0
3.0 3.0
4.0

public static double[][] triangle (int n) { double a[][] = new double[n][]; for (int i=0; i<a.length; ++i) { a[i]=new double[n-i]; for (int j=0; j<a[i].length; ++j) { a[i][j]=i+1; } } return (a); }
(5) Write a for loop that sums the even members of a double array x (that is x[0], x[2], x[4], ..., x[100]), but leaves the for loop if that sum ever becomes greater than the constant ZIPPO.

double sum = 0.0; for (i = 0; i <= 100; i=i+2) { sum += x[i]; if (sum > ZIPPO) break; }
(6) Write a method named descend() which takes a single parameter which is an array of doubles and which returns an int count of the number of elements in that array which are larger than the element immediately following it. (For example, if the array consists of 9, 6, 7, 4, 2, 2, 3, 1, descend() would return 4, because 9>6, 7>4, 4>2, and 3>1.)

public static int descend (double x[]) { int count = 0; for (int i=1; i<x.length; ++i) if (x[i-1]>x[i]) count++; return count; }
(7) Write a RECURSIVE method to display an integer number with embedded and trailing pound signs. For example, if the number is 4763, your method should print "4#7#6#3#".

static void poundDigits (int number) { if (number < 10) System.out.print (number + "#"); else { poundDigits (number/10); System.out.print (number%10 + "#"); } }
(8) Declare an object studyList which is a Vector. In a loop prompt the user each time and read in exactly 10 character strings to go on that studyList. You may use SavitchIn for input. When the loop ends, change the last item in the studyList to "Study for CS 180 Final"; use the size() method to do this. Be sure to include an import statement so that you can use Vectors.

import java.util.*; Vector studyList = new Vector(10); String next; for (int i=0; i<10; i++) { System.out.println("Input a study item:"); next = SavitchIn.readLine(); studyList.addElement(next); } studyList.setElementAt("Study for CS 180 Final", studyList.size()-1);