Here is a collection of frequently asked java interview questions and answers for technical interviews. All Java questions are provided with appropriate answers. If you find any answer is not correct, do comment in the comments section (scroll down).
Note: Click the question or click on the arrow to see the answer for a question.
Question and Answers
What is the implicit return type of the constructor?
The implicit return type of a constructor is the class name itself. For example if we are creating a constructor for class Student, then the return type of constructor is Student.
Can objects be stored in arrays?
If the array's type is a class, array can store objects. It will not exactly store the object itself but a reference to the object. This is similar to the concept of an array of pointers in C and C++.
A public class with the name “MyClass” has been declared inside a java file. Can I save the file with the name “AnotherClass.java”?
If a class is created with public access specifier, then the java file must be saved with the name of that class. So, this also implies that a java file cannot contain more than one public class.
How to access the variables and methods in a class without creating a object for the class?
If the variable or method is declared as static, we can access them using the class name itself without creating any object. One example method called directly without creating any object is main() method. This is the reason why main() is always prefixed with the static keyword.
Can a static method access instance variables?
A static method can access only static variables (also called class variables).
Can a static method be overloaded?
Yes. A static method can be overloaded but cannot be overriden.
Can a static method access non-static method?
Static methods can access only other static methods.
What are the default values of instance variables and local variables?
For instance variables, default value is 0 for int, 0.0 for float and double. Local variables does not have any default values. They must be initialized before using them in the program.
What is the use of final in java?
The final keyword can be used to create constants, create methods which are non-overridable, and to create class which cannot be inherited.
How to create strings in Java?
Strings can be created using the classes: String, StringBuffer, or StringBuilder.
Difference between String, StringBuffer and StringBuilder?
String class is for creating immutable strings. StringBuffer and StringBuilder classes are for creating mutable strings. StringBuffer methods are synchronized, whereas, StringBuilder methods are not synchronized.
Difference between == and equals() in the case of strings?
In the case of strings, == operator checks whether two references are pointing to the same object (memory location) or not. For example, consider the following code:
String s1 = new String("hello");
String s2 = new String("hello");
If we compare s1 and s2 as s1==s2, it evaluates to false as s1 and s2 reference separate memory locations (objects). To compare the string content we have to write s1.equals(s2). It evaluates to true.
What is the use of inheritance?
Main use of inheritance is reusability. General properties are moved to parent class and special properties and behavior is specified in the child classes.
Is multiple inheritance supported by Java?
In case of classes multiple inheritance is not supported. But in case of interfaces multiple inheritance is supported.
Use of super in java?
The super keyword is used to refer immediate super class (parent class) constructor and immediate super class (parent class) members.
What is method overriding?
If parent class and child class contains a method with same signature, and if child class method is called, then child class method executes hiding the parent class method. This process is known as method overriding.
What is dynamic method dispatch?
When a parent class reference is used to invoke a child class method, the association between the method call and method definition takes place at runtime. This process is known as dynamic method dispatch or late binding.
What is the use of abstract in java?
The abstract keyword is used to create abstract classes and abstract methods.
Can an abstract class be instantiated?
No. As an abstract class is not complete, it cannot be instantiated.
What happens when a class is declared as final?
When a class is declared as final it can no longer be extended (it cannot be a parent class).
What happens when a method is declared as final?
When a method is declared as final it cannot be overriden.
Base class for all the classes in java?
Base class or super class for all classes in Java is Object.
Interfaces can be extended. True or false?
Yes. Interfaces can be extended.
What is a package?
A package is a group of similar classes. A package can be created using the package keyword.
How to import a class in a package?
We can import a package using the import keyword. To import all the classes in a package named mypackage, we will write import mypackage.*.
How to declare a package?
We can declare a package using the keyword package. Consider this example:
package mypack;
In the above example mypack is the name of our package.
What is an Exception?
An exception is a run-time error. Handling exceptions is known as exception handling.
Difference between exception and error in java?
Run-time anomalies caused by instructions in the program are exceptions and run-time anomalies mostly caused due to operating system and hardware are termed as errors.
How does java handle exceptions?
Java provides try, catch, throw, throws, and finally for handling exceptions.
Is a catch block mandatory after a try block?
Yes. A try block must be followed by a catch block or finally block.
Is a finally block guaranteed to execute always?
In generally finally block is guaranteed to execute always. But if we use Jump statements like return etc., finally block might not get executed.
Difference between checked exceptions and unchecked exceptions in java?
Checked exceptions must be handled by the programmers. Handling unchecked exceptions is not mandatory. If an unchecked exception is not handled by the programmers, Java run-time will handle it automatically.
Use of throws in Java?
The throws keyword is used to throw one or more exceptions to the calling method or to the Java's run-time system.
What happens when there are multiple catch blocks?
When there are multiple catch blocks, the exceptions are matched against the catch blocks in the order they are defined.
What happens when there are nested try blocks?
When there are nested try blocks, if the exception raised in the inner try block is not handled, it will be checked against the catch blocks of the outer try block. If still not handled, it is handled by the Java's run-time system.
How to create own exceptions?
To create own exceptions, a new class should be created and it should extend the pre-defined class Exception. The message to be displayed when the exception is handled can be provided by overriding the method getMessage().
What is the use of multithreading?
Multithreading increases the efficiency of CPU. If one thread is suspended, another thread can be executed by the CPU. Also throughput might also increase if there are multiple processors.
What are the states in the thread life cycle?
The sates in the thread life cycle are: New, Runnable, Terminated, Waiting, and Blocked.
How to create threads in Java?
Threads can be created in two ways: 1) By extending Thread class or 2) By implementing Runnable interface.
How to synchronize thread access on shared resources?
Multiple threads accessing a shared resource can be synchronized to avoid race conditions by using either synchronized blocks or using synchronized methods.
What are daemon threads?
Threads which run in the background are known as daemon threads. An example for daemon thread is Java's garbage collector. An user thread can be converted to a daemon thread by invoking setDaemon() method.
How to manage multiple threads at a time?
Multiple threads can be created by instantiating Thread class multiple times. Those multiple threads can be controlled by using cooperation synchronization and by using thread life cycle methods.
What is an applet?
An applet is a small Java program which is embedded in a webpage and is automatically downloaded when the user requests the webpage.
Differences between standalone java program and an applet?
There are many differences between a Java program and an applet. Some of them are: a Java program requires main(), whereas an applet doesn't require main(). Starting point of execution for a Java program is main(), whereas for an applet is init().
Types of applets?
There are two types of applets: 1) Signed applets and 2) Unsigned applets. Signed applets are trusted applets which digitally signed by a third party. Unsigned applets are not signed by anyone. So they are unsecure.
How java provides security?
Java provides security through applets. An applet runs inside a sandbox in the browser's memory and prevents from accessing the local resources like memory, files etc.
How to create applets in java?
Applet can be created either by extending Applet class or by extending JApplet class.
What are the applet life cycle methods?
Applet life cycle methods are init(), start(), stop() and destroy(). The init() and destroy() methods execute only once in the applet life cycle. Other methods can execute multiple times.
What is an event?
Event is an object which is generated by an event source. Examples of events are key press, mouse click etc.
What is an event source?
Event source is an object which generates events. Examples of event sources are button, checkbox, textfield etc.
What is an event listener?
Event listener is an object which listens to events that are raised on event sources. An event listener should be attached to an event source to take appropriate action when an event occurs.
What are adapter classes?
To eliminate the need to implement all the methods in a listener interface, Java developers developed a set of classes called adapter classes which provide default implementations for all the methods in a listener interface.
What are inner classes?
Non static nested classes are known as inner classes.
What are anonymous classes?
Nameless nested class is known as anonymous class. Main use of anonymous classes is to handle events in GUI programs.
What is the motto of java?
Motto of Java is WORA (Write Once Run Anywhere).
What are the difference between AWT and Swing?
AWT components are heavy weight, whereas, Swing components are light weight. AWT components have different look and feel in different platforms, whereas Swing components have same look and feel in all platforms.AWT is the base GUI package, whereas Swing is developed on to off AWT.
Differences between interface and abstract class?
Some of the differences are: interface does not contain variables, whereas, an abstract class can contain variables. Interface contains only abstract methods, whereas, abstract class can contain both abstract and non-abstract methods. Interface does not have a constructor, whereas, an abstract class can have a constructor.
What if the main method is declared as private?
The program will compile but it will not run as JVM will not be able to access main method as it is declared as private.
Can a class contain multiple main methods?
Yes. We can have multiple main methods by overloading it. But, the entry point for any Java program is the main() method with String array as argument. Other versions of main cannot be called automatically.
Can I import same package/class twice? Will the JVM load the package twice at runtime?
Yes. Same package or class can be imported multiple times. But JVM loads a package or class only once at runtime.
Does importing a package imports the sub packages as well?
No. Importing the parent package does not mean that sub packages are also imported.
What is the default value of an object reference declared as an instance variable?
The default value for a object reference will be NULL.
Objects are passed by value or by reference?
In Java everything is passed by value. When an object is passed as a parameter, a copy of the object reference is passed which is pass by value.
Does Java provide any construct to find out the size of an object?
There is no direct support in Java to find the size of an object.
If my class already extends from some other class what should I do if I want an instance of my class to be thrown as an exception object?
Using multi level inheritance this can be done. The parent class can extend the Exception class and it can be extended by the sub class.
Can there be an abstract class with no abstract methods in it?
Yes. We can create an abstract class with no abstract methods. But, it is meaningless.
Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.
He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.
He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.
Well, your blog is quite interesting and helpful. I just loved reading your blog. Thanks a lot for sharing the top java interview question here, i have found your article very good and useful as i have an interview as java developer and was looking for some java questions to prepare for.