Search This Blog

Thursday 3 October 2013

JAVA Interview Question And Answer

------------------------------------------------------------------------------------------------------------------------------
Question: What is System.out in Java?
Here out is an instance of PrintStream. It is a static member variable in
System class. This is called standard output stream, connected to console.
------------------------------------------------------------------------------------------------------------------------------
Question: Can we have static methods in interface?
By default, all methods in an interface are decleared as public, abstract. It will never be static.
------------------------------------------------------------------------------------------------------------------------------

Question: What is java static import?
By using static imports, we can import the static members from a class
rather than the classes from a given package.  For example, Thread class has
static sleep method, below example gives an idea:

import static java.lang.Thread;
public class MyStaticImportTest {
   public static void main(String[] a) {
       try{
           sleep(100);
       } catch(Exception ex){
       
       }
   }
}
------------------------------------------------------------------------------------------------------------------------------
Question: What is abstract class or abstract method?
We cannot create instance for an abstract class. We can able to create
instance for its subclass only. By specifying abstract keyword just before
class, we can make a class as abstract class.

public abstract class MyAbstractClass{

}

Abstract class may or may not contains abstract methods. Abstract method is
just method signature, it does not containes any implementation. Its subclass
must provide implementation for abstract methods. Abstract methods are looks
like as given below:
public abstract int getLength();
------------------------------------------------------------------------------------------------------------------------------
Question: When to use LinkedList or ArrayList?
Accessing elements are faster with ArrayList, because it is index based.But accessing is difficult with LinkedList. It is slow access. This is to access any element, you need to navigate through the elements one by one. But insertion and deletion is much faster with LinkedList, because if you know the node, just change the pointers before or after nodes. Insertion and deletion is slow with ArrayList, this is because, during these operations ArrayList need to adjust the indexes according to
deletion or insetion if you are performing on middle indexes. Means,an ArrayList having 10 elements, if you are inserting at index 5, then you need to shift the indexes above 5 to one more.
------------------------------------------------------------------------------------------------------------------------------
Question: What is final, finally and finalize?

By specifying final keyword to the method you can avoid overriding
in a subcalss. Similarlly one can use final at class level to prevent creating subclasses.

final:
   final is a keyword. The variable decleared as final should be
   initialized only once and cannot be changed. Java classes
   declared as final cannot be extended. Methods declared as final
   cannot be overridden.
   
finally:
   finally is a block. The finally block always executes when the
   try block exits. This ensures that the finally block is executed
   even if an unexpected exception occurs. But finally is useful for
   more than just exception handling - it allows the programmer to
   avoid having cleanup code accidentally bypassed by a return,
   continue, or break. Putting cleanup code in a finally block is
   always a good practice, even when no exceptions are anticipated.
   
finalize:
   finalize is a method. Before an object is garbage collected, the
   runtime system calls its finalize() method. You can write system
   resources release code in finalize() method before getting garbage
   collected.
------------------------------------------------------------------------------------------------------------------------------
Question: Can interface be final?
No. We can not instantiate interfaces, so in order to make interfaces useful we must create subclasses. The final keyword makes a class unable to be extended.
------------------------------------------------------------------------------------------------------------------------------
Question: What are the different session tracking methods?
Cookies:
   You can use HTTP cookies to store information. Cookies will be
   stored at browser side.

URL rewriting:
   With this method, the information is carried through url as
   request parameters. In general added parameter will be sessionid,
   userid.

HttpSession:
   Using HttpSession, we can store information at server side. Http
   Session provides methods to handle session related information.
   
Hidden form fields:
   By using hidden form fields we can insert information in the webpages
   and these information will be sent to the server. These fields are not
   visible directly to the user, but can be viewed    using view source
   option from the browsers. The hidden form fields are as given below:
       <input type='hidden' name='blogName' value='PrabhakarSinghBlogs'/>
  ------------------------------------------------------------------------------------------------------------------------------     
Question: What is the difference between Enumeration and Iterator?
The functionality of Enumeration and the Iterator are same. You can get remove()
from Iterator to remove an element, while while Enumeration does not have remove()
method. Using Enumeration you can only traverse and fetch the objects, where as using
Iterator we can also add and remove the objects. So Iterator can be useful if you want
to manipulate the list and Enumeration is for read-only access.

------------------------------------------------------------------------------------------------------------------------------
Question: How can you convert Map to List?
We know that Map contains key-value pairs, whereas a list contains
only objects. Since Entry class contains both key-value pair,
Entry class will helps us to convert from Map (HashMap) to
List (ArrayList). By using Map.entrySet() you will get Set
object, which intern you can use it to convert to list object.

public static void main(String a[]){
   Map<String, String> wordMap = new HashMap<String, String>();
   Set<Entry<String, String>> set = wordMap.entrySet();
   List<Entry<String, String>> list = new ArrayList<Entry<String, String>>(set);
}
------------------------------------------------------------------------------------------------------------------------------
Question: What is serialization?
Question: What is serialization?
Question: What is a user defined exception? Explain with an Example ?
Question: What are checked and unchecked exceptions?
Question: What is the difference between an Abstract class and Interface?

No comments:

Post a Comment