Be it a fresher or experienced java professional one would often encounter core java related questions in interviews.
This article is a compilation of important core java interview questions and answers. It aims at helping you in preparing for any Java/J2EE based interviews.
Is Java platform independent?
Yes, Java is platform independent because it enables programs written in one platform to be able to run on other platforms. i.e. Code written on a machine with Windows OS can run on a machine with Mac OS.
How does Java achieve platform independence?
Java achieves platform independence using something known as byte codes. Upon compiling any java file the compiler would generate its corresponding byte code file.
Java Virtual Machine (JVM) of any platform can interpret these byte code files, hence making Java platform independent.
What is the difference between JVM, JRE, and JDK?
JVM stands for Java Virtual Machine
It is a virtual machine that runs the compiled Java byte codes
JRE stands for Java Runtime Environment
It provides libraries and other components (needed to run applications) to the JVM
JDK stands for Java Development Kit
It is the complete java software development kit
It contains everything in JRE along with tools such as compilers and debuggers.
Is Java Pass by Value of Pass by Reference?
Java is always Pass by Value. Furthermore, a method can either pass a copy of primitive value (e.g., int, float) or a copy of object reference.
In case of primitive value, changes made to the received copy would not affect the original value. This is because they are separate entities. In the case of object reference, copied reference points to the same object. Yet, modifications to the received copy would not affect the original value. Let’s look at the example below to understand more.
Which class is the superclass of all classes in Java?
The java.lang.Object class is the superclass of all the classes in java. It’s not necessary to explicitly extend it.
What is a java package? Which packages are implicitly imported?
Java package groups related files together and hence resolves any name conflicts. The package keyword creates a package, the java.lang package is implicitly imported.
Why java doesn’t support multiple inheritance?
Java doesn’t support multiple inheritance because it leads to “Diamond Problem”.
To understand it lets consider there is an Interface named Animal with a method named eat. Classes Cat and Dog implements the Animal interface and in turn the eat method.
Say, Java allowed multiple inheritance and a class named Pet extends classes Cat and Dog. Now trying to execute the eat method from the Pet class leads to ambiguity. This is because the compiler doesn’t know which eat method to execute (Cat and Dog?)
It's named “Diamond Problem” since the class hierarchy depicts a diamond.