Java 2 final Polymorphism and Exceptions
1) Consider a reference declared in the following manner. Animal a; This reference may only point to an object that created by instantiating the Animal class.
Answer: False
10) In practice, it is important to catch all exceptions that might be thrown by a program.
Answer: False
2) Let Animal be an interface. Then it is possible to create an object by instantiating the Animal interface.
Answer: False
2) Unchecked exceptions must be caught or propogated, or a program will not compile.
Answer: False
3) A finally clause is always required in a try-catch block.
Answer: False
3) The compareTo method of the Comparable interface returns a boolean value.
Answer: False
4) Attempting to divide by zero will result in an Error being thrown, not an Exception.
Answer: False
5) Every line in a catch block is guaranteed to be executed in all situations.
Answer: False
8) A reference variable can refer to an object of a child class, but not any further down the inheritance hierarchy.
Answer: False
8) The getMessage method of the Exception class prints out the stack trace, which helps the user to track down the source of the exception.
Answer: False
9) Polymorphism via inheritance requires that all classes in the inheritance hierarchy are concrete.
Answer: False
1) Files that are open for output from a program must be explicitly closed in the program
Answer: True
10) An interface name may be used as a reference type.
Answer: True
4) Compile-time binding is more efficient than dynamic binding
Answer: True
5) A parameter to a method can be polymorphic.
Answer: True
6) An exception will be propagated until it is caught and handled or until it is passed out of the main method.
Answer: True
6) An interface cannot declare any instance variables.
Answer: True
7) A throw statement is used to begin exception propagation.
Answer: True
7) Establishing the relationship between a listener and the component it listens to is accomplished using polymorphism.
Answer: True
9) When accessing an element of an array, if the index is outside of the range of the indexes of the array, an exception is thrown.
Answer: True
14) Suppose Animal is an interface that specifies a single method - speak. Now suppose the Dog class implements the Animal interface. In addition to the speak method, the Dog class also has a method called wagTail. Now consider the following code. Animal a = new Dog(); a.wagTail(); Which of the following is true about this code? a) It will result in a compile-time error. b) It will result in a run-time error. c) It will call the speak method defined in the Animal interface. d) It will call the wagTail method defined in the Dog class. e) none of the above are true.
a) It will result in a compile-time error
14) Which of the following exceptions are unchecked? a) RuntimeException b) IllegalAccessException c) NoSuchMethodException d) ClassNotFoundException e) none of the above
a) RuntimeException
11) Which of the following represents the standard input stream? a) System.in b) System.out c) System.err d) System.instream e) System.outstream
a) System.in
8) The Exception class and the Error class are subclasses of the ___________________ class. a) Throwable b) Catchable c) RuntimeProblem d) CompilerProblem e) none of the above
a) Throwable
15) Which GUI concepts use polymorphism to establish their relationship? a) a listener and its associated component b) a radio button and its default selection c) a button and its label d) a slider and its tick marks e) none of the above
a) a listener and its associated component
3) In Java, polymorphic method binding occurs ____________________ . a) at run time b) at compile time c) never d) when a programmer writes the code e) during the testing phase of software development
a) at run time
2) A(n) _____________________ is an object that defines an erroneous situation from which the program usually cannot recover. a) error b) exception c) interface d) try block e) catch block
a) error
7) In Java, polymorphic references can be created through the use of __________________ and ________________. a) inheritance, interfaces b) inheritance, abstract classes c) interfaces, abstract classes d) interfaces, iterators e) none of the above
a) inheritance, interfaces
12) Which of the following file streams should be explicitly closed to ensure that written data is properly retained? a) output b) input c) error d) writable e) readable
a) output
5) Suppose that Horse is a subclass of Animal, and neither class is abstract. Which of the following is an invalid declaration and initialization? a) Horse h = new Horse(); b) Horse h = new Animal(); c) Animal a = new Animal(); d) Animal a = new Horse(); e) all of the above are valid
b) Horse h = new Animal();
2) The commitment to execute certain code to carry out a method invocation is referred to as _________________. a) execution b) binding c) polymorphism d) inheritance e) none of the above
b) binding
3) A(n) ____________________ can be used to find the exact line where an exception was thrown during program execution. a) interface b) call-stack trace c) try block d) catch block e) none of the above
b) call-stack trace
1) A(n) _____________________ is an object that defines an unusual or erroneous situation that is typically recoverable. a) error b) exception c) interface d) try block e) catch block
b) exception
10) Let Object a be larger than Object b. What will the following method call return? a.compareTo(b) a) it will return 0 b) it will return a number greater than 0 c) it will return a number less than 0 d) it will return true e) it will return false
b) it will return a number greater than 0
4) Late binding is _______________ than _______________ . a) more efficient, compile-time binding b) less efficient, compile-time binding c) more efficient, run-time binding d) less efficient, run-time binding
b) less efficient, compile-time binding
7) If an exception is not caught, a program will __________________________ . a) not compile b) terminate abnormally c) print a message and continue executing d) all of the above e) neither a, b nor c
b) terminate abnormally
9) ____________ is the process of catching an exception in the chain of method calls from the method where the exception occurred up to the main method. a) Error handling b) Exception handling c) Exception propagation d) Catch block nesting e) Finally block nesting
c) Exception propagation
13) Consider the following line of code. Comparable s = new String(); Which of the following statements is true about this line? a) It will result in a compile-time error. b) It will result in a run-time error. c) It will create a String object pointed to by a Comparable reference. d) Although it is perfectly valid Java, it should be avoided due to confusion. e) none of the above are true
c) It will create a String object pointed to by a Comparable reference.
12) You need to create a reference variable that can refer to objects from many different classes. You do not know the inheritance hierarchies of the classes. The safest class to use to declare the reference variable is a) Animal b) String c) Object d) Scanner e) File
c) Object
5) A(n) ____________________ is used to specify how certain exceptions should be handled. a) finally block b) try block c) catch block d) error e) none of the above
c) catch block
13) Which of the following exception types must always be caught unless they are contained in methods that throw them in the method header? a) file stream b) IO c) checked d) unchecked e) none of the above
c) checked
1) A polymorphic reference is one that can refer to _______________ type(s) of object(s). a) exactly one b) zero c) multiple d) abstract e) static
c) multiple
15) Which of the following methods are part of the Exception class and can be used to give information about a thrown exception? a) getInfo b) printInfo c) printStackTrace d) getStackTrace e) none of the above
c) printStackTrace
8) Let Dog be a subclass of Animal, and suppose Animal has a method called speak() that is overridden in the Dog class. Consider the following code. Animal spot = new Dog(); spot.speak(); Which of the following is true? a) This code will result in a compile-time error. b) This code will result in a run-time error. c) The speak method defined in the Animal class will be called. d) The speak method defined in the Dog class will be called. e) The speak method will not be called at all.
d) The speak method defined in the Dog class will be called.
11) Which of the following methods are included with any object that implements the Iterator interface? a) next b) hasNext c) toString d) all of the above e) a and b
d) all of the above
9) The Comparable interface contains which of the following methods? a) isGreaterThan b) isLessThan c) equals d) compareTo e) all of the above
d) compareTo
6) In Java, a(n) ___________________ is a collection of constants and abstract methods. a) polymorphic reference b) abstract class c) implementation d) interface e) iterator
d) interface
10) A(n) ________________ is an ordered sequence of bytes. a) exception b) error c) input-output flow d) stream e) none of the above
d) stream
4) A(n) ____________________ is used to identify a block of statements that may cause an exception. a) call-stack trace b) error c) catch block d) try block e) none of the above
d) try block
6) Every line of a(n) __________________ is executed no matter what exceptions are thrown. a) try block b) call stack trace c) catch block d) interface e) finally block
e) finally block
