Sunday 24 August 2014

Java / J2ee interview questions for experience java developer




Interview generally starts with:
                 *  Tell me about yourself.
                 * Tell me about your current project and your role in it. And few related questions on project, if he gets what you have explained.

       Then they start with core java. Be very much perfect in core java concepts, because this is the section which will decide your fate in interview. Even if you suck at j2ee part, and if you are very much good at core java concepts then consider you are almost in. Best book I suggest for core java preparation is SCJP (OCJP) by Kathy Sierra.  And following are few important questions for your quick review.

     Key point to remember:  Do not answer the question if you don’t know the answer. It’s better than answering it wrong.

   

Core Java interview questions for experienced developer:

  1.   Explain OOPS concepts with example.
  2.  What is difference between abstraction and encapsulation?
  3.  What is difference between abstract class and Interface?
  4.   What is polymorphism? Explain with example.
  5.   Can we override static method?
  6.  Can we overload the static method?
  7.  What is base class of java?And what methods it provides? Ans: It's Object and methods are : Clone(), equals(), hashCode(), finalize(), notify(), notifyAll(), wait(), toString()
  8. How variables are defined in interface and how to access it?
  9.  Explain System.out.println() in detail     
  10. What is final object in java? 
  11. What is static import in java?
  12. What is agreement between equals() and  hashcode() methods in java?
  13. What will print on console, if I don’t override toString() method and I pass object to  System.out.println()? Ans: If you don’t override toString() method & pass object to System.out.println(), then it will print classname followed by @ symbol, followed by unsigned hexadecimal representation of objects  hashcode. 


Now as you are 3+ years of experience, generally interviewers don’t ask you direct question. They might ask you to consider a scenario and asks questions in-between or expect you to correct him, if he is going wrong somewhere. Consider the following scenarios: 

Scenario 1:
I have two classes A & B. Where class B extends class A. And now I’m having one more Test class, to test it. Now I have created one reference of class B and provide it object of class A. Like, 
                  B obj = new A();   

Here, he expects you to correct him, that you cannot store object of parent class into reference of base class.


Scenario 2:
File name: Test.java
class A{
      String name = "Parent Class";
      public void printMe(){
            System.out.println("This is parent class");
      }
}

class B extends A{
      String name = "Base Class";
      public void printMe(){
            System.out.println("This is base class");
      }
}
public class Test {
      public static void main(String[] args) {
            A objA = new B();
            System.out.println("Name is :"+objA.name);
            objA.printMe();
      }

}

Now when I run this test class, what will be the output?

Output:
Name is :Parent Class
This is base class

Now few things to learn here:


      #   Java file may contain more than one class classes, but only one public class. And this public class name should be same as that of file name.
     #  Java provides method overriding but not variable overriding. That is why, when I print variable name, it will pick up the variable definition from the class of which reference is created i.e. A. And method of the class of which object is created i.e. B.

      

       Interview question on String class in Java:

  1. What is difference between String, StringBuffer and StingBuilder? In what scenarios they should be used?
  2. What is String.intern() method?
  3. What is mutable and immutable class? Where does String class belongs to? How to write immutable class
  4. What will be output for following:
       
String a = "abc";
            String b = "abc";
            String c = new String("abc");
            String d = new String("abc");

if(a==b)
                  System.out.println("a==b");
           
            if(a==c)
                  System.out.println("a==c");
           
            if(c==d)
                  System.out.println("c==d");
           
            if(a.equals(b))
                  System.out.println("a equals b");
           
            if(a.equals(c))
                  System.out.println("a equals c");
           
            if(c.equals(d))
                  System.out.println("c equals d");

     Output:
a==b
a equals b
a equals c
c equals d

  

Important point about String, you should know:

  1. String is final class, this means you cannot extend it or inherit it.
  2. String is immutable. This means, every time you assign value to it, it will be stored at new memory location.
  3. Whenever we assign value to String variable directly using double quote (without new keyword), it will check for that value in String pool and if it exists, it directly reference to that memory location without creating new one.
  4. And whenever we use new keyword, it creates new memory location to store that object.
  5. As we know == checks for memory location to compare, it will not return true for a==c & c==d  (.equals() method checks for value in object)

 

       Interview question on Serialization in Java: 

  1.  What is serialization? Why we use it? How did you use it? Short Ans : The primary purpose of serialization is to write an object into a stream, so that it can be transported through network and object can be rebuild from it on the other end. And other  purpose is to store state of object and retrieve it back. Or you can say to persist the object  across the sessions.
  2. I serialize the object which contains reference of non serialized object? Ans: No, it will throw nonSerializableException
  3. Explain steps of serialization
  4. What are transient variables?  Give real time example.  Ans:  Variables which you don't want to serialized are marked as transient. Example age in student object is kept transient, so as to it will be calculated on the basis of its birth date and current date comparison.
  5. Can we mark method as transient ? Ans: No 
 

   

 



TO BE CONTINUED.....