Translate

Monday, January 7, 2013

Exception Handling Interview Questions


Q) Diff Exception & Error
 Exception and Error both are subclasses of the Throwable class.

ExceptionàException is generated by java runtime system (or) by manually. An exception is a abnormal condition that transfer program execution from a thrower to catcher.
Error        àWill stop the program execution, Error is a abnormal system condition we cannot handle these.

Q) Can an exception be rethrown?
A) Yes, an exception can be rethrown.

Q) try, catch, throw, throws
try         à This is used to fix up the error, to prevent the program from automatically terminating, try-catch is used to catching an exception that are thrown by the java runtime system.
Throw   à is used to throw an exception explicitly.
Throws à A Throws clause list the type of exceptions that a methods might through.

Q) What happens if an exception is not caught?
A) An uncaught exception results in the uncaughtException() method of the thread's ThreadGroup being invoked, which eventually results in the termination of the program in which it is thrown.

Q) What happens if a try-catch-finally statement does not have a catch clause to handle an exception that is thrown within the body of the try statement?
The exception propagates up to the next higher level try-catch statement (if any) or results in the program's termination.

Q) Checked & UnChecked Exception :-
Checked exception is some subclass of Exception. Making an exception checked forces client programmers to deal with the possibility that the exception will be thrown. eg, IOException thrown by java.io.FileInputStream's read() method·
            
Unchecked exceptions are RuntimeException and any of its subclasses. Class Error and its subclasses also are unchecked. With an unchecked exception, however, the compiler doesn't force client programmers either to catch the exception or declare it in a throws clause. In fact, client programmers may not even know that the exception could be thrown. eg, StringIndexOutOfBoundsException thrown by String's charAt() method· Checked exceptions must be caught at compile time. Runtime exceptions do not need to be. Errors often cannot be.

Checked Exceptions
Un checked exception
ClassNotFoundException
ArithmeticException
NoSuchMethodException
ArrayIndexOutOfBoundException
NoSuchFieldException
ClasscastException
InterruptedException
IllegalArgumentException
IllegalAccessException
IllegalMonitorSateException
CloneNotSupportedException
IllegalThreadStateException

IndexOutOfBoundException

NullPointerException

NumberFormatException

StringIndexOutOfBounds

OutOfMemoryError --> Signals that JVM has run out of memory and that the garbage collector is unable to claim any more free memory.
StackOverFlow --> Signals that a stack O.F in the interpreter.
ArrayIndexOutOfbound --> For accessing an array element by providing an index values <0 or="or"> or equal to the array size.
StringIndexOutOfbound --> For accessing character of a string or string buffer with index values <0 or="or"> or equal to the array size.
Arithmetic Exception --> such as divide by zero.
ArrayStore Exception --> Assignment to an array element of an incompatible types.
ClasscastException --> Invalid casting.
IllegalArgument Exception --> Illegal argument is used to invoke a method.
Nullpointer Exception --> If attempt to made to use a null object.
NumberFormat Exception --> Invalid conversition of string to numeric format.
ClassNotfound Exception --> class not found.
Instantion Exception --> Attempt to create an object of an Abstract class or Interface.
NosuchField Exception --> A request field does not exist.
NosuchMethod Exception --> A request method does not exist.

Q) Methods in Exceptions?
A) getMessage(), toString(), printStackTrace(), getLocalizedMessage(),

Q) What is exception chaining?
A) An exception chain is a list of all the exceptions generated in response to a single root exception. As
each exception is caught and converted to a higher-level exception for rethrowing, it's added to the chain.
This provides a complete record of how an exception is handled The chained exception API was introduced in 1.4. Two methods and two constructors were added to Throwable.
Throwable getCause()
Throwable initCause(Throwable)
Throwable(String, Throwable)
Throwable(Throwable)

The Throwable argument to initCause and the Throwable constructors is the exception that caused the current exception. getCause returns the exception that caused the current exception, and initCause returns the current exception.

Q) Primitive multi tasking
            If the threads of different priorities shifting the control depend on the priority i.e.; a thread with higher priority is executed first than the thread with lower priority. This process of shifting control is known as primitive multi tasking.

Q) Http Status Codes
            To inform the client of a problem at the server end, you call the “sendError” method. This causes the server to respond with status line, with protocol version and a success or error code.
The first digit of the status code defines the class of response, while the last two digits do not have categories
Number
Type
Description
1xx
Informational
Requested received, continuing to process
2xx
Success
The action was successfully received, understood, and accepted
3xx
Redirection
Further action must be taken in order to complete the request
4xx
Client Error
The request contains bad syntax or cannot be fulfilled
5xx
Server Error
The server failed to fulfil valid request

