Java interview questions


1. Give a few reasons for using Java?

Java is a fun language. Let's look at some of the reasons:

  • Built-in  support for multithreading, socket communication, and memory management(automatic garbage collection)
  • Object oriented()
  • Better portability than other languages across operating systems.
  • Supports web applications, distributed applications and network protocols with the help of extensive standardized APIs


2. Explain the usage of Java packages.
This is a way to organize files when a project consists of multiple modules. It also helps resolve naming conflicts when different packages have classes with the same names. Packages access level also allows you to protect data from being used by the non-authorized classes.


3. What is the difference between constructors and other regular methods?



Regular methods

Constructors must have the same name as the class name and cannot return a value. Constructors are called only once per creation of an object while regular methods can be called many times. E.g for a Pet.class


public Pet(){}//constructor

Regular methods can have any name and can be called any number of times E.g for a Pet.class

Public void Pet(){}// regular method has            void return type.


Note: Method name is shown with an uppercase to differentiate a constructor from regular method. Better naming convention is to have a meaningful name starting with a lowercase like:


public void createPet(){} //regular method has a void return type.





4. What happens if you do not  provide a constructor? Can you call one constructor from another? How do you call  the superclass's constructor?

Java does not actually require an explicit constructor in the class description. If you do not include a constructor, the Java compiler will create a default constructor in the  byte code with an empty argument. This default constructor is equivalent to the explicit "Pet(){}". If a class includes one or more explicit constructors like "public Pet(int id)" or "Pet(){} etc, the java compiler does not create the default constructor.

            Yes we can call one constructor from another using this() syntax. E.g.

public Pet(int id){;


public Pet (int id, String type){


this.type = type;


We can call the superclass's constructor using the "super" keyword E.g. If a class "SpecialPet"  extends your Pet class then  you can use "super" keyword to invoke the superclass's constructor


public specialPet(int id){

super(id); //must be very first statement in the constructor.



5. What's the difference between an interface and an abstract class? Also discuss the similarities?


Abstract class
Abstract class is a class which contains one or more abstract methods, which has to be implemented by sub classes. Abstract class is a Class prefix with a abstract keyword followed by Class definition. Abstract class contains one or more abstract methods and also concrete methods. Abstract classes are useful in a situation that some general methods should be implemented and specialization behavior should be implemented by child classes.



Interface is a Java Object containing method declaration and doesn't contain implementation. The classes which have implementing the Interfaces must provide the method definition for all the methods. Interface is a Interface which starts with interface keyword. Interface contains all abstract methods and final declarations. Interfaces are useful in a situation that all properties should be implemented.

Differences are as follows:

  • Interfaces provide a form of multiple inheritance. A class can extend only one other class.
  • Interfaces are limited to public methods and constants with no implementation. Abstract classes can have a partial implementation, protected parts, static methods, etc.
  • A Class may implement several interfaces. But in case of abstract class, a class may extend only one abstract class.
  • Interfaces are slow as it requires extra indirection to find corresponding method in                                                                 the actual class. Abstract classes are fast. 



  Either Abstract classes or Interface can be instantiated.


6. What is transient variable and tell the usage of this variable?
We can not serialize the transient variable. Transient keyword indicates that the value of this member variable does not have to be serialized with the object. When the class will be de-serialized, this variable will be initialized with a default value of its data type (i.e. zero for integers).


7. What is the difference between finally and finalize() method?

  finally The finally block always executes when the try block exits, except System.exit(0) call. 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 () method helps in garbage collection. A method that is invoked before an object is discarded by the garbage collector, allowing it to clean up its state. Should not be used to release non-memory resources like file handles, sockets, database connections etc because Java has only a finite number of these resources and you do not know when the garbage collection is going to kick in to release these non-memory resources through the finalize() method.


8 .  What is life cycle of a thread?

  1. New state After the creations of Thread instance the thread is in this state but before the start() method invocation. At this point, the thread is considered not alive.  
  2. Runnable (Ready-to-run) state A thread start its life from Runnable state. A thread first enters runnable state after the invoking of start() method but a thread can return to this state after either running, waiting, sleeping or coming back from blocked state also. On this state a thread is waiting for a turn on the processor. 
  3. Running state A thread is in running state that means the thread is currently executing. There are several ways to enter in Runnable state but there is only one way to enter in Running state: the scheduler select a thread from runnable pool.
  4. Dead state A thread can be considered dead when its run() method completes. If any thread comes on this state that means it cannot ever run again.
  1. Blocked - A thread can enter in this state because of waiting the resources that are hold by another thread.


9.  Can we declare an anonymous class as both extending a class and implementing an interface?

No. An anonymous class can extend a class or implement an interface, but it cannot be declared to do both


10. How do you know if an explicit object casting is needed?
If you assign a superclass object to a variable of a subclass's data type, you need to do explicit casting. For example:
Object a; Pet b; b = (Pet) a;


11. Wat is Collection API?

Collection API is a set of classes and interfaces that support operation on collections of objects. These classes and interfaces are more flexible, more powerful, and more regular than the vectors, arrays, and hashtables if effectively replaces. 
Example of classes: HashSet, HashMap, ArrayList, LinkedList, TreeSet and TreeMap.
Example of interfaces: Collection, Set, List and Map.


12. Is it possible to print the statments without invoking a main method?

yes. using static block




13. What is  type casting ?How many types are there? when do you get ClasscastException?

Type casting means treating a variable of one type as through it is another type.

There are two types of typecasting

1> upcasting ---> Here automatic conversion occurs and casting occurs from left to right

2> down casting---> Here casting occurs from right to left and its is explicit cast.

 The ClasscastException is thrown to indicate that code has attempted to cast an object to a subclass of which it is not an instance. For example


Object 0= new integer(1);

System.out.println((String) o);

}catch(ClasscastExacption cce){

logger.log("Invalid casting, String is expected .. Not an Integer");




14. What is the difference between "==" operator and equal() method?


The equals( ) method compares the characters inside a String object. The == operator compares two object references to see whether they refer to the same instance.

Sandeep Joshi
Mathematics, Technology and Programming are my passion. I am a part of Java Ecosystem and through this blog, I contribute to it. I am here to blog about my interests, views and experiences.
I am on Google+ and Facebook.
I feel proud to be listed as a "National Memory Record Holder" in the Limca Book of Records, 2009 and have attempted for an International Memory record in the Guiness Book of Records. I can remember the value of PI upto 10,000 digits after the decimal (3.1415.....). You can contact me on ; I would like to hear from you :)