Java Interview Question and Answers

 

What is Java Programming Language?

Java is an Object-oriented, High-level and Secured Programming Language. Java is platform independent and portable programming language which means it can easily run in any computer.

 

What is JDK, JRE, and JVM in Java?

JDK: JDK stands for Java Development Kit. It is a software IDE ( Integrated Development Environment) which is used to develop software applications and Web applications using Java.

JRE: JRE stands for Java Runtime Environment. It contains a set of libraries and many other files use at runtime. It consists of a Set of software tools for developing Java applications.

JVM: JVM Stands for Java Virtual Machine. JVM allows executing byte code. JVM is available for many software and hardware platform.  JVM is platform dependent.

 

What is a platform?

In computer field platform means software based and hardware based platform. A platform is a hardware or software environment in which software can be executed.

 

Explain about public static void main in Java

  • The public is an access modifier. The main purpose of declaring the main method has the public to access this method by any Class.
  • Static is a keyword which is declared to access the main method without creating the instance of a Class.
  • Void: It is a return type for the main method. It means the main method doesn’t return any value.
  • Main:  It is a method. It is the starting point for the application.

 

What are access specifiers in Java?

Access specifiers are used to defining the access scope of the method, class or a variable in Java. Java supports four types of access specifiers namely public, private, default and protected.

 

  • Public: If the class, method or variable is defined as a public means they can be accessed by any class or method.
  • Private: If a class or method is private means it can be accessed within the same class.
  • Default: By default, all classes, methods, and variables are default which means they can be accessed outside the class and within the package only. Default classes or methods cannot be accessed by other package methods or classes.
  • Protected: Protected variables, methods, and constructors can be accessed within the class in the same package and the subclasses in other packages. The protected modifier cannot be used for class and interfaces.

 

What are the packages and advantages of packages in Java?

They prevent naming conflicts. They are used to group related classes into a single package, Just like a file directory or folder. Packages are again divided into two categories 1. Built-in Packages 2. User-defined Packages

  • Built-in Packages are prewritten classes. They are ready to use in Java application.
  • User-defined Packages are to create our own package in Java for further usage.

 

What is a Constructor in Java?

Constructors are used to initializing the state of the object. They are used to assign values to class variables at the time of creating an object.  Constructors are invoked when the class is instantiated. There are two types of constructors.  1. Default Constructor  2. Parameterized Constructor.

 

Default constructors do not accept any value whereas Parameterized constructors accept arguments. The main purpose of default constructor is to assign default values to the objects at the time of creation. Constructors can be overloads like other methods in Java.

 

What are the differences between constructor and methods?

  • Constructors are used to initializing the state of an object whereas method is used the tell the behavior of an object.
  • Constructors are invoked implicitly whereas method is invoked explicitly.
  • The name of the constructor and the name of the class must be the same. But the name of the methods need not be same as class name.
  • Constructors must not have a return type whereas a method must have a return type.

  Click here to learn more about Java