Wednesday, January 2, 2013

Core Java Interview Question Part-8


Q) Class

            Class encapsulate the run-time state of an object or interface. Methods in this class are

static Class forName(String name) throws ClassNotFoundException
getClass()
getClassLoader()
getConstructor()
getField()
getDeclaredFields()
getMethods()
getDeclearedMethods()
getInterface()
getSuperClass()

Q) java.jlang.Reflect (package)

            Reflection is the ability of software to analyse it self, to obtain information about the field, constructor, methods & modifier of class. You need this information to build software tools that enables you to work with java beans components.

Q) InstanceOf

            Instanceof means by which your program can obtain run time type information about an object.
Ex:- A a = new A();
       a.instanceOf A;
 
Q) Java pass arguments by value are by reference?
A) By value
 
Q) Java lack pointers how do I implements classic pointer structures like linked list?
A) Using object reference.
 
Q) java. Exe

Micro soft provided sdk for java, which includes “jexegentool”. This converts class file into a “.Exec”  form. Only disadvantage is user needs a M.S java V.M installed.

Q) Bin & Lib in jdk?

Bin contains all tools such as javac, appletviewer and awt tool.
Lib contains API and all packages.


  
Collections Frame Work


Q)        
Collection classes
Collection Interfaces
Legacy classes
Legacy interface
Abstract collection
Collection
Dictionary
Enumerator
Abstract List
List
Hash Table

Abstract Set
Set
Stack

Array List
Sorted Set
Vector

Linked List
Map
Properties

Hash set
Iterator


Tree Set



Hash Map



Tree Map



Abstract Sequential List



           
Collection Classes

      Abstract collection à Implements most of the collection interfaces.
     
      Abstract List à Extends Abstract collection & Implements List Interface. A.L allow “random access”.

Methods>> void add (int index, Object element), boolean add(Object o), boolean addAll(Collection c), boolean addAll(int index, Collection c), Object remove(int index), void clear(), Iterator iterator().

      Abstract Set à Extends Abstract collection & Implements Set interface.

Array List à Array List extends AbstractList and implements the List interface. ArrayList is a variable length    of array of object references, ArrayList support dynamic array that grow as needed. A.L allow rapid random access to element but slow for insertion and deletion from the middle of the list. It will allow duplicate elements. Searching is very faster.
A.L internal node traversal from the start to the end of the collection is significantly faster than Linked List traversal.
      à A.L is a replacement for Vector.
     
Methods>>void add (int index, Object element), boolean add(Object o), boolean addAll(Collection c), boolean addAll(int index, Collection c), Object remove(int index), void clear(), object get(int index), int indexOf(Object element),  int latIndexOf(Object element), int size(), Object [] toArray(). 

Linked List à Extends AbstactSequentialList and implements List interface. L.L provide optimal sequence access, in expensive insertion and deletion from the middle of the list, relatively slow for random access. When ever there is a lot of insertion & deletion we have to go for L.L. L.L is accessed via a reference to the first node of the list. Each subsequent node is accessed via a reference to the first node of the list. Each subsequent node is accessed via the link-reference number stored in the previous node.
     
Methods>> void addFirst(Object obj), addLast(Object obj), Object getFirst(), Object getLast(),void add (int index, Object element), boolean add(Object o), boolean addAll(Collection c), boolean addAll(int index, Collection c), Object remove(int index), Object remove(Object o), void clear(), object get(int index), int indexOf(Object element), int latIndexOf(Object element), int size(), Object [] toArray(). 

Hash Set à Extends AbstractSet & Implements Set interface, it creates a collection that uses HashTable for storage, H.S does not guarantee the order of its elements, if u need storage go for TreeSet. It will not allow duplicate elements
     
      Methods>>boolean add(Object o), Iterator iterator(), boolean remove(Object o), int size().

Tree Set à Extends Abstract Set & Implements Set interface. Objects are stored in sorted, ascending order. Access and retrial times are quite fast. It will not allow duplicate elements
     
Methods>> boolean add(Object o), boolean addAll(Collection c), Object first(), Object last(), Iterator iterator(), boolean remove(Object o).

Hash Map à Extends Abstract Map and implements Map interface. H.M does not guarantee the order of elements, so the order in which the elements are added to a H.M is not necessary the order in which they are ready by the iterate. H.M permits only one null values in it while H.T does not
     
      à HashMap is similar to Hashtable.
     
Tree Map à implements Map interface, a TreeMap provides an efficient means of storing key/value pairs in sorted order and allow rapid retrieval.
    
      Abstract Sequential List à Extends Abstract collection; use sequential access of its elements.

