Wikipedia

Search results

Friday, April 11, 2014

Java Interview Common Questionnaire

Hi Guys,

Based on my experience in the software industry, there are some important topics you need to prepare for the interview for any of your dream company.
If you study this topic, you will definitely crack the technical round interview, I will also suggest some books at the end of the topic which will get you through understanding java in easy way.

The topics you need to focus on, for the Technical Interview on Java:
  1. Inheritance : In this you have to be perfect in the concept of "overriding" and "overloading". Much more tricky questions are asked on this topic which will confuse you but if you keep in mind the basic principal then it will be just a piece of cake for you.
Eg : Interviewer will write the class A and class B, and extend the class A with the class B i.e Class B extends class A. In that super class they will write the method which will be public but in the child class they will override method but will change the method access modifier to private. In this case the answer is that it wont work.
class A{
      public void getMeSomething(){
      }
}

 class B extends class A{
       private void getMeSomething(){
       }
}

In this case it wont work. Like this you will find many more examples go through "SCJP" Java the book which I recommend the most and the "Head First Java".

    2. Constructors:
  • In java constructors play an important role. Interviewer may ask many confusing questions on constructors. Such as can constructor of the class can be private? so do you get it now, if you make the constructor private then no one except that method of that class only can create an object of that class and ultimately none other class will be able to create the object of that class.
Eg:  class A{
                          private A(){
                          }
       }
when you actually create the instance of class A then at that time constructor gets invoked. In this example only we cannot directly create the instance of the class directly we have to rely on the method in the class.
  •     Most important thing the interviewer ask about constructor is that he will extend the classes and write the System.out.println(""); in it and he will ask you to what it will print after creating an object of the child class. Let me explain this with example:
Eg:
class A{
                  A(){
                         syso("A");
                        }
 }
class B extends A{
                    B(){
                              syso("B");
                   }
                   public static void main(String []args){
                              B b = new B();
                   }
}
In this case the output will be A , B. Because the super class constructor get called first the the child class in this case. Like this you will find a lot of question easy when your concept will be clear. I recommend strongly to read Java SCJP book thoroughly even if you are still pursuing degree of engineering.

      3.String:  String is the most important concept to understand once you understand the concept of the string and string pool it will be a lot better for you to answer the question on the string:
  • You need to study the how the object are created and destroyed on the heap.
 Eg : what will happen if the string are created and assigned to the different String? What will the actual result on the heap how the reference variable will work in this case, all you have to study to know the string well?
  • Pay extra attention towards what are the different methods in the String class that can used such as substring, length ,lastIndexOf etc.
  • Interviewer will ask the difference between String Buffer and String Builder for sure. Main difference is that String Buffer is synchronized and StringBuilder is not.
  • Read all the methods related to the StringBuffer and StringBuilder also.
     4. Exceptions: Exception is also one of the most important topic you have to consider the following points during interview:
  • The types of exceptions?
  • How will you create the custom exceptions?
  • Can the try block be written without the catch and only using finally? 
      Eg: class A{
                          try{
                                ...................
                                ......................
                               }finally{
                                ...............
                               }
                         }
  • And the answer to the above question is "Yes", it is possible to write the try block without using the catch block but in this case the finally block will not be used to catch the exceptions.
  • Like this they will also ask what is the use of the finally block?
  • what will happen if we catch the generalized exception first and then the particular exception?
       Eg : try{
                             number format exception occurs;
                   }catch(Exception e){
                             syso("Exception");
                   }catch(NumberFormatException e){
                             syso("number format Exception");
                   }
  • In the above case, It will show the unreachable code exception because all the exception will be consumed by the above Exception catch statement.
         5. Collections : Java Collections chapter what you have to really focus on most of the question come from this chapter:
  • Define ArrayList<?>(); ?
  • How to sort any collection?
  • How to use comparable Interface?
  • How to sort two objects based on the particular instance variable?
Eg : Example the class contains the emp id , emp name ,emp salary  all this is in the array list then you have to sort it on the basis of the emp id. Like this, question will be appearing in the interview.

         6. Equals and Hashcode : The main question asked on this type will be:
  •  Is it necessary to override both hashcode and equals method at the same time?
  • What will happen if the equals is overridden and not the hashcode?
Ans: To fulfill the contract between the hashcode and the equals method we have to override both the method.Why? Bcoz when the two objects are considered equal they should also have the same hashcode otherwise hashcode function will generate the different key for same object and the objects will fall in different bucket so while retrieving the object it can return wrong value corresponding to the object.So the contract must not be violated.

        7. Difference between Interface and abstraction? when should we use interface and when should we use Abstraction?
  • The case where the implementation changes according the class we should use the interface. Suppose the birds class and Jet Plane class is there, both have the method in common fly but how they fly has to be written in the concrete class by the developer.And In case of abstraction consider the example of the alarm clock the may come with certain functions such as the current time and date. When you have to set the alarm that functionality may be given to the developer to implement it or if the developer wants then he can also override the in-built methods. And we can extend only one abstract class and we can implement multiple interfaces.
So guys these are the basic questions that every interviewer asks, if are trying to switch the job or preparing for the campus recruitment then you can find this blog more helpful.
For Java you should try reading SCJP and java headfirst it will be very helpful and the most convenient way to learn java.

Please let me know if you like the blog.