Hover the mouse over the colored boxes to see notes and answers
(1 pt. each) | |
House and Door are Java classes. Assuming that the code fragment below
compiles and executes without error, draw (or imagine carefully) a picture (in the official 143
style) to illustrate the
resulting state of the program at the conclusion of the sequence of
instructions:
List duplex = new ArrayList(); duplex.add(new House()); House temphouse = new House(); duplex.add(temphouse); temphouse = (House) duplex.get(0); temphouse.frontdoor = new Door(); temphouse.sidedoor = new Door(); temphouse2 = new House(); temphouse2.frontdoor = new Door(); temphouse2.sidedoor = temphouse.sidedoor; duplex.add(0, temphouse2); Object returnHouse = duplex.get(1);
|
Just after these statements have executed, there are... . ____ how many objects of type List? . ____ how many objects of type ArrayList . ____ how many objects of type House? . ____ how many objects of type Door? . ____ how many bindings are there to (arrows pointing at) objects of type Door? Answer with this code: A: 0, B: 1, C:2, D: 3, E: 4. |
B, B, D, D, E The object named duplex is of type List and also of type ArrayList. |
(1 pt. each) | |
Answer: A if the line (in isolation, in an otherwise valid Java program) is good (has no compile error). B if the line is bad (has a compile error): |
. ___ int i1 = new int(3); . ___Integer i2 = (Integer) 3; . ___int i3 = (int) (new Integer(3)); . ___Integer i4 = (int) '\u2809'; . ___char c1 = (char) 4567; . ___char c2 = 1A39; |
B. ints do not have constructors. B. Only an object can be cast to another object (and not always even then). B. An object cannot be cast to an elementary type B. An object cannot be cast from an elementary type. A. B. A character literal must be in single quotes |
. | |
After g.drawstring(mystring, xLoc, yLoc); has executed in a paint method, apparently without error, the string displays as expected, except that one of the character positions has a little box instead of the character you expected to see. Which one of the following is the most plausible explanation? A. The font which g is set to is null B. The font which g is set to does not know how to display that particular character. C. The character code is an int value greater than 64K (more precisely, >= 65536) D. The Unicode standard has not provided a definition of the character that you wished to display. |
|
B.
Not C: Characters in Java are by definition 16 bits (max 64K). Not D: Unicode is only a standard. It cannot force a font to display any particular character, or prevent a font from displaying any character for any value. |
. | |
It is legal in Java to compare two characters, e.g.: return (initial <= 'M'); Which of the following are accurate concerning character comparisons? A. The comparison follows the usual conventions of the Java Comparable interface B. The comparison is based on the (int) values of the characters being compared C. In the Unicode standard, if a particular hex value does not have a character defined for it, then the result of comparing it with a defined character cannot be predicted.
|
A. A only B. B only C. C only D. A and B only E. A and C only |
B. Not A: a character conversion, as shown
in the example, has a Boolean value, whereas compareTo returns an int. |
. | |
Concrete class StringWriter extends the Writer abstract class.
One non-abstract method of Writer is write, a void method which takes
a char as argument. A. If Writer.write(char) throws an IOException, then StringWriter.write(char) must throw an IOException. B. If Writer.write(char) has a throws clause, then StringWriter.write(char) must have a throws clause. C. If StringWriter.write(char) has a throws clause, then Writer.write(char) must have a throws clause. |
A. A only B. B only C. C only D. A and B only E. A and C only |
C only.
Not A: A throws IOException clause simply says that the method might, directly or indirectly, throw an IOException. It does not obligate the the method to throw such an exception. Likewise for any overriding method in a subclass. Not B: The chief principle in inheritance is that an object of a subtype must be usable anywhere where an object of a supertype can be used. In this case, the supertype method might throw a checked exception, so the compiler must allow for that worst case, regardless of whether the method, or any of its overriding methods, actually does throw an IOException. If an overriding method knows for sure that it cannot throw that exception, then it doesn't have to throws it. This will allow the compiler to be more lenient when it knows that the object is in fact of the subclass type, but it doesn't affect the compiler's decision when the runtype type (subtype) is know. C: A subclass method cannot be more restrictive in its exceptions (or
access modifier, or argument types) than the superclass method which is
overrides (actually, if there is a change of argument types, the situation
is one of overloading instead of overriding, so the same rules don't apply).
At compile time, the compiler must "know the worst", and base its type
compatibility decisions on what it knows: the supertype, and not on the
possibly more lenient case of the actual runtime type. |
(3 pts each)
Study the following method, equestion. The instance variable
sw has been opened and is
ready to write. Assume that in the following code, sw does not throw
any exceptions. The write method of sw works as
expected: sends its character or string arguments to an output character
stream.
public double equestion (double humidity[]) { double average = 0; try { int aIndex; try { aIndex = -1; double hsum = 0; while(aIndex <= humidity.length) { try { aIndex++; if (humidity[aIndex] > 0.9) { //Whew! It's humid this.sw.write("h "); } hsum += humidity[aIndex]; } catch (ArrayIndexOutOfBoundsException e) { this.sw.write("ae1 "); } catch (Exception e) { this.sw.write("ee1 "); } } average = hsum / humidity.length; } catch (ArrayIndexOutOfBoundsException e) { this.sw.write("ae2 "); } } catch (Exception e) { this.sw.write("e3 "); } return average; } |
At the time when the method execution terminates (whether normally or by
throwing an exception that takes control out of the method), what is the first thing
the method has written to the output stream...
. ___ ... if the input argument is null . ___ ... if the input argument has the value new double {0.1} [i.e., an array of length 1 with its element value being 0.1] . ___ ... if the input argument has the value new double {0.95, 0.1} [i.e., an array of length 2 with element values 0.95 and 0.1] A. h B. ae1 C. ee1 D. ae2 E. e3 |
E, B, A |
(1 pt. each) | |
Which of the following must be specified as part of a complete client-implementer contract for a method? (A yes, B no) |
. ___
naming conventions of local variables. . ___ whether or not checked exceptions might be thrown. . ___ what the implementer will do if a precondition is violated. . ___ the conditions under which a null is returned from the method. |
B, A, B, A |
(1 pt. each) | |
For each situation, choose the most effective and appropriate approach, from
the list given, using each only once: A. unchecked exception B. checked exception C. assert statement D. status return value
|
. ญญ___
debugging a method that you wrote.
. ญญ___ signaling to a client that it supplied an illegal argument. . ญญ___ notifying client that one of a number of expected and documented situations has occurred. . ญญ___ notifying client that a documented but unusual (and usually undesirable) situation has occurred.
|
C, A, D, B |
. | |
What is wrong about the following definition of a "loop invariant"? "A loop invariant is a condition that is true at every point inside a loop." A. It fails to consider that the loop might not be executed at all. B. The word "every" is too strong. C. The invariant might also be true outside the loop. |
A. A only B. B only C. C only D. All of A, B, and C E. None of A, B, or C |
B only. A and C are irrelevant to the
definition, regardless of whether they might be true in some cases. |
. | |
What of the following describes a true relationship between class
invariants, preconditions, loop invariants, and postconditions? A. Every class invariant is a postcondition of every one of its public methods. B. If a condition is both a precondition and a postcondition, then it is also a loop invariant. C. A class invariant must be true at every point inside a method.
|
A. A only B. B only C. C only D. All of A, B, and C E. None of A, B, or C |
A only. Not C: class invariants are
regularly violated while a method is updating instance variables, but must
be true again when the method exits. |
. | |
Comparing a TreeSet and a HashSet... A. They have identically (or practically identically) the same public methods. B. For those public methods which the types of sets have in common, the pre- and postconditions are identical (or practically identical). C. The words "Tree" and "Hash" which occur in the class refer to the internal structure by which the data is stored in the set. |
A. A only B. B only C. C only D. Exactly two of A, B, and C E. All of A, B, and C |
E. All of the above. |
(1 pt. each) | |
The exact complexity function for a particular algorithm S is given by s(n) = 300 + n^2. Another algorithm, T, for the same task has a complexity function t(n) = 10 + 2*n*(5 + n). In both cases, the parameter n indicates the problem size. Answer each question as true (mark A) or false (mark B). |
.___ For a problem size of 10, S has fewer steps than T. .___ For a problem size of 100, S has fewer steps than T. .___ s(n) = O(t(n)) .___ t(n) = O(s(n)) |
B, A, A, A. S and T are both quadratic. |
. (1 pt.) | |
BONUS QUESTION. (There is a programming question (not extra credit) after this question.) You work for a package delivery company, developing
algorithms for routing shipments and trucks. Your boss, who only got
as far as CSE142 in his computing education, has heard that your best
algorithm is "order n". He offers you a bonus if you can improve it to
"order n-squared" (obviously, n-squared is bigger and better). You
should A. Ask him for an additional bonus if you can deliver the new algorithm ahead of schedule. B. Close the door and explain quietly just what you think of him. C. Explain loudly and sarcastically just what you think of him. D. Thank him for entrusting you with the assignment, and then go straight to his boss. E. Other: _____________________________ |
|
A, B, C, D, or E, depending on
your business sense. Some of the answers we read for E would probably
get you fired or arrested. |
. | |
(5 MC questions (15 pts))
For a student registration application, you are asked to implement a linked
list with some special properties and constraints. The class OrderedStudentList should extend the SimpleLinkedList class (instance data as given in lecture, and repeated on the handout). SimpleLinkedList is implemented using the Link class on the handout. The class should have exactly the same public methods and signatures as SimpleLinkedList -- no more, no less. Only instances of the class StudentData can be added to the list. Attempts to add other types of data should result in an unchecked exception. Within the list, objects should be stored in order by studentID (a String value, which is unique per student). Thus, if you went through the list from head to tail, you would get all the students in order of their studentIDs. Clients cannot in any way cause this to be violated. Implement fully the class OrderedStudentList. a) List the method(s) or other changes (briefly!!) you plan to implement (this will help your grade if you run out of time).
b) Write full code for the solution. Write your answer here, and not on the handout. You do not need to repeat here any of the Java on the handout, except to show how your code relates to it. |
|
Four methods needed to be
modified: the three add methods, and set. We graded
add in detail and gave it most of the points. If you at least
mentioned the other methods and didn't mess up too badly on them, you got
credit for that part. Some of the things we looked for in add: Method signature must be exactly as in the superclass (for example, can't change the argument type to StudentRecord). Must somehow cause an unchecked exception if the argument type is wrong (a simple cast to (StudentRecord) is sufficient). Must determine the proper place to put the new record. This includes searching down the list, and making proper comparisons using compareTo. Must properly update the list, once the place is found (i.e., get the links correct). Must properly handle the special case of an empty list. |