Core Java Interview Question Part-8


Q) Class

            Class encapsulate the run-time state of an object or interface. Methods in this class are

static Class forName(String name) throws ClassNotFoundException
getClass()
getClassLoader()
getConstructor()
getField()
getDeclaredFields()
getMethods()
getDeclearedMethods()
getInterface()
getSuperClass()

Q) java.jlang.Reflect (package)

            Reflection is the ability of software to analyse it self, to obtain information about the field, constructor, methods & modifier of class. You need this information to build software tools that enables you to work with java beans components.

Q) InstanceOf

            Instanceof means by which your program can obtain run time type information about an object.
Ex:- A a = new A();
       a.instanceOf A;
 
Q) Java pass arguments by value are by reference?
A) By value
 
Q) Java lack pointers how do I implements classic pointer structures like linked list?
A) Using object reference.
 
Q) java. Exe

Micro soft provided sdk for java, which includes “jexegentool”. This converts class file into a “.Exec”  form. Only disadvantage is user needs a M.S java V.M installed.

Q) Bin & Lib in jdk?

Bin contains all tools such as javac, appletviewer and awt tool.
Lib contains API and all packages.


  
Collections Frame Work


Q)        
Collection classes
Collection Interfaces
Legacy classes
Legacy interface
Abstract collection
Collection
Dictionary
Enumerator
Abstract List
List
Hash Table

Abstract Set
Set
Stack

Array List
Sorted Set
Vector

Linked List
Map
Properties

Hash set
Iterator


Tree Set



Hash Map



Tree Map



Abstract Sequential List



           
Collection Classes

      Abstract collection à Implements most of the collection interfaces.
     
      Abstract List à Extends Abstract collection & Implements List Interface. A.L allow “random access”.

Methods>> void add (int index, Object element), boolean add(Object o), boolean addAll(Collection c), boolean addAll(int index, Collection c), Object remove(int index), void clear(), Iterator iterator().

      Abstract Set à Extends Abstract collection & Implements Set interface.

Array List à Array List extends AbstractList and implements the List interface. ArrayList is a variable length    of array of object references, ArrayList support dynamic array that grow as needed. A.L allow rapid random access to element but slow for insertion and deletion from the middle of the list. It will allow duplicate elements. Searching is very faster.
A.L internal node traversal from the start to the end of the collection is significantly faster than Linked List traversal.
      à A.L is a replacement for Vector.
     
Methods>>void add (int index, Object element), boolean add(Object o), boolean addAll(Collection c), boolean addAll(int index, Collection c), Object remove(int index), void clear(), object get(int index), int indexOf(Object element),  int latIndexOf(Object element), int size(), Object [] toArray(). 

Linked List à Extends AbstactSequentialList and implements List interface. L.L provide optimal sequence access, in expensive insertion and deletion from the middle of the list, relatively slow for random access. When ever there is a lot of insertion & deletion we have to go for L.L. L.L is accessed via a reference to the first node of the list. Each subsequent node is accessed via a reference to the first node of the list. Each subsequent node is accessed via the link-reference number stored in the previous node.
     
Methods>> void addFirst(Object obj), addLast(Object obj), Object getFirst(), Object getLast(),void add (int index, Object element), boolean add(Object o), boolean addAll(Collection c), boolean addAll(int index, Collection c), Object remove(int index), Object remove(Object o), void clear(), object get(int index), int indexOf(Object element), int latIndexOf(Object element), int size(), Object [] toArray(). 

Hash Set à Extends AbstractSet & Implements Set interface, it creates a collection that uses HashTable for storage, H.S does not guarantee the order of its elements, if u need storage go for TreeSet. It will not allow duplicate elements
     
      Methods>>boolean add(Object o), Iterator iterator(), boolean remove(Object o), int size().

Tree Set à Extends Abstract Set & Implements Set interface. Objects are stored in sorted, ascending order. Access and retrial times are quite fast. It will not allow duplicate elements
     
Methods>> boolean add(Object o), boolean addAll(Collection c), Object first(), Object last(), Iterator iterator(), boolean remove(Object o).

Hash Map à Extends Abstract Map and implements Map interface. H.M does not guarantee the order of elements, so the order in which the elements are added to a H.M is not necessary the order in which they are ready by the iterate. H.M permits only one null values in it while H.T does not
     
      à HashMap is similar to Hashtable.
     
Tree Map à implements Map interface, a TreeMap provides an efficient means of storing key/value pairs in sorted order and allow rapid retrieval.
    
      Abstract Sequential List à Extends Abstract collection; use sequential access of its elements.