1 pt. each |
|
In
Java, Reader... |
.
...is
an interface or abstract class . ...is a concrete class . ...has a method to read characters . ...has a method to read bytes . ...has a method to read a line at a time |
abstract class, reads
(Unicode) characters A, B, A, B, B |
. | |
/**
Method (from slides) to copy a file */ public void copyFile(String srcFilename, String destFilename) throws IOException { PrintWriter outFile = ...... A suitable completion of the line shown is... |
A.
new destFilename(); B. new PrintWriter(); C. new PrintWriter(new BufferedWriter(new FileWriter(destFilename))); D. new File(destFilename); E. new Writer(new File(destFilename))) |
C |
1 pt. each |
|
A
valid Java program might contain the code catch (java.io.IOException e) {... } True or False: |
.
IOException
is a subclass of RuntimeException . IOException is a subclass of io . IOException is a subclass of Throwable . IOException implements Exception . The dynamic type of e might be FileNotFoundException |
B; B; A; B (Exception is a
concrete class); A |
1 pt. each | |
Assume
the following code compiles correctly. Assume callSomeMethod doesn't print
anything. try { System.out.print("A"); callSomeMethod(); System.out.print("B"); } catch (ExType1 e1) { System.out.print("C"); } catch (ExType2 e2) { System.out.print("D"); throw e2; } catch (Exception e) { System.out.print("E"); } System.out.print("F"); With no other knowledge, say whether each of the following is or is not a possible outcome of this code (outcome meaning, everything the code shown prints when it executes, as completely as we can determine). Answer A for possible, B for not possible. |
. A . AD . AF . ACF . ADF . AEF . ABF . ACBF . ABCDEF |
A; A; B; A; B; A; A; B; B The possible outcomes are: A if callSomeMethod never returns (System.exit, assert, etc.) |
. | |
In
a method public void paintComponent(Graphics gr) {... a typical line early in the method would be... (pick one) |
A.
Graphics2D g2 = new Graphics2D(gr); B. Graphics2D g2 = new Graphics2D( ); C. Graphics2D g2 = (Graphics2D) gr; |
C |
. | |
In
a method public void paintComponent(Graphics gr) {... In order to display a String colored red, the proper procedure is... |
A.
Set the string's color to red before
displaying it B. Set the string's color to red just after displaying it C. Set gr's color to red just before the string is displayed D. Set this color of this to red just before the string is displayed |
C |
. | |
In
a method public void draw(Graphics2D g) {... Assuming that myRec is a properly created Rectangle object of the desired size and position, what is the correct way to display myRec? |
A.
myRec.draw(); B. this.draw(myRec); C. g.draw(myRec); |
C |
. | |
The
class typically used for the top-level window in a Java Swing
application is a... |
A.
Window B. JWindow C. JFrame D. JPanel |
C |
. | |
Rank
these functions in order of asymptotic complexity, from best
(fastest) to worst (slowest). f(m) = 3 + 20 * m * log m + 7 * m g(t) = .01 * t2 + 900 h(n) = 1 + 2n |
A.
f, g, h B. f, h, g C. h, f, g D. g, h, f E. h, g, f |
A nlogn, quadratic, exponential |
.1 pt. each | |||||||||||||
In
class we studied implementing a list with arrays(SimpleArrayList)
and using a linked
structure (SimpleLinkedList). Assume the classes are
defined in the same
general fashion as we did them in class (but with no size or numElems
instance variable)., what are the asymptotic (Big-O) complexities
(worst case) of the methods? Fill in each blank entry of the
table. Clarification during the test: no "last" referenence in the linked list |
|
||||||||||||
|
. worth 3 M.C. questions | |
Analyze
the method below for its execution complexity.
Come up with the exact complexity function (worst case) as best you
can; make notes on the code if it will help explain how you got the
answer. Write the final answer
here: Then figure out the asymptotic complexity in its simplest form and write it here in Big-O notation: Finally, write the short descriptive name of the asymptotic complexity class: public static int countSomething(int[] raindrops) { int result = 0; int iter = 0; while (iter < 10 && iter < raindrops.length) { if (raindrops[iter] > 0) { result++; } iter++; } return result; } |
|
84 -- O(1) -- constant time.
Regardless of the lengthof the raindrops array, there are never more than 10 iterations of the loop. Following the step-counting guidlines presented in class, there are appoximately 8 steps for each of the iterations, and a few steps before and after the loop, so a total exact complexity of 84 would be a reasonable estimate (a wide range of answers was accepted for the first part, as long as they did not contain a variable like n). |
. Worth 9 M.C. questions | |
America's
Favorite Beverage.
A marketing agency
wants to know what beverage is preferred by the largest number of
people in a recent survey. A program has been written which reads
the
survey data into a linked list of Strings, one per person
surveyed. The strings are names of beverages: they are spelled
and capitalized correctly, are already trimmed for spaces, and there
are no nulls or empty strings. The list is in no particular
order. Implement the findFavoriteBeverage method.
Use the SurveyList and Link class as given below (very similar to what
was used in lecture, but with none of the list class methods
implemented! In particular, there is no get method or iterator method). Hint: although the parameter of findFavoriteBeverage of of the weird SurveyList type, you can use classes of the Java Collections framework elsewhere in your solution as desired. For example, a Map might be handy... /** Link class for a linked list of survey responses *./ public class Link { public String item; public Link next; public Link(String data, Link nextLink); // constructor this.item = data; this.next = nextLink; } /** Linked list class for the survey */ public class SurveyList { public Link first = null; // There are NO other methods! } /** Return the name of the beverage chosen as "favorite" by the largest number of people. */ public static String findFavoriteBeverage(SurveyList bevNames) {.... //IMPLEMENT THIS |
/
|
Executable test code will be posted separately. Handout: Javadoc method summary for the Map interface. Using a Map is an elegant solution (key is beverage name, value is number of fans of that beverage), but not the only possibility. Many people used two (parallel) lists, one with beverage names, and the other with fan counts. The two chief components in grading were: correctly traversing the linked list, and keeping track of the number of people preferring each beverage. You also had to correctly determine and return the name of the beverage with the most fans. A Map solution is (ideally) O(N) where N is the number of people surveyed. Using lists, the efficiency could be as bad as O(N*N), although if the number of beverages M is a constant, the complexity O(N*M) is linear in N. Following is a solution using a Map. Full testable code for this will be posted separately, along with a non-Map solution. ------------------------------------------------------------------------- public static String findFavoriteBeverage(SurveyList bevNames) { //IMPLEMENT THIS //Solution using a Map Map uniqueNames = new HashMap(); //create an empty Map String favorite = "no favorite yet"; int howManyOfFavorite = 0; Link currentLink = bevNames.first; while (currentLink != null) { //go through all the survey responses String currName = currentLink.item; Integer occurrences = (Integer) uniqueNames.get(currName); int howManySoFar; if (occurrences == null) { howManySoFar = 0; } else { howManySoFar = occurrences.intValue(); } howManySoFar++; uniqueNames.put(currName, new Integer(howManySoFar+1)); if (howManySoFar > howManyOfFavorite) { //got a new favorite favorite = currName; howManyOfFavorite = howManySoFar; } currentLink = currentLink.next; } return favorite; } |