JAVA Interview Questions


Define constructor in Java?

Constructor is a special type of method used to create a new object in a program. It has the same name as that of the class. Whenever an object is created using a new keyword, the class used is the default. If one constructor is created by the user with parameters, then other constructors would be without parameter. There may be a possibility of overloading the constructor. 

Name the different types of memory areas allocated by JVM?

There are numerous memory areas located by Java Virtual Machine:

  • Stack: It plays a role in method invocation and returns. Frames are stored in Java Stack. It holds local variables and partial results, every time a new frame is created, and there is a particular JVM stack for each thread. 
  • Heap: It is nothing but a runtime data area. In this area, memory is allocated. 
  • Class Area:  Class Area stores per-class structures such as the code for methods.
  • Program Counter Register: It mainly contains the address of JVM instruction that is currently in use. 
  • Native Method Stack: All the native methods used are present here.

Define Packages in Java? Enlist its benefits?

Classes and Interfaces collection together is known as packages. So, the developer can easily modularize the code and reuse it.  

The Key Benefits of packages in Java are:

  • Packages prevent same name occurrence 
  • Easier access control provided by packages
  • Hidden classes are used by packages that are invisible outside.
  • There is proper hierarchical structure creation, which simplifies the location of related classes. 

What is public static void main(String args[])?

The starting point of any Java program is main(). This is always written as:

  • Public: Access modifier, determine who can access this method. However, its public that means it can be available for any class. 
  • Static: Java Keyword used for the identity of class-based. It is a keyword in Java that identifies it is class-based. main() is made static in Java 
  • void: Return type method. 
  • main: Method name searched by JVM as an initiation point for application with a specific signature
  • String args[]: Parameter passed to the main method.

Define the BREAK statement and singleton class in Java?

BREAK statement The flow sequence in Java is broken using the BREAK statement. It is generally used with a switch case data structure so as to exit the statement once a case is executed. However, It can be used to exist Java loop.

Singleton class For this class, only a single instance can be created in one JVM at any given time. If you want to make a class singleton, you have to make its constructor private.