1. On which version of Java you work. Do you know, what new features are implemented in latest version?By asking this type of question, the interviewer wants to know whether you are updating yourself or not with the latest technologies. Java 8 has been one of the biggest release after Java 5 annotations and generics. Some of the important features of Java 8 are:
- Interface changes with default and static methods
- Functional interfaces and Lambda Expressions
- Java Stream API for collection classes
- Java Date Time API
2. Is Java Platform-Independent?This type of question is generally asked from a fresher candidate. Platform independence means that you can run the same Java Program in any Operating System. For example, you can write java program in Windows and run it in Mac OS.
As, Java compiler compiles the source code into bytecode and then bytecode is interpreted by JVM. As we can move bytecode from one OS to another OS, so Java in platform- independent. Only, we require is JRE for the specified OS.
3. What is JVM and is it platform independent?Java Virtual Machine (JVM) is the heart of java programming language. JVM is responsible for converting byte code into machine readable code. So, JVM is not platform independent, and we need different JVM for different operating systems. We can customize JVM with Java Options, such as allocating minimum and maximum memory to JVM.
4. What is the difference between JDK and JVM?Java Development Kit (JDK) is used for development of Java applications while JVM is a part of it for executing the Java programs.
JDK provides all the tools, executables and binaries required to compile, debug and execute a Java Program. The execution part is handled by JVM.
5. What is the difference between JVM and JRE?Java Runtime Environment (JRE) is the implementation of JVM. JRE consists of JVM and java binaries and other classes to execute any program successfully. JRE doesn’t contain any development tools like java compiler, debugger etc. If you want to execute any java program, you should have JRE installed.
6. What is Package in Java?Package in Java is the way to organize the Java classes by grouping them. The grouping can be based on functionality or modules based. The name of the fully classified Java class contains package and class name. For example, java.lang.Object is the fully classified name of Object class that is part of java.lang package.
java.lang package is imported by default and we don’t need to import any class from this package explicitly.
7. If Parent class is Serializable, then Child class will be Serializable class?The child class would be Serializable because there is (IS-A) relationship between child class and parent class, and hence child will also be serializable too.
However, if we talk about the object of child class then it may or may be serializable, as it is possible for a subclass to provide readObject() and writeObject() methods, that throws a NotSerializableException.
8. Can we overload main method?Yes, we can have multiple methods with name “main” in a single class. However if we run the class, java runtime environment will look for main method with syntax as public static void main(String args).
9. What is overloading and overriding in java?When we have more than one method with same name in a single class but the arguments are different, then it is called method overloading.
Overriding concept comes in picture with inheritance when we have two methods with same signature, one in parent class and another in child class. We can use @Override annotation in the child class overridden method to make sure if parent class method is changed, so as child class.
10. What is soft reference, weak reference, phantom reference?A weak reference, is a reference that isn't strong enough to force an object to remain in memory. Weak references allow you to leverage the garbage collector's ability to determine reachability for you, so you don't have to do it yourself. It is used in java.util.WeakHashMap.
A soft reference is exactly like a weak reference, except that it is less eager to throw away the object to which it refers. An object which is only weakly reachable will be discarded at the next garbage collection cycle, but an object which is softly reachable will generally stick around for a while.
A phantom reference is quite different than either SoftReference or WeakReference. The object is marked for Garbage collection but it is finalized and have not yet reclaimed. The object is called as phantom reachable. The grip on the object is so tenuous that you can't even retrieve the object -- its get() method always returns null. The only use for such a reference is keeping track of when it gets enqueued into a ReferenceQueue, as at that point you know the object to which it pointed is dead.