happy diwali
Happy Diwali
happy diwali

Tuesday, 16 June 2015

exceptions


Q1.What is an Exception?
 An unwanted, unexpected event that disturbs normal flow of the program is called Exception.Example: FileNotFondException.

Q2.What is the purpose of Exception Handling?
The main purpose of Exception Handling is for graceful termination of the program.

Q3.What is the meaning of Exception Handling?
Exception Handling doesn’t mean repairing an Exception, we have to define alternative way to continue rest of the code normally.
Example: If our programming requirement is to read the data from the file locating at London but at Runtime if London file is not available then we have to use local file alternatively to continue rest of program normally. This is nothing but Exception Handling.

Q4.Explain Default Exception Handling Mechanism in java?
 If an exception raised, the method in which it’s raised is responsible for the creation of  Exceptions object by including the following information:
·          Name of the Exception
·          Description of the Exception
·          Stack Trace
·          After creating Exception object the method handover it to the JVM.
·          JVM checks for Exception Handling code in that method.
·          If the method doesn’t contain any Exception handling code then JVM terminates the method abnormally and removes the corresponding entry from the stack.
·          JVM identify the caller method and checks for Exception Handling code in that method. If the caller doesn’t contain any exception handling code then JVM terminates that method abnormally and removes the corresponding entry from the stack.
·          This process will be continue until main() method.
·          If the main() method also doesn’t contain exception handling code the JVM terminates that main() method and removes the corresponding entry from the stack.
·          Just before terminating the program abnormally JVM handovers the responsibility of exception handling to the Default Exception Handler which is the component of JVM.
·          Default Exception Handler just print exception information to the consol in the following format
·          Name of Exception: Description
Stack Trace (Location of the Exception)

Q5.What is the purpose of try?
  We should maintain all risky code inside the try block.

Q6. What is the purpose of catch block?
   We have to maintain all Exception Handling code inside the catch block.

Q7.  Is try with multiple catch block is possible?
 The way of handling an exception is varied from exception to exception compulsory we have to write a separate catch block for every exception. Hence try will multiple catch block is possible and it is recommended to use.
               
 Example:
                try{
                                //Risky code
                }
                catch(IOException e)
                {
                                //Hndling code for IOException
                }
                catch(ArithmeticException e)
                {
                                //handling code for AE
                }
                catch(NullPointerExcetpion e)
                {
                                // handling code for NPE
                }
                catch(Exception e)
                {
                                //default exception handling code
                }

Q8. If try with multiple catch block present is order of catch blocks important in which order we have to take?
Ans. If try with multiple catch block present then the order of catch block is very important it should be from child to parent but not from parent to child.

Q9. What are various methods to print Exception information? and differentiate them.
  Throwable class defines the following method to print exception or error information .
1. printStackTrace() :- This method print exception information in the following format.
                                    
                              Name of the Exception: Description
                                StackTrace

2.toString():-  This method print exception information in the following format.

                          Name of the Exception: Description               

3.getMessage():- This method prints only description of the exception.
                     
                            Description

Q10.If an exception rised inside catch block then what will happen?
   If an exception raised inside catch block and it is not part of any try block then it is always abnormal termination.

Q11. Is it possible to take try, catch inside try block?
   Yes, It is possible to take try, catch inside try block. That is nesting of try catch is possible.

Q12.Is it possible to take try, catch inside catch block?
  Yes, It is possible to take try, catch inside catch block.

Q13. Is it possible to take try without catch?
   Yes, it is possible to take try without catch but compulsory finally block should be available.

Q14. What is the purpose of finally block?
   The main purpose of finally block is, to maintain the cleanup code. This block will execute always.

Q15.  Is finally block will be execute always?
Ans.  Yes finally block will be executed always irrespective of whether exception raised or not raised whether exceptions are handled or not handle. There is one situation where the finally block won’t be executed if the JVM is going to be shutdown.

Q16. In which situation finally block will not executed?
Ans. There is one situation where the finally block won’t be executed if we are using system.exit(0) explicitly then JVM itself will be shutdown and there is no chance of executing finally block.

