Java Interview Questions


Q#1. What is JAVA?

Ans. Java is a high-level programming language and is platform independent. Java is a collection of objects. It was developed by Sun Microsystems. There are a lot of applications, websites and Games that are developed using Java.


Q#2. What do you understand by Java virtual machine?

Ans. Java Virtual Machine is a virtual machine that enables the computer to run the Java program. JVM acts like a run-time engine which calls the main method present in the Java code. JVM is the specification which must be implemented in the computer system. The Java code is compiled by JVM to be a Bytecode which is machine independent and close to the native code.


Q#3. What are wrapper classes in Java?

Ans. Wrapper classes convert the Java primitives into the reference types (objects). Every primitive data type has a class dedicated to it. These are known as wrapper classes because they “wrap” the primitive data type into an object of that class. Refer to the below image which displays different primitive type, wrapper class and constructor argument.


Q#4. How does Java enable high performance?

Ans. Java uses Just In Time compiler to enable high performance. JIT is used to convert the instructions into bytecodes.


Q#5. How many types of memory areas are allocated by JVM?

Ans. A platform is the hardware or software environment in which a piece of software is executed. There are two types of platforms, software-based and hardware-based. Java provides the software-based platform.


Q#6. What are constructors in Java?

Ans. In Java, constructor refers to a block of code which is used to initialize an object. It must have the same name as that of the class. Also, it has no return type and it is automatically called when an object is created. There are two types of constructors: Default Constructor: In Java, a default constructor is the one which does not take any inputs. In other words, default constructors are the no argument constructors which will be created by default in case you no other constructor is defined by the user. Its main purpose is to initialize the instance variables with the default values. Also, it is majorly used for object creation. Parameterized Constructor: The parameterized constructor in Java, is the constructor which is capable of initializing the instance variables with the provided values. In other words, the constructors which take the arguments are called parameterized constructors.


Q#7. What is meant by Local variable and Instance variable?

Ans. Local variables are defined in the method and scope of the variables that have existed inside the method itself. An instance variable is defined inside the class and outside the method and scope of the variables exist throughout the class.


Q#8. What are the main differences between the Java platform and other platforms?

Ans. There are the following differences between the Java platform and other platforms. Java is the software-based platform whereas other platforms may be the hardware platforms or software-based platforms. Java is executed on the top of other hardware platforms whereas other platforms can only have the hardware components.


Q#9. Why pointers are not used in Java?

Ans. Java doesn’t use pointers because they are unsafe and increases the complexity of the program. Since, Java is known for its simplicity of code, adding the concept of pointers will be contradicting. Moreover, since JVM is responsible for implicit memory allocation, thus in order to avoid direct access to memory by the user, pointers are discouraged in Java.


Q#10. What gives Java its 'write once and run anywhere' nature?

Ans. The bytecode. Java compiler converts the Java programs into the class file (Byte Code) which is the intermediate language between source code and machine code. This bytecode is not platform specific and can be executed on any computer.