25 Java Interview Questions
What are the main features of Java?  Object Oriented  Simple  Platform Independent  Secured  Robust  Portable  Multithreaded  Distributed
What are the fundamental principles of object oriented programming?  Inheritance  Abstraction  Polymorphism  Encapsulation
What do you mean by inheritance in java?  Inheritance is one of the Key principle of object oriented programming. Through inheritance, one class can inherit the properties of another class. The class from which properties are inherited is called super class and the class to which properties are inherited is called sub class
What is constructor overloading? What is the use of constructor overloading?  A class can Have any number of constructors. These constructors will have different list of arguments. It is called constructor overloading. Constructor overloading provides different way to instantiate a class
What is polymorphism in java?  Polymorphism refers to any entity whether it is a method or a constructor or an operator which takes many forms or can be used for multiple tasks.
What is the method overloading in java?  If a class has more than one method with same name but with different list of arguments, then it is called method overloading.
What is the method overriding?  If a super class method is modified in the sub class then it is called method overriding.
Does java supports multiple inheritance?  Java supports multiple inheritance but only through interfaces. That means a class can implement more than one interfaces but can not extend more than one class.
What is the difference between constructor and method?  Constructor is a special member of a class which is used to create the objects to the class. It is special because it will have same name as class. It will have no return type.  Method is ordinary member of a class which is used to implement some behavior of a class. It will have it’s own name and return type.
Can we overload the main() method?  Yes, we can overload a main() method. A class can have any number of main() methods. But, one of those must be in the form “public static void main(String[] args)” in order to start the execution.
How the exceptions are handled in java? OR Explain try, catch and finally blocks in java?  Java has it’s own mechanism to handle the exceptions. In Java, exceptions are handled using three blocks – try, catch and finally blocks.  try block – The code to be monitored for exceptions will be kept in this block.  catch block – If any exceptions occurred in try block, those exceptions will be caught by this block.  finally block – This block will be always executed whether exception is raised or not and raised exceptions are caught or not.
What are PATH and CLASSPATH?  PATH and CLASSPATH are two environment variables which need to be set in order to compile and run the java programs.
What is multithreaded programming?  Multithreaded programming is one of the key features of java which allows multiple threads to execute their task simultaneously
What is the difference between error and exception in java?
What are the differences between static and non-static methods? Static method is common to all instances of a class. Static methods are stored in the class memory. Where as non-static methods are stored in the object memory. Each instance of a class will have their own copy of non-static methods.
What are the differences between method overloading and method overriding?
What are the different ways of creating threads in java?  There are two ways to create the threads in java  a) By extending java.lang.Thread class.  b) By implementing java.lang.Runnable interface.
What is static binding and dynamic binding in java?
What is garbage collection in java? Removing unwanted objects or abandoned objects from the memory is called garbage collection. Garbage collection is done automatically in java. You need not to remove the unwanted objects explicitly. Garbage collector thread does this for you. click here to see how garbage collector thread works in java.
What is the difference between ArrayList and Vector Class? 1) Thread Safety This is the main difference between ArrayList and Vector class. ArrayList class is not thread safety where as Vector class is thread safety. Vector class is a synchronized class. Only one thread can enter into Vector object at any moment of time during execution. Where as ArrayList class is not synchronized. Multiple threads can access ArrayList object simultaneously. Below diagram clearly shows that.
What is the difference between ArrayList and Vector Class?  Performance ArrayList has better performance compared to Vector. It is because, Vector class is synchronized. It makes the threads to wait for object lock to enter into vector object. Where as ArrayList class is not synchronized. Threads need not to wait for object lock to access ArrayList object. This makes ArrayList faster than the Vector class.
What is cloning in java?  Cloning is a process of creating an exact copy of an existing object in the memory. Cloning may be shallow or deep. In java, clone() method is used to create a clone of an object
What are differences between final, finally and finalize in java?  final is a keyword which is used to make a variable or a method or a class as “unchangeable“. In simple terms,  A variable which is declared as final, it’s value can not be changed once it is initialized.
finally Block :  finally is a block which is used for exception handling along with try and catch blocks. finally block is always executed whether exception is raised or not and raised exception is handled or not. Most of time, this block is used to close the resources like database connection, I/O resources etc.
finalize() Method :  finalize() method is a protected method of java.lang.Object class. It is inherited to every class you create in java. This method is called by garbage collector thread before an object is removed from the memory. finalize() method is used to perform some clean up operations on an object before it is removed from the memory.
What are checked and unchecked exceptions in java?
Checked Exceptions :  Checked exceptions are the exceptions which are known during compile time. These are the exceptions that are checked at compile time. They are also called compile time exceptions.  These exceptions must be handled either using try-catch blocks or using throws clause. If not handled properly, they will give compile time error.  All sub classes of java.lang.Exception except sub classes of RunTimeException are checked exceptions.  The following example throws checked exception. But it is not handled, so it gives compile time error.  public class CheckedException  {  public static void main(String[] args)  {  Class.forName("AnyClassName");   //Compile time error because  //above statement throws ClassNotFoundException which is a checked exception  //this statement must be enclosed within try-catch block or declare main method with throws clause  }  }
Unchecked Exceptions :  Unchecked exceptions are the exceptions which are known at run time. They can not be known at compile time because they occur only at run time. That’s why they are also called Run Time Exceptions.  All the sub classes of RunTimeException and all sub classes of Error class are unchecked exceptions.  If any statement in the program throws unchecked exceptions and you are not handling them either using try-catch blocks or throws clause, then it does not give compile time error. Compilation will be successful but program may fail at run time. Therefore, to avoid premature termination of the program, you have to handle them properly.

25 java interview questions

  • 1.
  • 2.
    What are themain features of Java?  Object Oriented  Simple  Platform Independent  Secured  Robust  Portable  Multithreaded  Distributed
  • 3.
    What are thefundamental principles of object oriented programming?  Inheritance  Abstraction  Polymorphism  Encapsulation
  • 4.
    What do youmean by inheritance in java?  Inheritance is one of the Key principle of object oriented programming. Through inheritance, one class can inherit the properties of another class. The class from which properties are inherited is called super class and the class to which properties are inherited is called sub class
  • 5.
    What is constructoroverloading? What is the use of constructor overloading?  A class can Have any number of constructors. These constructors will have different list of arguments. It is called constructor overloading. Constructor overloading provides different way to instantiate a class
  • 6.
    What is polymorphismin java?  Polymorphism refers to any entity whether it is a method or a constructor or an operator which takes many forms or can be used for multiple tasks.
  • 7.
    What is themethod overloading in java?  If a class has more than one method with same name but with different list of arguments, then it is called method overloading.
  • 8.
    What is themethod overriding?  If a super class method is modified in the sub class then it is called method overriding.
  • 9.
    Does java supportsmultiple inheritance?  Java supports multiple inheritance but only through interfaces. That means a class can implement more than one interfaces but can not extend more than one class.
  • 10.
    What is thedifference between constructor and method?  Constructor is a special member of a class which is used to create the objects to the class. It is special because it will have same name as class. It will have no return type.  Method is ordinary member of a class which is used to implement some behavior of a class. It will have it’s own name and return type.
  • 11.
    Can we overloadthe main() method?  Yes, we can overload a main() method. A class can have any number of main() methods. But, one of those must be in the form “public static void main(String[] args)” in order to start the execution.
  • 12.
    How the exceptionsare handled in java? OR Explain try, catch and finally blocks in java?  Java has it’s own mechanism to handle the exceptions. In Java, exceptions are handled using three blocks – try, catch and finally blocks.  try block – The code to be monitored for exceptions will be kept in this block.  catch block – If any exceptions occurred in try block, those exceptions will be caught by this block.  finally block – This block will be always executed whether exception is raised or not and raised exceptions are caught or not.
  • 13.
    What are PATHand CLASSPATH?  PATH and CLASSPATH are two environment variables which need to be set in order to compile and run the java programs.
  • 14.
    What is multithreadedprogramming?  Multithreaded programming is one of the key features of java which allows multiple threads to execute their task simultaneously
  • 15.
    What is thedifference between error and exception in java?
  • 16.
    What are thedifferences between static and non-static methods? Static method is common to all instances of a class. Static methods are stored in the class memory. Where as non-static methods are stored in the object memory. Each instance of a class will have their own copy of non-static methods.
  • 17.
    What are thedifferences between method overloading and method overriding?
  • 18.
    What are thedifferent ways of creating threads in java?  There are two ways to create the threads in java  a) By extending java.lang.Thread class.  b) By implementing java.lang.Runnable interface.
  • 19.
    What is staticbinding and dynamic binding in java?
  • 20.
    What is garbagecollection in java? Removing unwanted objects or abandoned objects from the memory is called garbage collection. Garbage collection is done automatically in java. You need not to remove the unwanted objects explicitly. Garbage collector thread does this for you. click here to see how garbage collector thread works in java.
  • 21.
    What is thedifference between ArrayList and Vector Class? 1) Thread Safety This is the main difference between ArrayList and Vector class. ArrayList class is not thread safety where as Vector class is thread safety. Vector class is a synchronized class. Only one thread can enter into Vector object at any moment of time during execution. Where as ArrayList class is not synchronized. Multiple threads can access ArrayList object simultaneously. Below diagram clearly shows that.
  • 22.
    What is thedifference between ArrayList and Vector Class?  Performance ArrayList has better performance compared to Vector. It is because, Vector class is synchronized. It makes the threads to wait for object lock to enter into vector object. Where as ArrayList class is not synchronized. Threads need not to wait for object lock to access ArrayList object. This makes ArrayList faster than the Vector class.
  • 23.
    What is cloningin java?  Cloning is a process of creating an exact copy of an existing object in the memory. Cloning may be shallow or deep. In java, clone() method is used to create a clone of an object
  • 24.
    What are differencesbetween final, finally and finalize in java?  final is a keyword which is used to make a variable or a method or a class as “unchangeable“. In simple terms,  A variable which is declared as final, it’s value can not be changed once it is initialized.
  • 25.
    finally Block : finally is a block which is used for exception handling along with try and catch blocks. finally block is always executed whether exception is raised or not and raised exception is handled or not. Most of time, this block is used to close the resources like database connection, I/O resources etc.
  • 26.
    finalize() Method : finalize() method is a protected method of java.lang.Object class. It is inherited to every class you create in java. This method is called by garbage collector thread before an object is removed from the memory. finalize() method is used to perform some clean up operations on an object before it is removed from the memory.
  • 27.
    What are checkedand unchecked exceptions in java?
  • 28.
    Checked Exceptions : Checked exceptions are the exceptions which are known during compile time. These are the exceptions that are checked at compile time. They are also called compile time exceptions.  These exceptions must be handled either using try-catch blocks or using throws clause. If not handled properly, they will give compile time error.  All sub classes of java.lang.Exception except sub classes of RunTimeException are checked exceptions.  The following example throws checked exception. But it is not handled, so it gives compile time error.  public class CheckedException  {  public static void main(String[] args)  {  Class.forName("AnyClassName");   //Compile time error because  //above statement throws ClassNotFoundException which is a checked exception  //this statement must be enclosed within try-catch block or declare main method with throws clause  }  }
  • 29.
    Unchecked Exceptions : Unchecked exceptions are the exceptions which are known at run time. They can not be known at compile time because they occur only at run time. That’s why they are also called Run Time Exceptions.  All the sub classes of RunTimeException and all sub classes of Error class are unchecked exceptions.  If any statement in the program throws unchecked exceptions and you are not handling them either using try-catch blocks or throws clause, then it does not give compile time error. Compilation will be successful but program may fail at run time. Therefore, to avoid premature termination of the program, you have to handle them properly.