Q17. If return statement present inside try is finally block will be executed?
Ans. Yes, if return statement present inside try, then also finally block will be executed. finally block will dominate return statement also.

Q18. What is the difference between final, finally and finalize()?
Ansfinal:-
         final is a modifier applicable for variables, methods and classes. final variable means constant  
            and reassignment is not possible.
        final method means implementation is final in the child classes we can’t override.
    final class means it  won’t participate in inheritance and child class creation is not possible.

finally:- It is a block associated with try catch to maintain cleanup code. Finally block will be executed  
    always irrespective of whether exception is raised or not raised or whether the exception is handle or not  
    handle.
finalize():- It is a method, Garbage collector always calls this method just before destroying any object to
    perform cleanup activities.

Q19. Is it possible to write any statement between try-catch and finally?
Ans. No, it is not possible to write any statement between try catch and finally. If we will try to write any statement between them then we will get compile time error.

Q20. Is it possible to take two finally blocks for the same try?
Ans. No, it is not possible to take two finally blocks for the same try. If we try to take then we will get compile time error.

Q21.  Is syntax try-finally-catch is valid ?
Ans.   No, this syntax is not valid. It should be like try-catch-finally then only code will compile.

Q22. What is the purpose of throw?
Ans.   Sometimes we can create Exception object explicitly and we can handover that exception object to the JVM explicitly by throw keyword.
          The purpose of throw keyword is to handover our created exception object explicitly to the JVM.
                Example1:
class Test{
                public static void main(String[] args){
                                System.out.println(10/0);
               }
                }
                In this case ArithmeticException object created implicitly and handover to the JVM automatically by the main method.
                Example2:
Class Test{
                Public static void main(String[] args){
                                Throw new ArithmeticException(“/by Zero”);
                                }
                }
                In this case creation of an exception object and handover to the JVM explicitly by the programmer.

Q23.  Is it possible to throw an Error?
Ans.   Yes, It is possible to throw any Throwable type including Error.

Q24.  Is it possible to throw any java object?
Ans.   No, we can use throw keyword only for throwable objects otherwise we will get compile time error saying incompatible type.

Q25. After throw is it allow to take any statement directly?
Ans.  After throw statement we are not allow to place any statement directly violation leads to compile time error saying Unreachable Statement.

Q26. What is the purpose of throws?
Ans.  The main purpose of throws keyword is to delegate the responsibilities of exception handling to the caller. It requires in the case of checked exception.

Q27. What is the difference between throw and throws?
Ans.   Sometimes we can create Exception object explicitly and we can handover that exception object to the JVM explicitly by throw keyword.The main purpose of throw keyword is to handover our created exception object explicitly to the JVM. The main purpose of throws keyword is to delegate the responsibilities of exception handling to the caller. It requires in the case of checked exception.

Q28.  What is the difference between throw and thrown?
Ans.   There is no terminology of thrown in  java.

Q29. Is it possible to use throws keyword for any java class?
Ans.  No, we can use throws keyword only for Throwable classes. Otherwise we will get compile time error saying Incompatible types.

Q30. If we are taking catch block for an exception but there is no chance of rising that exception
        in try then what will happen?
Ans.  If there is no chance of raising an exception in try then we are not allow to write catch block for that exception violation leads to compile time error. But this rule is applicable only for fully checked exception.

Q31.  Explain Exception Handling keyword?
Ans.  Exception Handling keyword:
                Try :- To maintain Risky code.
                Catch:- To maintain Exception Handling code.
                Finally:- To maintain the clean up code.
                Throw:- To handover our created exception object to the JVM explicitly.
                Throws:- To delegate the responsibilities of Exception Handling to the caller.

Q32. Which class act as root for entire java Exception hierarchy?
Ans.   Throwable class act as root for entire java Exception hierarchy.

Q33. What is the difference between Error and Exception?
Ans.  Throwable class contain two child classes.
          Exception:- These are mostly caused by our program and are recoverable.
          Error:- These are not caused by our program, mostly caused by lake of system resources. These are non recoverable.

