Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CSC-220 Data Structures Exam #3 Chapters 12 and 13 Closed Book / Closed Notes MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) What is the output of running class Test? 1) public class Test { public static void main(String[] args) { new Circle9(); } } public abstract class GeometricObject { protected GeometricObject() { System.out.print(ʺAʺ); } protected GeometricObject(String color, boolean filled) { System.out.print(ʺBʺ); } } public class Circle9 extends GeometricObject { /** Default constructor */ public Circle9() { this(1.0); System.out.print(ʺCʺ); } /** Construct circle with a specified radius */ public Circle9(double radius) { this(radius, ʺwhiteʺ, false); System.out.print(ʺDʺ); } /** Construct a circle with specified radius, filled, and color */ public Circle9(double radius, String color, boolean filled) { super(color, filled); System.out.print(ʺEʺ); } } A) AEDC B) ABCD C) BEDC 1 D) CBAE E) BACD 2) Analyze the following code. 2) Number[] numberArray = new Integer[2]; numberArray[0] = new Double(1.5); A) Since each element of numberArray is of the Number type, you cannot assign a Double object to it. B) Since each element of numberArray is of the Number type, you cannot assign an Integer object to it. C) At runtime, new Integer[2] is assigned to numberArray. This makes each element of numberArray an Integer object. So you cannot assign a Double object to it. D) You cannot use Number as a data type since it is an abstract class. 3) Which of the following is a correct interface? A) abstract interface A { print(); } B) interface A { void print() { }; } C) abstract interface A { abstract void print() { };} D) interface A { void print();} 3) 4) Show the output of running the class Test in the following code lines: 4) interface A { } class C { } class B extends D implements A { } public class Test { public static void main(String[] args) { B b = new B(); if (b instanceof A) System.out.println(ʺb is an instance of Aʺ); if (b instanceof C) System.out.println(ʺb is an instance of Cʺ); } } class D extends C { } A) b is an instance of A followed by b is an instance of C. B) b is an instance of A. C) Nothing. D) b is an instance of C. 2 5) The printout from the following code is ________. 5) java.util.ArrayList<String> list = new java.util.ArrayList<String>(); list.add(ʺNew Yorkʺ); java.util.ArrayList<String> list1 = (java.util.ArrayList<String>)(list.clone()); list.add(ʺAtlantaʺ); list1.add(ʺDallasʺ); System.out.println(list1); A) [New York, Atlanta] C) [New York] B) [New York, Dallas] D) [New York, Atlanta, Dallas] 6) What is the best suitable relationship between Employee and Faculty? A) Aggregation B) Composition C) None 6) D) Inheritance 7) Assume an employee can work for only one company. What is the best suitable relationship between Company and Employee? A) Inheritance B) Composition C) Aggregation D) None 7) 8) All the numeric wrapper classes implement the Comparable interface. A) true B) false 8) 9) You can create an Integer object from an integer by using the Integer constructor with the int argument. A) true B) false 9) 10) You cannot create an instance of an abstract class using the new operator. A) true B) false 10) 11) An abstract class can be extended. A) true 11) B) false 12) A subclass of a non-abstract class must be non-abstract. A) true B) false 12) 13) All methods in an interface are abstract. A) true 13) B) false 14) At least one method in an abstract class must be abstract. A) true B) false 14) 15) A subclass cannot extend more than one class, but may implement any number of interfaces. A) true B) false 15) 3 16) Analyze the following code. 16) 1. public class Test { 2. public static void main(String[] args) { 3. Fruit[] fruits = {new Fruit(2), new Fruit(3), new Fruit(1)}; 4. java.util.Arrays.sort(fruits); 5. } 6. } class Fruit { private double weight; public Fruit(double weight) { this.weight = weight; } } A) The program has a runtime error on Line 3 because the Fruit class does not have a default constructor. B) The program has a runtime error on Line 4 because the Fruit class does not implement the java.lang.Comparable interface and the Fruit objects are not comparable. C) The program has a compile error because the Fruit class does not have a default constructor. D) The program has a compile error on Line 4 because the Fruit class does not implement the java.lang.Comparable interface and the Fruit objects are not comparable. 17) 17) Analyze the following code. public class Test { public static void main(String[] args) { Number x = new Integer(3); System.out.println(x.intValue()); System.out.println((Integer)x.compareTo(new Integer(4))); } } A) The program has a compile error because x cannot be cast into Integer. B) The program has a compile error because an Integer instance cannot be assigned to a Number variable. C) The program compiles and runs fine. D) The program has a compile error because the member access operator (.) is executed before the casting operator. E) The program has a compile error because intValue is an abstract method in Number. 4 18) Analyze the following code. 18) public class Test { public static void main(String[] args) { Number x = new Integer(3); System.out.println(x.intValue()); System.out.println(x.compareTo(new Integer(4))); } } A) The program has a compile error because an Integer instance cannot be assigned to a Number variable. B) The program compiles and runs fine. C) The program has a compile error because intValue is an abstract method in Number. D) The program has a compile error because x does not have the compareTo method. 19) Which of the following declares an abstract method in an abstract Java class? A) public void abstract Method(); B) public abstract void method(); C) public abstract method(); D) public void method() {} E) public abstract void method() {} 19) 20) Which of the following class definitions defines a legal abstract class? A) class A { abstract void unfinished(); } B) public class abstract A { abstract void unfinished(); } C) class A { abstract void unfinished() { } } D) abstract class A { abstract void unfinished(); } 20) 21) A Java exception is an instance of ________. A) RuntimeException B) Exception C) Error D) Throwable E) NumberFormatException 21) 22) An instance of ________ describes system errors. If this type of error occurs, there is little you can do beyond notifying the user and trying to terminate the program gracefully. A) RuntimeException B) Exception C) Error D) Throwable E) NumberFormatException 22) 23) An instance of ________ describes the errors caused by your program and external circumstances. These errors can be caught and handled by your program. A) RuntimeException B) Exception C) Error D) Throwable E) NumberFormatException 23) 5 24) An instance of ________ describes programming errors, such as bad casting, accessing an out-of-bounds array, and numeric errors. A) RuntimeException B) Exception C) Error D) Throwable E) NumberFormatException 24) 25) The following code causes Java to throw ________. 25) int number = Integer.MAX_VALUE + 1; A) RuntimeException B) Exception C) Error D) Throwable E) no exceptions 26) What exception type does the following program throw? 26) public class Test { public static void main(String[] args) { System.out.println(1 / 0); } } A) ArithmeticException B) ArrayIndexOutOfBoundsException C) StringIndexOutOfBoundsException D) ClassCastException E) No exception 27) What exception type does the following program throw? public class Test { public static void main(String[] args) { int[] list = new int[5]; System.out.println(list[5]); } } A) ArithmeticException B) ArrayIndexOutOfBoundsException C) StringIndexOutOfBoundsException D) ClassCastException E) No exception 6 27) 28) What exception type does the following program throw? 28) public class Test { public static void main(String[] args) { String s = ʺabcʺ; System.out.println(s.charAt(3)); } } A) ArithmeticException B) ArrayIndexOutOfBoundsException C) StringIndexOutOfBoundsException D) ClassCastException E) No exception 29) What exception type does the following program throw? 29) public class Test { public static void main(String[] args) { Object o = new Object(); String d = (String)o; } } A) ArithmeticException B) ArrayIndexOutOfBoundsException C) StringIndexOutOfBoundsException D) ClassCastException E) No exception 30) What exception type does the following program throw? public class Test { public static void main(String[] args) { Object o = null; System.out.println(o.toString()); } } A) ArithmeticException B) ArrayIndexOutOfBoundsException C) StringIndexOutOfBoundsException D) ClassCastException E) NullPointerException 7 30) 31) What exception type does the following program throw? 31) public class Test { public static void main(String[] args) { Object o = null; System.out.println(o); } } A) ArithmeticException B) ArrayIndexOutOfBoundsException C) StringIndexOutOfBoundsException D) No exception E) NullPointerException 32) A method must declare to throw ________. A) unchecked exceptions C) RuntimeException 32) B) checked exceptions D) Error 33) Analyze the following code: 33) class Test { public static void main(String[] args) { try { String s = ʺ5.6ʺ; Integer.parseInt(s); // Cause a NumberFormatException int i = 0; int y = 2 / i; } catch (Exception ex) { System.out.println(ʺNumberFormatExceptionʺ); } catch (RuntimeException ex) { System.out.println(ʺRuntimeExceptionʺ); } } } A) The program has a compilation error. B) The program displays NumberFormatException followed by RuntimeException. C) The program displays NumberFormatException. D) The program displays RuntimeException. 8 34) 34) Analyze the following program. class Test { public static void main(String[] args) { try { String s = ʺ5.6ʺ; Integer.parseInt(s); // Cause a NumberFormatException int i = 0; int y = 2 / i; System.out.println(ʺWelcome to Javaʺ); } catch (Exception ex) { System.out.println(ex); } } } A) An exception is raised due to 2 / i; B) The program compiles and runs without exceptions. C) The program has a compilation error. D) An exception is raised due to Integer.parseInt(s); 9 35) What is displayed on the console when running the following program? class Test { public static void main(String[] args) { try { method(); System.out.println(ʺAfter the method callʺ); } catch (RuntimeException ex) { System.out.println(ʺRuntimeExceptionʺ); } catch (Exception ex) { System.out.println(ʺExceptionʺ); } } static void method() throws Exception { try { String s = ʺ5.6ʺ; Integer.parseInt(s); // Cause a NumberFormatException int i = 0; int y = 2 / i; System.out.println(ʺWelcome to Javaʺ); } catch (RuntimeException ex) { System.out.println(ʺRuntimeExceptionʺ); } catch (Exception ex) { System.out.println(ʺExceptionʺ); } } } A) The program has a compilation error. B) The program displays RuntimeException twice. C) The program displays Exception twice. D) The program displays RuntimeException followed by After the method call. E) The program displays Exception followed by RuntimeException. 10 35) 36) What is displayed on the console when running the following program? 36) class Test { public static void main (String[] args) { try { System.out.println(ʺWelcome to Javaʺ); } finally { System.out.println(ʺThe finally clause is executedʺ); } } } A) Welcome to Java followed by The finally clause is executed in the next line B) Welcome to Java C) The finally clause is executed D) None of the above 37) What is displayed on the console when running the following program? class Test { public static void main(String[] args) { try { System.out.println(ʺWelcome to Javaʺ); int i = 0; int y = 2/i; System.out.println(ʺWelcome to Javaʺ); } catch (RuntimeException ex) { System.out.println(ʺWelcome to Javaʺ); } finally { System.out.println(ʺEnd of the blockʺ); } } } A) The program displays Welcome to Java two times. B) The program displays Welcome to Java two times followed by End of the block. C) The program displays Welcome to Java three times followed by End of the block. D) The program displays Welcome to Java three times. 11 37) 38) What is displayed on the console when running the following program? 38) class Test { public static void main(String[] args) { try { System.out.println(ʺWelcome to Javaʺ); int i = 0; int y = 2/i; System.out.println(ʺWelcome to Javaʺ); } catch (RuntimeException ex) { System.out.println(ʺWelcome to Javaʺ); } finally { System.out.println(ʺEnd of the blockʺ); } System.out.println(ʺEnd of the blockʺ); } } A) The program displays Welcome to Java two times followed by End of the block two times. B) The program displays Welcome to Java three times followed by End of the block. C) You cannot catch RuntimeException errors. D) The program displays Welcome to Java two times followed by End of the block. 39) Analyze the following code: 39) class Test { public static void main(String[] args) { try { int zero = 0; int y = 2/zero; try { String s = ʺ5.6ʺ; Integer.parseInt(s); // Cause a NumberFormatException } catch(Exception e) { } } catch(RuntimeException e) { System.out.println(e); } } } A) The program has a compilation error because Exception appears before RuntimeException. B) A try-catch block cannot be embedded inside another try-catch block. C) A good programming practice is to avoid nesting try-catch blocks, because nesting makes programs difficult to read. You can rewrite the program using only one try -catch block. D) None of the above. 12 40) Which class contains the method for checking whether a file exists? A) File B) PrintWriter C) Scanner D) System 40) 41) Which class do you use to write data into a text file? A) File B) PrintWriter C) Scanner D) System 42) Which class do you use to read data from a text file? A) File B) PrintWriter C) Scanner D) System 41) 42) 43) Which of the following statements are correct? 43) I: File file = new File(ʺinput.txtʺ); try (Scanner input = new Scanner(file)) { String line = input.nextLine(); } II: try (File file = new File(ʺinput.txtʺ); Scanner input = new Scanner(file);) { String line = input.nextLine(); } III: File file; try (file = new File(ʺinput.txtʺ); Scanner input = new Scanner(file);) { String line = input.nextLine(); } IV: File file; Scanner input; try (file = new File(ʺinput.txtʺ); input = new Scanner(file);) { String line = input.nextLine(); } A) I B) II C) III D) IV 44) Suppose you enter 34.3 57.8 789, then press the ENTER key. Analyze the following code. Scanner input = new Scanner(System.in); int v1 = input.nextInt(); int v2 = input.nextInt(); String line = input.nextLine(); A) The program has a runtime error because 34.3 is not an integer. B) After the last statement is executed, v1 is 34. C) After the last statement is executed, line contains characters ʹ7ʹ, ʹ8ʹ, ʹ9ʹ, ʹ\nʹ. D) After the last statement is executed, line contains characters ʹ7ʹ, ʹ8ʹ, ʹ9ʹ. 13 44) 45) A method should not claim Error or RuntimeException in the method declaration, but it may throw exceptions of these types. A) true B) false 45) 46) ________ are unchecked exceptions. A) RuntimeException C) Throwable 46) B) Exception D) IOException 47) If an exception occurs in a try-catch block, the code in the finally clause ________. A) is executed B) may be executed C) is not executed D) is not executed if the exception is caught 47) 48) Analyze the following code: 48) public static boolean isCorrect() { try { // Perform some tasks // ... return true; } finally { return true; } } A) When invoking the isCorrect method, it always returns true. B) When invoking the isCorrect method, it always returns false. C) When invoking the isCorrect method, it returns false if any exceptions occur in the method. D) When invoking the isCorrect method, it returns true if no exceptions occur in the method. 49) Closing a PrintWriter object ensures that the data in the buffer are sent to the file. A) true B) false 49) 50) If no exception occurs in a try-catch block, the code in the finally clause ________. A) is not executed B) may be executed C) is ignored D) is executed 50) 14 Answer Key Testname: EXAM3 1) C 2) C 3) D 4) A 5) B 6) D 7) B 8) A 9) A 10) A 11) A 12) B 13) A 14) B 15) A 16) B 17) D 18) D 19) B 20) D 21) D 22) C 23) B 24) A 25) E 26) A 27) B 28) C 29) D 30) E 31) D 32) B 33) A 34) D 35) D 36) A 37) B 38) A 39) C 40) A 41) B 42) C 43) A 44) A 45) A 46) A 47) A 48) B 49) A 50) D 15