You are on page 1of 3

CSE215 Quiz-4 Revision

Exception Handling

1) In a try-block, no further line will be executed, if an exception is generated.


try {
S1;
S2; // exception generated
S3; // will not be executed
S4; // will not be executed
}
2) Exception is thrown by try-block and received by the catch-block.
3) Every try-block must have at least one catch-block;
4) Finally-block can be used to omit catch-block [NO multiple finally-block]. It is always
executed.
5) The fileNotFound exception originally generates from the File (String file name) {..}
constructor.
6) System Errors are thrown by JVM.
7) Runtime Exception, Error and their subclasses are unchecked exception.
8) Unchecked exceptions are checked if they are handled during runtime.
9) Java does not mandate you to write code to catch unchecked exceptions.
10) Exception is declared using throws keyword and thrown using throw keyword.
11) Exception declaration is mandatory for checked exception.
12) A common use of the finally clause is in I/O programming. To ensure that a file is closed
under all circumstances, you may place a file closing statement in the finally-block.
13) If exception is partially handled in a catch-block, and is thrown again using throw keyword,
we call this action – rethrow.
14) Rethrown exception must be handled before returning to normal execution.
15) Exception handling usually requires more time and resources because it requires instantiating
a new exception object, rolling back the call stack, and propagating the errors to the calling
methods.
16) Exception handling is generally used for unexpected error conditions.
17) During rethrow, once exception is thrown by the catch-block, no other catch-block of the
same try-block will handle this exception. Exception will be sent to the caller.
18) The order in which exceptions are specified in catch blocks is important. A compile error
will result if a catch block for a superclass type appears before a catch block for a subclass type.
19) Java generates no exception for overflow and underflow errors.
20) https://liveexample-
ppe.pearsoncmg.com/selftest/selftest11e?chapter=12&username=liang11e
Abstract Class and Interfaces
❖ Abstract Class

1) Abstract Class helps to reduce redundancy produced by polymorphic method invocation


(excess use of instanceOf operator).
2) Abstract methods are written with the following prototype-
modifier(public/protected) abstract return type(void/double) method-Name (parameter list);
Example – public abstract double getArea();
3) If there is at least one abstract method in a class, then the class must be declared abstract.
4) An abstract class does not necessarily contain an abstract method.
5) You cannot create an instance from an abstract class using the new operator, but an abstract
class can be used as a data type.
GeoObject g = new GeoObject(); // NOT ALLOWED
GeoObject g; // Allowed-creates a reference variable.
GeoObject[ ] =new GeoObject[5]; // ALLOWED. -creates an array of reference variable.
GeoObject g =new Circle(); // ALLOWED. -creates an object of circle class.
6) A constructor of abstract class must be written so that constructor chaining can occur in the
inheritance chain.
7) Usually abstract class constructors are made protected. Default modifier is NOT used because
if subclass and superclass is on different package, then access will be restricted.
8) In the inheritance chain if the abstract superclass contains an abstract method, then at least one
subclass should implement that method. Any subclass that doesn’t implement that method must
be made abstract.
9) If there is more than one method in abstract superclass, and subclass implements one method,
but not all, then it must also be made abstract.
10) In UML diagram abstract is written in Italics and protected modifier is represented using #.
11) A subclass can be abstract even if its superclass is concrete.
12) A superclass nonabstract method can be overridden to make abstract by a subclass. In that
case, the subclass must be made abstract and the superclass method implementation becomes
invalid.
❖ Interface
1) All data fields are public final static and all methods are public abstract in an interface.
2) Interface behaves as a regular class, and a bytecode is created. Like abstract class, we cannot
instantiate interface objects with new operator.
3) Interface generally indicates property using the “has-a” relationship.
4) Chicken Implements Edible.
Chicken a = new Chicken ();
(a instanceOf Edible) is TRUE; // every instance of subclass is an instance of superclass.
5) In java, the Comparable Interface contains method which accept parameterized type
argument.
public interface Comparable<E>{ // E – Integer,String,etc.
public int compareTo(E o);
}
6) a1.CompareTo(a2) → a1- this object. a2- specified object. This returns-
>a negative integer if this object is less than o.
>zero if this object is equal to o.
>a positive integer if this object is greater than o.
7) If any class implements the comparable interface, then an array of that object can use the
array sort method. This is an example of Generic Programming.
8)

9) An interface can inherit other interfaces using the extends keyword. Such an interface is
called a subinterface.
10) All classes share a single root, the Object class, but there is no single root for interfaces.
11) Conflicting interfaces results in compiler error.
12) Cloneable is a Marker (Empty) Interface.
13) The built-in clone method copies using shallow copy.
14) https://liveexample-
ppe.pearsoncmg.com/selftest/selftest11e?chapter=13&username=liang11e

You might also like