Final Name: Ed Parrish (Preview) Start Time: May 28, 2003 13:03 Time Allowed: 165 minutes Number of Questions: 30 -------------------------------------------------------------------------------- Question 1 (5 points) In Java exceptions of class Error and RuntimeException and their descendants are called ___________ exceptions. 1. unchecked 2. checked 3. implicit 4. explicit -------------------------------------------------------------------------------- Question 2 (5 points) Java provides both interfaces (e.g. MouseMotionListener) and classes called adpaters, (e.g. MouseMotionAdapter) for responding to events. Since the adaptor classes save typing, why not just provide adapter classes and not interfaces? 1. You can only extend one adapter, but you can implement multiple interfaces. 2. Adaptor classes cannot be overridden, which limits their utility. 3. Two interfaces cannot be implemented in one class, unless the class is an adaptor. 4. Typing is an important skill for programmers, and it is important to keep skills polished. -------------------------------------------------------------------------------- Question 3 (5 points) Java is considered a strongly typed language because: 1. The primitive types in Java are portable across all computer platforms that support Java. 2. Java requires all variables to have a type before they can be used in a program. 3. Instance variables of the primitive data types are automatically assigned a default value. 4. All of the above. -------------------------------------------------------------------------------- Question 4 (5 points) The type of a variable specifies: 1. its value 2. its name 3. the operations that are permitted on it 4. its value and name -------------------------------------------------------------------------------- Question 5 (5 points) Which statement of the following is true about arrays? 1. Once you have created an array, you can change its size. 2. The last element of an array of size n is at index n. 3. 2D arrays are twice as big as 1D arrays. 4. None of the above statements are true. -------------------------------------------------------------------------------- Question 6 (5 points) Which of the following is not an algorithm? 1. A recipe 2. Operating instructions 3. A textbook index 4. Shampoo instructions (lather, rinse, repeat) -------------------------------------------------------------------------------- Question 7 (5 points) What is the name of the method a ServerSocket uses to listen for connections at a port? 1. listen 2. accept 3. waitClient 4. connection -------------------------------------------------------------------------------- Question 8 (5 points) Which of the following statements are false? 1. The test expression in an if statement can contain method calls. 2. If a and b are of type boolean, the expression (a=b) can be the test condition of an if statement. 3. An if statement can have either an if clause or an else clause, but not both. 4. The statement if(false);else; is legal. 5. Only expressions which evaluate to a boolean value can be used as the condition of an if statement. -------------------------------------------------------------------------------- Question 9 (5 points) Enter the name of the method used to convert a String to an int. Answer: -------------------------------------------------------------------------------- Question 10 (5 points) The purpose of testing is to make sure your program: 1. Does what it is supposed to do 2. Doesn't do what it is not supposed to do 3. Does what it used to do 4. All of the above -------------------------------------------------------------------------------- Question 11 (5 points) A derived class is called a ____________ if it has an is-a relationship with its parent class. 1. Subclass 2. Virtual class 3. Package 4. Subtype -------------------------------------------------------------------------------- Question 12 (5 points) A programmer-defined exception class 1. can be derived from any existing class 2. can be defined with a single constructor 3. should not have more than one instance variable 4. contains at least one int variable to store the exception code number -------------------------------------------------------------------------------- Question 13 (5 points) Which statement of the following is true about public vs. private? 1. Public methods can only access public data elements 2. Private data elements are accessible by methods in the same package. 3. By default, all data elements are private. 4. Private methods are accessible in subclasses. -------------------------------------------------------------------------------- Question 14 (5 points) What Java class defines the methods wait() and notify()? 1. Object 2. Thread 3. System 4. Runner -------------------------------------------------------------------------------- Question 15 (5 points) If a thread calls wait() and is then awakened via a notify(), the thread must still wait for what before being allowed to run? 1. the condition variable to be set to true 2. the current thread to terminate 3. the lock to be released 4. nothing - it can begin running immediately -------------------------------------------------------------------------------- Question 16 (5 points) To exit out of a loop completely, and resume the flow of control at the next line in the method, use which of the following: 1. A continue statement 2. A break statement 3. A return statement 4. Any of the above -------------------------------------------------------------------------------- Question 17 (5 points) Method overloading is when you write a method that 1. has too much code in it 2. has the same name and parameters as another method in the class 3. has the same name but different parameters than another method in the class 4. has the same name and parameters as the superclass -------------------------------------------------------------------------------- Question 18 (5 points) Only one __________ method may be active on an object at once. 1. synchronized 2. ordered 3. exclusive 4. thread -------------------------------------------------------------------------------- Question 19 (5 points) Enter the name of the method called so that changes to the graphics being displayed will show on the screen. Answer: -------------------------------------------------------------------------------- Question 20 (5 points) Match the following file I/O objects with their intended purpose. FileInputStream --> Choose match byte-based outputbyte-based inputtext-based inputtext-based output FileOutputStream --> Choose match byte-based outputbyte-based inputtext-based inputtext-based output FileReader --> Choose match byte-based outputbyte-based inputtext-based inputtext-based output FileWriter --> Choose match byte-based outputbyte-based inputtext-based inputtext-based output -------------------------------------------------------------------------------- Question 21 (5 points) Write a method that tests whether the formula a2 + b2 = c2 is true for three integers passed as parameters and returns the result of this test. Such a triple is a Pythagorean triple and forms a right-angle triangle with these numbers as the lengths of its sides. Starter code is available here. Enter the completed code in the box below. -------------------------------------------------------------------------------- Question 22 (5 points) Write a single-line of code that both declares and instantiates a FileWriter object and assigns the object to a variable named fw. Open the file so that it will append data to the end of a file named "myfile.txt". Answer: -------------------------------------------------------------------------------- Question 23 (5 points) Write a code fragment that when given a char c, displays "A", "B.", or "C" depending on whether the value of c is ‘a’, ‘b’, or ‘c’. Use a switch statement for your solution. Starter code is available here. Enter the completed code in the box below. -------------------------------------------------------------------------------- Question 24 (5 points) Write a code fragment that when given a double called celsiusTemp, displays "Within range" or "Out of range" depending on whether or not celsiusTemp is greater than or equal to 0 and less than 100. Starter code is available here. Enter the completed code in the box below. -------------------------------------------------------------------------------- Question 25 (10 points) Write a method called pairCount() that takes as a parameter an array of characters and returns a count of the number of pairs of characters (consecutive elements of the array with the same value). For example, the array {'a', 'b', 'b', 'a'} returns 1 because 'b' and 'b' are next to each other. Starter code is available here. Enter the completed code in the box below. -------------------------------------------------------------------------------- Question 26 (10 points) One way to calculate square root of a number n is to guess that the root is some number x between 0 and n, and then repeatedly refine your guess x, by setting x equal to the average of x and n / x, until the difference between your x2 and n is very small. Show how you could implement squareRoot() using this method to return the square root to within 0.0001 of a number passed as a parameter. Starter code is available here. Enter the completed code in the box below. -------------------------------------------------------------------------------- Question 27 (10 points) Write a main() method that reads in four doubles and displays "yes" if the numbers were entered in increasing order and displays "no" otherwise. You may assume the existence of the Console class with a static method readDouble() that we used during the course. Enter the completed code in the box below. -------------------------------------------------------------------------------- Question 28 (10 points) Write a method called divisors() that takes as a parameter an int n, and uses a for loop to find and display the positive integer divisors of n (i.e. all integers that evenly divide into n). Do not use any other methods - compute the divisors yourself. divisors() should return the number of integer divisors of n. Starter code is available here. Enter the completed code in the box below. -------------------------------------------------------------------------------- Question 29 (20 points) Implement a class called Coordinate that allows the user to store an (x, y) coordinate. Provide: A constructor that sets both variables to values passed as parameters. An instance method called add() that adds two Coordinates and returns a new Coordinate containing the sum of the other two. A static method called distance() that returns the distance between two Coordinates. Recall that the distance between two coordinates (x1, y1) and (x2, y2) is sqrt((x2 - x1)2 + (y2 - y1)2). You may use the method Math.sqrt() to calculate the square root. A toString() method that returns a String of the coordinates within parenthesis. Some test code in a main() method that declares two Coordinates A and B and adds them and stores the result in C. Enter your code in the box below. -------------------------------------------------------------------------------- Question 30 (20 points) Implement a class called FileDisplayer that reads a text file and displays it in the JTextArea of a JScrollPane. The appication must perform the following actions: When the user types a file name and presses the Enter key while in the JTextField, the chosen file is displayed in the display panel If an incorrect file name is entered, a pop-up message is displayed with an error message Pressing the clear button clears the display panel When the user closes the application (by clicking the X-button), the program exits completely Construct the application to include the following: A constructor that creates a GUI similar to the one shown in the figure below An instance method called readFile() with a file name parameter that opens and reads a text file A main() method to instantiate the class. Appropriate methods to respond to ActionEvents generated by the GUI. Starter code is available here. Enter the completed code in the box below. --------------------------------------------------------------------------------