Q34.  What is difference between checked exception and unchecked exception?
Ans.  The exceptions which are checked by the compiler for smooth execution of the program at Runtime is called checked exception. Example: IOException, InterruptedException.The exceptions which are not checked by the compiler are called unchecked exception. Example: ArithmeticException,RuntimeException.

Q35.What is difference between partially checked and fully checked Exception?
Ans.  A checked exception is said to be fully checked if and only if all the child classes also checked otherwise it is called partially checked exception.
                Example:
 IOException:- fully checked exception
                Exception:- partially checked exception
                Throwable:- partially checked exception
                RuntimeException:- unchecked exception

Q36. What is a customized Exception?
Ans.   Sometimes based on our programming requirement we have to create our own exception such type of exception are called customize Exception.
                Example:
                                TooYoungException
                                TooOldException
                                InsufficientFundException

Q37.  Explain the process of creating the customized Exception.
Ans.   Creating customized Exception:
Class TooYoungException extends RuntimeException{
  TooYoungExcetpion(String desc){
                Super(desc);
                }
}
Class TooOldException extends RuntimeException
{
                TooOldException(String desc){
super(desc);
                }
}
Class custExcepiton{
                Public static void main(String[] args){
Int age=Integer.parseInt(args[0]);
If(age>60)
{
Throw new TooYoungException(“Your age is already crossed of marriage, no chance to getting marriage”);
                }
                Else if(age<18)
                {
Throw new TooOldException(“Please wait some more time, definitely you will get best match”);
                }
                Else
                {
                                System.out.println(“Congratulation! You will get match details soon by your email”);
                }
}
               

Q38. Explain control flow in try, catch, finally.
Ans.       Try{
                Statement1;
                Statement2;
Statement3;
}
Catch(X e){
Statement4;
}
Finally{
Statement5;
}
Statement6;
Case1:
If there is no Exception then output is
Statement1
Statement2
Statement3
Statement5
Statement6
Normal termination
Case2:
If an exception raised at statement2 and corresponding catch block has matched then output is
Statement1
Statement4
Statement5
Statement5
Normal termination
Case3:
An exception raised at statement2 and corresponding catch has not matched then output is
Statement1
Statement5
Abnormal termination
Case4:
An exception occurs at statement4 it always Abnormal termination but before that finally block will be executed and output is
Statement1
Statement2
Statement5
Abnormal termination
Case5:
If an exception raised at statement5 or statement6, it is always abnormal termination.
Q39. Can you give the most common occurred exception in your previous project.
Ans. NullPointerException, ArrayIndexOutofBoundException, StackOverFlowError, ClassCastException, NoClassDefFoundError, ExceptionInitilizerError, IllegalArgumentException, NumberFormatException, IllegalStateException, AssertionError.



happy diwali

Collections Brief

Collection framework
Q1.  What limitations are of object Arrays?
The main limitations of Object arrays are
  • These are fixed in size i.e. once we created an array object there is no chance of increasing or decreasing size based on our requirement. Hence  If we don’t know size in advance , arrays are not recommended to use
  • Arrays can hold only homogeneous elements. 
  • There is no underlying data structure for arrays and hence no readymade method support for arrays. Hence for every requirement programmer has to code explicitly
       To over come  these problems collections are recommended to use
Q2. What are differences between arrays and collections?

Arrays
Collections
1.  Arrays r fixed in size and hence once we created an array we are not allowed to increase or decrease the size based on our requirement.
1. Collections are growable in nature and hence based on our requirement we can increase or decrease the size.
2.  Memory point of view arrays are not recommended to use
2. Memory point of view collections are recommended to use.
3. Performance point of view arrays are recommended to use
3. Performance point of view collections are not recommended to use.
4.  Arrays can hold only homogeneous elements
4. Collections can hold both homogeneous and heterogeneous elements.
5. Arrays can hold both primitives as well as objects
5. Collections can hold only objects.
6. For any requirement, there is no ready method support compulsory programmer has to code explicitly.
6. For every requirement ready made method support is available. Being a programmer we have to know how to use those methods and we are not responsible to implement those.

Q3. What is Collection API? Or what is Collection framework?
 It defines set of classes and interfaces which can be used for representing a group of objects as single entity
 Q4.  What  is difference between Collections and Collection?
Collection is an interface which can be used for representing a group of individual objects as single entity  and it acts as root interface of collection frame  work.
Collections is an utility class to define several utility methods for Collection implemented class objects.
Q5.  Explain about Collection interface?
  • This interface can be used to represent a group of objects as a single entity.
  • It acts as root interface for entire collection framework.
  • it defines the most commonly used methods which can be applicable for any collection implemented class object
Q6. Explain about List interface?
List interface is a child interface of Collection interface. This can be used to represent group of individual objects in as a single entity where
  • Duplicates are allowed
  • Insertion order is preserved
Q7.  Explain about Set interface?
Set is a child interface of Collection interface. it can be used to represent a group of individual objects as a single entity where
  • Duplicate objects are not allowed.
  • Insertion order is not preserved
           
Q8.  Explain about SortedSet interface?
it is child interface of Set interface. it can be used to represent a group of individual objects in to a single entity where
  • All the objects are arranged in some sorting order (Can be natural sorting order or customized).
  • Duplicates are not allowed.
Q9.  Explain about NavigableSet ?
It is child interface of SortedSet and provides several utility methods for navigation purposes
  • It doesn’t allows duplicates                                
  • Insertion order is preserved
  • It is introduced in 1.6 version
Q10. Explain about Queue interface?
If we want to represent a group of individual objects prior to processing, then we should go for Queue interface. It is child interface of Collection interface.
It has introduced in 1.5 version.
Q11. Explain about Map interface?
Remember it is not a child Interface of Collection Interface and hence Map and Collection Interfaces doesn’t have any relationship.
  • It can be used for representing a group of Objects as key, value pairs.
  • Both keys and values should be objects
  • Keys can t be duplicated but values can be duplicated.
  • it has  introduced in 1.2 version
Q12. Explain about SortedMap ?
  • If we want to represent a group of objects as key value pairs where all the entries are arranged according some sorting order of keys then we should go for SortedMap.
  • It is child interface of Map.
  • It has  introduced in 1.2 version
Q13. Explain about NavigableMap?
  • It is child interface of SortedMap and defines several method for navigation purpose
  • It is introduced in 1.6 version


Q14.  Explain about ArrayList class?
 ArrayList is a Collection which can be used to represent a group of objects as a single entity.
  • it is a implemented class for  List interface
  • Introduced in 1.2 version
  • The underlying data structure is resizable or growable array.
  • Insertion order is preserved
  • Duplicates are allowed
  • Heterogeneous objects are allowed
  • null insertion is possible
  • This  class implements RandomAccess , Serializable , Cloneable interfaces
  • Best choice  for retrieval purpose and worst if our frequent operation is insertion or deletion in the middle
Q15. What is RandomAccess Interface?
  • If a collection class implements RandomAccess interface then we can access any of its element with the same speed.
  • RandomAccess interface is marker interface and it dosent contains any methods.
  • ArrayList and vector classes implements this interface.
Q16. Explain about LinkedList class?
LinkedList is a Collection implemented class which can be used for representing a group of objects as a single entity.
  • LinkedList is the implemetation class for List interface
  • Introduced in 1.2 version
  • Underlying data Structure is   DoubleLinkedList
  • Allows duplicates
  • Insertion order is preserved
  • Allows heterogeneous objects
  • null insertion is possible
  • LinkedList class implements Seriallizable and Cloneable interface but not RandomAccess interface
  • Best choice  if frequent operation is insertion or deletion an objects in middle  but worst choice if frequent operation is retrieval.
Q17. Explain about Vector class?
     Vector is a legacy collection class which can be used to represent a group of objects.
  • Introduced in 1.0 version. it is legacy class
  • The underlying data structure is resizable or growable array.
  • Insertion order is preserved
  • Duplicates are allowed
  • Heterogeneous objects are allowed
  • It is a implemented class for  List interface
  • null insertion is possible
  •  Vector class implements RandomAccess ,Serializable,Cloneable interfaces
  • Best Choice if frequent operation is retrieval and worst choice if frequent operation is insertion or deletion in the middle.
  • All methods present in Vector class are synchronized hence Vector class object is thread safe.
Q18. What is difference between ArrayList and Vector?

ArrayList
Vector
1. No method is synchronized in the ArrayList class
1. All methods in Vector are synchronized.
2. ArrayList object is not thread safe.
2.  Vector is thread safe.
3. Relatively performance is high
3. Relatively performance is low
4. Introduced in 1.2 version and it is non legacy
4. Introduced in 1.0 version and it is legacy

Q19. How we can get synchronized version of ArrayList?
Collections class contains synchronizedList() method for this
                Public static List synchronizedList (List l)
               
               
EX
                ArrayList l= new ArrayList ();
                List l2=Collections.synchronizedList (l);
Similarly we can get synchronized versions of Set and Map objects by the following methods.
Public static List synchronizedSet(Set s)
Public static List synchronizedMap(Map m)
Q20. What is difference between size and capacity of a Collection Object?
size means number  of objects present  where as capacity means no of objects it can accommodate.
Q21. What is difference between ArrayList and Linked List?
ArrayList
LinkedList
1. The underlying data structure is resizable or growable array.
1. The underlying data structure is Double Linked List.
2.  This is Best choice if frequent operation is retrieval and worst choice if frequent operation is insertion or deletion in the middle.
2.  This is Best choice  if frequent operation is insertion or deletion in the middle and worst choice if frequent operation is retrieval .
3. This class implements Serializable , Cloneable and RandomAccess interfaces.
3. This class implements Serializable , Cloneable but not  RandomAccess interface.
Q22. What are legacy classes and interfaces present  in Collections framework ?
  • Enumeration ---Interface
  • Dictonary ------Abstract class
  • Hashtable -----Concrete class
  • Properties -----Concrete class
  • Vector -----Concrete class
  • Stack  -----Concrete class

Q23. what is difference Enumeration and Iterator?
Enumeration
Iterator
1. It is legacy interface and introduced in 1.0 version
1 It is non-legacy and introduced in 1.2 version
2 Applicable only for legacy classes and it is not universal cursor
2Applicable for any Collection implemented class object.
3 While iterating the elements we are not allowed to remove the objects just we can perform only read operation
3While iterating we can perform removal also in addition to read operation.
4 By using elements() method we can get Enumeration object
4.   By using iterator() method we can get Iterator  
    object
Q24. What are limitations of Enumeration?
  • While iterating the elements we are not allowed to perform removal operation
  • It is applicable only for legacy classes and it is not a universal cursor.
  • It can retrieve the elements only in forward direction


Q25. What is difference between enum and Enumeration?
An enum can be used to define a group of named constants .It has  introduced in 1.5 version
Ex
Class Beer{
                KO,KF,RC,FO
}
Enumeration is cursor to retrieve Objects one by one from Collection objects.
Q26. What is difference between Iterator and ListIterator?
    • ListIterator is the child interface of the Iterator
    • Iterator is the single direction cursor where as ListIterator is bidirectional cursor.
    • While iterating the elements by Iterator we can perform only read and remove operations. But by using ListIterator we can perform read,removal, replace and addition of new objects also.
    • Iterator is applicable for every Collecton implemented class object but ListIterator  is applicable only for List implemented class objects.
    • Iterator can be get by using iterator() of Collection interface where as ListIterator can be get by using listIterator() method of List interface
    • both are introduced in 1.2 version
Q27. What is relation between ListIterator and Iterator?
   
      ListIterator is child interface of Iterator

Q28. Explain about HashSet class?
  • The underlying data structure is Hashtable
  • null values are accepted
  • duplicates are not allowed
  • insertion order is based on hashcode of the object hence insertion order is not preserved
  • best  suitable if frequent operation is  search operations
  • HashSet  class implements Serializable and Cloneable
  • it is implementation class for Set interface
  • heterogeneous objects are allowed
  • it is introduced in 1.2 version
Q29. If we are trying to insert duplicate values in Set what will happen?
 If we are trying to insert duplicate objects to the HashSet  , we wont get any compile time or run time errors just the add(Object o) returns false and it doesn’t add that object.
Q30. What is LinkedHashSet?
It is the child class of HashSet. The main difference between HashSet and LinkedHashSet is:
In  HashSet insertion order is not preserved , but in the case of LinkedHashSet insertion will be preserved.
Q31. Differences  between HashSet and LinkedHashSet?
HashSet
LinkedHashSet
1The Underlying datastructure is Hashtable
1The underlying datastructure is combination of LinkedList and Hashtable
2Insertion Order is not preserved
2     Insertion order is preserved.
3Introduced in 1.2 version
3     Introduced in 1.4 version

Q32. What are major enhancements in 1.4 version of collection frame work?
 LinkedHashSet
 LinkedHashMap
 IdentityHashMap
                                                                      
Q33. Explain about TreeSet?
    
 It is Collection object which can be used to represent a group of objects according to some sorting order.
  • The underlying datastructure is Balanced tree
  • Duplicates are not allowed
  • All objects are stored according to some sorting order hence insertion order is not preserved
  • Heterogeneous objects are not allowed violation leads to ClassCastException
  • For an Empty TreeSet as firs element null value can be inserted but after inserting that first value if we are trying to insert any other objects then we will get NullPointerException
  • For an non empty TreeSet if we are trying to  insert null value at run time u will get NullPointerException

Q34. What are differences between List and Set interfaces?
List
Set
1   Insertion Order is preserved
1   Insertion Order is not preserved
2   Duplicate Objects are allowed
2   Duplicate Objects are not allowed
3   The implemented classes are ArrayList, LinkedList , Vector and Stack classes
3   The implemented classes are HashSet,      LinkedHashSet and Tree
Q35. What is Comparable interface?
  • This interface can be used for defining natural sorting order of the objects.
  • It is present in java.lang package
  • It contains a method public int compareTo(Object obj1)
Q36. What is Comparator interface?
  • This interface can be used for implementing customized sorting order.
  • It is present in java.util package
  • It contains two methods
    • public int compare(Object ,Object)
    • public boolean equals(Object)

Q37. What are differences between Comparable and Comparator?
Comparable
Comparator
1  This can be used for natural sorting order
1 This can be used for implementing customized    sorting
2  This interface present in java.lang package
2 This is present in java.util package
3  Contains only one method:
public int compareTo(Object obj1)
3 It contains two methods.
public int compare(Object ,Object)
public Boolean equals(Object)
4    It is marker interface
4 It is not a marker interface.


Q38. What is difference between HashSet and TreeSet?
HashSet
TreeSet
1The underlying data structure is Hashtable
1The underlying data structure is balanced tree
2Heterogeneous objects are allowed
2       Heterogeneous objects are not allowed  bydefalut
3Insertion order is not preserved and it is based on hashcode of the objects
3   Insertion order is not preserved and all the objects are inserted according to some sorting order.
4null insertion is possible
4   As the first element only null insertion is   possible and in all other cases we will get NullPointerException
Q39. What is Entry interface?
It is inner interface of Map.
In the Map each key value pair is considered as Entry object.

interface Map{
                //more code here
                interface Entry{               
                                Object  getKey()
                                Object  getValue()
                                Object  setValue(Object new)
                }
}
Q40. Explain about HashMap?
It is a Map Object which can be used used to represent a group of objects as key-value pairs.
  • The underlying data structure is Hashtable
  • Duplicates keys are not allowed duplicate values are allowed
  • Insertion order is not preserved because insertion is based on hashcode of keys.
  • Heterogeneous objects are allowed for  both keys and values
  • null key is allowed  only once
  • null values  are allowed multiple times
  • Introduced in 1.2 version
Q41. Explain about LinkedHashMap?
It is child class of HashMap. It is exactly same as HashMap except the following difference.
In  HashMap the insertion order is not preserved but in LinkedHashMap insertion order is preserved.
Q42. Differences between HashMap and LinkedHashMap ?
HashMap
LinkedHashMap
1.The underlying data structure is Hashtable
1.The underlying data structure is a combination of Hashtable and linkedlist
2.Insertion order is not preserved and it is based on hashcode of keys
2    Insertion order is preserved
3.Introduced in 1.2 version
3   Introduced in 1.4 version.
Q43. Differences between HashMap and Hashtable?
HashMap
Hashtable
1.The underlying data structure is Hashtable
1.The underlying data structure of Hashtable
2.No method is synchronized and hence HashMap object is not thread safe
2 .All methods are synchronized and hence it is   thread safe
3.Performance is high
3.   Performance is low
4.null insertion is possible for both keys and values
4.   null insertion is not possible for both key and value violation leads to NullPointerException
5.Introduced in 1.2 version and it is non legacy
5.   Introduced in 1.0 version and it is legacy

Q44. What is IdentityHashMap? Or What is difference between HashMap and IdentityHashMap?
It is exactly same as HashMap except the following difference.
     In the HashMap JVM uses equals() method to identify duplicate keys  but in the  case of IdentityHashMap JVM uses == operator for this.

Q45. What is WeakHashMap? Or What is difference between HashMap and WeakHashMap?

      
  
It is exactly same as HashMap except the following difference.
 In case of HashMap an Object is not eligible for garbage collection if it is associated with HashMap even though it dosent have any external references i.e. HashMap dominates garbage collector.
But in case of WeakHashMap, if an Object is not having any external references then it is always eligible for garabage collectoion even though it is associated with weakHashMap i.e.  garbage collector dominates WeakHashMap
Q46. What is TreeMap?
TreeMap can be used to store a group of objects as key-value pairs where all the entries are arranged     according to some sorting order of keys.
  • The underlying data structure is RED-BLACK Tree
  • Duplicates keys are not allowed but values can be duplicated.
  • Insertion order is not preserved because insertion is based on some sorting order
  • If we are depending on Natural sorting order then keys should be homogeneous(violation leads to ClassCastException)  but values need not homogeneous
  • In case of customized sorting order we can insert  heterogeneous keys and values
  • For empty TreeMap as first entry with null values are allowed but after inserting that entry if we are trying to insert any other entry we will get NullPointerException
  • For non empty TreeMap if we are trying to insert null keys we will get NullPointerException
  • There are no restrictions for null values.

Q47. What is Hashtable
     
   
  Hashtable is a legacy Map and can be used to store objects as key value pairs.
  • The underlying data structure is Hashtabe
  • Duplicates keys are not allowed but duplicate values are allowed
  • null insertion is not possible for both keys and values
  • all methods are synchronized
  • insertion order is not preserved because it is  based on hashcode  of keys
  • heterogeneous Objects are allowed for both keys and values
  • introduced in 1.0 version it is legacy class
Q48. What is PriorityQueue?
It represents a data structure to hold group of individual objects prior to processing based on some priority .it can be natural sorting order and it can be customized sorting order described by Comparator.
It is the implementation class of Queue interface.
  • Insertion order is not preserved because here insertion is done based on some sorting order
  • Duplicates are not allowed
  • null insertion is not possible even as first element also
  •  If we are depending on natural sorting order Objects should be homogeneous  violation leads to ClassCastException
  •  If we are depending on customized  sorting order Objects can be heterogeneous also.
Q49. What is Arrays class?
  • It is utility class for arrays.
  • It defines several utility methods for arrays like sorting an array or searching an element in array
  • present in java.util package
Q50. We are planning to do an indexed search in a list of objects. Which of the two Java collections should you use: ArrayList or LinkedList?
ArrayList
Q51. Why ArrayList is faster than Vector?  
All methods present in the Vector are synchronized  and hence  any method can be executed by only one thread at a time. It slows down the execution.
But in ArrayList,  no method is synchronized and hence multiple thread are allowed execute simultaneously which speed up the execution.




happy diwali
happy diwali