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)
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).
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.
1.0 1.0 1.0 1.0
2.0 2.0 2.0
3.0 3.0
4.0