Java Interview Questions and Answers
1. What is the most important feature of Java?
Java is a platform independent language.
2. What do you mean by platform independence?
Platform independence means that we can write and compile the java code in one platform (eg Windows) and can execute the class in any other supported platform eg (Linux,Solaris,etc).
3. What is a JVM?
JVM is Java Virtual Machine which is a run time environment for the compiled java class files.
4. Are JVM’s platform independent?
JVM’s are not platform independent. JVM’s are platform specific run time implementation provided by the vendor.
5. What is the difference between a JDK and a JVM?
JDK is Java Development Kit which is for development purpose and it includes execution environment also. But JVM is purely a run time environment and hence you will not be able to compile your source files using a JVM.
6. What is a pointer and does Java support pointers?
Pointer is a reference handle to a memory location. Improper handling of pointers leads to memory leaks and reliability issues hence Java doesn’t support the usage of pointers.
7. What is the difference between final, finally and finalize() in Java?
final – A final variable acts as a constant, a final class is immutable and a final method cannot be ovrriden while doing inheritance.
finally – handles exception. The finally block is optional and provides a mechanism to clean up regardless of what happens within the try block (except System.exit(0) call). Use the finally block to close files or to release other system resources like database connections, statements etc.
finalize() – method belongs to Object class. The method that is invoked while doing the garbage collection of the object. It could be used for allowing it to clean up its state. Good use cases will be to free connection pools , de allocate resources etc.
8. What is difference between Hash Map and Hash Table ?
Both collections implements Map. Both collections store value as key-value pairs. The key differences between the two are
Hash map is not synchronized in nature but hash table is.
Another difference is that iterator in the Hash Map is fail-safe while the enumerator for the Hash table isn’t.
Fail-safe -if the Hash table is structurally modified at any time after the iterator is created, in any way except through the iterator’s own remove method, the iterator will throw a Concurrent Modification Exception?
Hash Map permits null values and only one null key, while Hash table doesn’t allow key or value as null.
9. What is difference between abstract class and interface ?
A class is called abstract when it contains at least one abstract method. It can also contain n numbers of concrete method.Interface can contain only abstract( non implemented) methods.
The abstract class can have public,private,protect or default variables and also constants. In interface the variable is by default public final. In nutshell the interface does not have any variables it only has constants.
A class can extend only one abstract class but a class can implement multiple interfaces.
If an interface is implemented its compulsory to implement all of its methods but if an abstract class is extended its not compulsory to implement all methods.
The issue with an interface is, if you want to add a new feature (method) in its contract, then you MUST implement those method in all of the classes which implement that interface. However, in the case of an abstract class, the method can be simply implemented in the abstract class and the same can be called by its subclass.
10. What is the difference between equals() and == ?
== operator is used to compare the references of the objects.
public bollean equals(Object o) is the method provided by the Object class. The default implementation uses == operator to compare two objects. But since the method can be overriden like for String class. equals() method can be used to compare the values of two objects.
11. Can you override a private or static method in Java?
you can not override a private or static method in Java, if you create a similar method with same return type and same method arguments in child class then it will hide the super class method, this is known as method hiding. Similarly, you cannot override a private method in sub class because it’s not accessible there, what you do is create another private method with the same name in the child class.
12. Does Java support multiple inheritances?
Java does support multiple inheritances of Type by allowing an interface to extend other interfaces, what Java doesn’t support is multiple inheritances of implementation.
Core Java interview questions:
What are the important features of Java 8 release?
What do you mean by platform independence of Java?
What is JVM and is it platform independent?
What is the difference between JDK and JVM?
What is the difference between JVM and JRE?
Which class is the superclass of all classes?
Why Java doesn’t support multiple inheritance?
Why Java is not pure Object Oriented language?
What is difference between path and classpath variables?
What is the importance of main method in Java?
What is overloading and overriding in java?
Can we overload main method?
Can we have multiple public classes in a java source file?
What is Java Package and which package is imported by default?
What are access modifiers?
What is final keyword?
What is static keyword?
What is finally and finalize in java?
Can we declare a class as static?
What is static import?
What is try-with-resources in java?
What is multi-catch block in java?
What is static block?
What is an interface?
What is an abstract class?
What is the difference between abstract class and interface?
Can an interface implement or extend another interface?
What is Marker interface?
What are Wrapper classes?
What is Enum in Java?
What is Java Annotations?
What is Java Reflection API? Why it’s so important to have?
What is composition in java?
What is the benefit of Composition over Inheritance?
How to sort a collection of custom Objects in Java?
What is inner class in java?
What is anonymous inner class?
What is Class loader in Java?
What are different types of class loaders?
What is ternary operator in java?
What does super keyword do?
What is break and continue statement?
What is this keyword?
What is default constructor?
Can we have try without catch block?
What is Garbage Collection?
What is Serialization and Deserialization?
How to run a JAR file through command prompt?
What is the use of System class?
What is instance of keyword?
Can we use String with switch case?
Java is Pass by Value or Pass by Reference?
What is difference between Heap and Stack Memory?
Java Compiler is stored in JDK, JRE or JVM?
What will be the output of following programs?