In this post, you will learn about the Java Basic Interview Questions.
1) What is Java ?
Java is a programming language developed by Sun Microsystems in . The Java programming language was developed by a small team of engineers, known as the Green Team, who initiated the language in . The language was originally called OAK. Java is fast, secure, and reliable. From laptops to data centers, game consoles to scientific supercomputers, cell phones to the Internet, Java is everywhere!
2) What is the most important feature of Java ?
Java is a platform independent language.
3) What do you mean by platform independent ?
Platform independent means that we can write and compile java code in one platform ex. Windows and can execute the code in any other supported platform ex. Linux, Solaris etc.
4) What is the difference between JDK and JRE ?
JDK stands for Java Development Kit. It contains the tools and libraries for development of Java programs. It also contains compilers and debuggers needed to compile Java program. JRE stands for Java Runtime Environment. This is included in JDK. JRE provides libraries and JVM that is required to run a Java program.
5) What is JVM ?
JVM is Java Virtual Machine, an abstract computing machine that enables a computer to run a Java program. Java Virtual Machine executes Java Bytecode. There are different JVM’s for different hardware and software platforms. So JVM is platform dependent. JVM is responsible for loading, verifying and executing the Bytecode on a platform.
6) 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.
7) What are the different types of memory areas allocated by JVM ?
In java, JVM allocates memory to different processes, methods and objects. Some of the memory areas allocated by JVM are:
. ClassLoader: Classloader is a subsystem of JVM that is used to load class files.
. Class (MethodArea: It stores per-class structures such as the runtime constant pool, field and method data, and the code for methods.
. Heap: Heap is a data area from which memory for all class instances and arrays are allocated. It is created at the JVM start-up. Heap is the storage for Java objects.
. 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: It 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.
. Execution Engine: It contains
a. A virtual processor
b. Interpreter: It reads byte code stream and executes lines of byte code as commands to be executed.
c. Just-In-Time(JITcompiler: It is used to improve the performance.
8) What is JIT compiler ?
Just In Time compiler also known as JIT compiler is used for performance improvement in Java. It is enabled by default. JIT compiles parts of the byte code that have similar functionality at the same time and hence reduces the amount of time needed for compilation. Here the term compiler refers to a translator from the instruction set of a Java virtual machine (JVMto the instruction set of a specific CPU.
9) What is WORA (Write once and run anywhere ?
WORA means Java can be developed on any device, compiled into a standard byte code and be expected to run on any device equipped with a Java virtual machine (JVM). For ex. The class and jar files that you create in Windows
platform can run as it is on Unix environment. So Java is truly platform independent language.
10) How does a ClassLoader work in Java ?
The Java Classloader is a part of the Java Runtime Environment that dynamically loads Java classes into the Java Virtual Machine. ClassLoader loads files from the physical file locations ex. File system, Network location etc.
Classloaders are classified into the following:
. Bootstrap ClassLoader: The bootstrap class loader loads the core Java libraries located in the $JAVA_HOME/jre/lib directory. This class loader, which is part of the core JVM, is written in native code.
. Extension ClassLoader: The extensions class loader loads the code from the extensions directories $JAVA_HOME/jre/lib/ext, or any other directory specified by the java.ext.dirs system property).
. System ClassLoader: This ClassLoader loads classes from the CLASSPATH. If you specify your jars in CLASSPATH, then this ClassLoader will load them.
11) What is an Object in Java ?
Object is a runtime entity having state and behavior. It’s state is stored in fields and behavior is shown via methods.
12) What is a Class ?
A class is a blue print from which individual objects are created. A class can contain fields and methods to describe the behavior of an object.
13) Which class is the super class of all classes ?
java.lang.Object is the super class for all the java classes.
14) Do we need to declare main() method in all java classes ?
No, we don’t to declare main() method in all java classes. The main(method should be defined only if the source class is a Java application.
15) What is the return type of the main() method ?
Main() method doesn’t return anything hence declared as void.