Java Interview Questions and Answers

Java Interview Questions and Answers

 

List of basic java interview questions and answers. interview questions on core java, advance java will be discussed here.

Java is one of the highly used programming in The World. If you are searching for the Java Interview Questions and Answers, then you visited the right place. More than 1 billion devices in the World runs on Java. So its most popular language used in the World.

There are lots of opening in Java technologies around the World. To crack the Java interviews, job seeker needs Java Interview questions and answers,so that they can crack Java interviews. Here we will discuss Interview questions on Java on various topics.

 

 

What is Java?

Java is one of the widely used programming language in the World. Java is object oriented programming language. Java programming language is one of the most famous language in the World. Java is Owned by Oracle.


Who Developed the Java Programming language?

Java was developed by James Gosling in 1991.


What are the feature of Java?

By asking the feature of Java, Interviewer wants to know whether you about Java or not. Let’s see some feature of object oriented language Java.

  • Object oriented language
  • Platform Independent
  • Multi threading
  • Simple
  • Secure

What is classpath in Java?

classpath in Java is the path or location where Java is installed in the System. When we install Java in the system we have to set the path variables. Java path variable is kind of indication to operating system that Java is installed on some specific location.

JAVA_HOME and path are two variables which needs to be set to run Java program.

 

How to save Java program file?

Java program file is save with .java file extension. After you done the java coding, file will be saved as .java extension.

filename.java

 

How to comment Java code?

There are two ways to comment Java code.

  • Single Line comment in Java: Java code can be comment by using //. Anything written after // will be treated as comment.
  • Multi-line comment in Java: Multiple lines can be commented in Java by using /* */ tag. Anything written in between /* */ tag will be treated as comment in Java.
public class JavaExamples {

//Single Line and mult-line comment in Java by byteArray.in
public static void main(String[] args) {
System.out.println("Hello ByteArray.in");
//This is single line comment
System.out.println("How Are You !!");
/*
This is Multiline comment
in Java. Anything written in between
will not be compile.
*/
}
}

output

Hello ByteArray.in
How Are You !!

 

What is the parent class of all classes in Java?

Object class is the parent class of all classes in Java. Object class has various inbuilt methods.

 

What is the parent package of all classes in Java?

Java lang package is the parent package of all classes in Java.

 

what are the access modifiers in Java?

Access Modifier specify the scope of the method, class, constructor, variables on which they are applied.

There are basically four types of Access modifier in Java

  1. private
  2. public
  3. protected
  4. default

 

What is default access modifier in Java?

If no access modifier is specified then default modifier is by default access modifier in Java. The scope of the default access modifier is in the package on which it is specified.

 

What is class in Java?

Class in Java is the template to create an object. In simple terms, It provide the structure so that objects can be created.

 

What is method in Java?

method is a group of statements or line of statements to perform some operation. Method in Java execute the java code written inside when it is called.

 

What is the entry point of a program in Java?

Main() method is the entry point to execute the java program.

System.out.println(String [] arg)

 

What is method overloading in Java?

Method overloading in Java is the process of overloading the method. In Java method overloading, one method is overloaded by passing different argument in the same method, but they have same return type.

In method overloading two or more methods having same name and same return type but difference is only of parameters provided.

public double area(double r);

public double area(int a, int b);

Above area method is same, In first method only 1 argument is provided. In second, two argument is provided. So, this is method overloading in Java.

 

What is method overriding in Java?

Method overriding in Java is the way in which child class method override the method written in Parent class. Method overriding concept is used in Java inheritance where Parent-child concept is there. In method overriding child class method gets higher priority to execute than the parent class method.

In Java Method overriding, both parent and child class has same method with exact same everything, but child class method is called.

 

What is the use of a constructor in Java?

Constructor are used to create the objects in Java class. There are default constructor and parameterised constructor in Java.

 

What is the use of a new keyword in Java?

Use of new keyword is to create the object of the class. new keyword is used with constructor to create the object.

Person p = new Person();

Here we created the object of class Person with the new keyword.

 

What is the use of static keyword in Java?

static is the keyword in Java. The purpose of the static keyword is mainly in memory management. static keyword is used with variable, class, method,  static block in Java.

When static keyword is applied to variable it become class level variable. static variables can be access anywhere in the class.

class can be created inside class its called inner class. inner classes can be created with the help of static keyword.

 

What is variable in Java?

Variable in Java is the placeholder to store some value for a temporarily. different types of values can be stored in variable. different types like integer, float, decimal, String, Array list, map etc values can be stored in variable.

Read more on Java Variable.

int age = 20;

String name=”Allen”;

Here age is integer variable holding numeric value and name is String variable holding String values.

 

Can main method be private in Java?

No, main method can not be private in Java.

We can declare main method as private, it will compile successfully, but when it was executed at runtime, it throws exception.

 Main method not found in class , please define the main method as: 

public static void main(String[] args)

 

Can main method be overloaded in Java?

Yes, we can overload main method in Java.

 

Can main method be overridden in Java?

No main method can not be overridden in Java.

 

Can main method return any value in Java?

No, main method does not return any value, as its return type is void. if you will try to change its return type other than void, it will throw error.

 

Can main method be final in Java?

Yes, we can make main method as final in Java.

public static final void main(String[] args)

 

can main method be non static?

No, main method in Java can not be non static. if declared, it will throw error.

 

Why main method takes only string arguments in Java?

Its the standard format of Java main method that it will take only String array argument. if we try to put other than String argument, It will throw error.

The reason of taking only String arguments other than others data type could be that String is very versatile datatype. Anything can be converted from String.

 

What is Strings in Java?

Strings are class in Java. we can create object of String class. Strings in Java is immutable, once created Strings ca not be modified. String is also a data type in Java.

 

Can we declare class as final in Java?

Yes, a class can be declared as final in Java.final class can not be inherited. it means if you make any class as final then that class can not be sub classed.