Java Basic Interview Questions
Differentiate between JDK and JRE.
Explain Java Virtual Machine (JVM)?
What are the different types of memory areas allocated by JVM?
What is the JIT compiler?
How the Java platform is different from other platforms?
Why Java is called as ‘write once and run anywhere’ language?
Explain the working of ClassLoader in Java?
Do you think ‘main’ used for the main method is a keyword in Java?
Can you write the main method as public void static instead of the public static void?
In Java, if we do not specify any value for local variables, then what will be the default value of the local variables?
Let say, we run a java class without passing any arguments. What will be the value of the String array of arguments in the Main method?
Differentiate between byte and char data types in Java.
Explain the main principles of Object-Oriented Programming?
Differentiate between Object Oriented Programming language and Object-Based Programming language.
In Java what is the default value of an object reference defined as an instance variable in an Object?
Need for constructor in Java?
Why do we need a default constructor in Java classes?
What is the value returned by Constructor in Java?
Can we inherit a Constructor?
Why constructors cannot be final, static, or abstract in Java?
1- JDK contains all the tools and libraries required for the development of Java programs. It also contains compilers and debuggers required to compile a Java program. JDK is also considered a superset of JRE. JRE is included in JDK. JRE provides libraries and JVM.
2- JVM is an abstract tool that executes Java Bytecode. There are different JVMs for different hardware and software platforms. Hence, it is platform-dependent. JVM is responsible for loading, verifying, and executing the Bytecode on a specific platform. JVM is included in a JRE package. The hotSpot is one of the most popular JVMs developed by Oracle.
- ClassLoader: It is a component of JVM used to load class files.
- Class (Method) Area: It stores per-class structures such as the runtime constant pool, field and method data, and the code for methods.
- Heap: Heap is created on runtime and it contains the runtime data area in which objects are allocated.
- Stack: Stack stores local variables and partial results at runtime. It also helps in method invocation and return value. Each thread creates a private JVM stack at the time of thread creation.
- Program Counter Register: This memory area contains the address of the Java virtual machine instruction that is currently being executed.
- Native Method Stack: This area is reserved for all the native methods used in the application.
4- Just In Time compiler known as JIT compiler, used for performance improvement. It is enabled by default. It is compilation done at execution time. JIT compiler is including in JVM.
5- Java compiler converts java code into byte code that is interpreted by JVM. Java byte code can run on any supported platform.
6- Byte code generated by Java compiler can be interpreted by any JVM. So it becomes much easier to write programs in Java and expect those to run on any platform.
7- In Java, ClassLoader is a class that is used to load files in JVM. ClassLoader loads files from their physical file locations e.g. Filesystem, Network location, etc.
There are three main types of ClassLoaders in Java.
- Bootstrap ClassLoader: This is the first ClassLoader. It loads classes from rt.jar file.
- Extension ClassLoader: It loads class files from jre/lib/ext location.
- Application ClassLoader: This ClassLoader depends on CLASSPATH to find the location of class files. If you specify your jars in CLASSPATH, then this ClassLoader will load them.
8- No, main is just a name of the method. There can be multiple methods with same name main in a class file. It is not a keyword in Java.
9- No, you cannot write it like this. Any method has to first specify the modifiers and then the return value. The order of modifiers can change. We can write static public void main() instead of public static void main().
10- Java does not initialize local variables with any default value. So these variables will be just null by default.
11- By default, the value of the String array of arguments is empty in Java. It is not null.
12- Both byte and char are numeric data types in Java. They are used to represent numbers in a specific range. Major difference between them is that a byte can store raw binary data whereas a char stores characters or text data.
The usage of char is for example. char ch = ‘x’;
Byte values range from -128 to 127.
A byte is made of 8 bits. But a char is made of 16 bits. So it is equivalent to 2 bytes.
13- Main principles of Object-Oriented Programming (OOPS) are:
16-Java is an object-oriented language, in which we create and use objects. A constructor is a piece of code similar to a method. It is used to create an object and set the initial state of the object. A constructor is a special function that has the same name as the class name. Without a constructor, there is no other way to create an object. By default, Java provides a default constructor for every object. If we overload a constructor then we have to implement the default constructor.
17- Default constructor is the no-argument constructor that is automatically generated by Java if no other constructor is defined. Java specification says that it will provide a default constructor if there is no overloaded constructor in a class. But it does not say anything about the scenario in which we write an overloaded constructor in a class. We need at least one constructor to create an object, that’s why Java provides a default constructor. When we have an overloaded constructor, then Java assumes that we want some custom treatment in our code. Due to which it does not provide a default constructor. But it needs a default constructor as per the specification. So it gives an error.
18- When we call a constructor in Java, it returns the object created by it.
19- Java does not support the inheritance of the constructor.
20- If we set a method as final it means we do not want any class to override it. But the constructor already cannot be overridden. So there is no sense in marking it final.
If we set a method as abstract it means that it has no body and it should be implemented in a child class. But the constructor is called implicitly when the new keyword is used. Therefore it needs a body.
If we set a method as static it means that it belongs to the class, but not a particular object. The constructor is always called to initialize an object. Therefore, there is no use of marking constructor static.