Java interview questions

Java is an object oriented programming language; it runs on many operating systems and is actually one of the most popular programming languages. Since 2007; most of Java's technologies were released under GNU License, and over the years it has evolved very significantly. If you are an applicant or an interviewer and you wish to know which Java Interview Questions you can ask or answer then this section is perfect for you, take a look at this compilation and pick the questions you need.

Java interview questions and answers

Java interview questions

Is Java a full object oriented programming language?

The first answer that comes out is YES, and it's also correct, but you should know that the primitive variables are not objects, anyway, Java can be considered as a non full-object oriented programming language.

What is the difference between initialization and assignment?

Initialization is done once, assignment can be done as many times as you want.

Which are the primitive data types on Java?

They are eight, byte, short, int, long, float, double, boolean, char.

What are global variables? and, Does Java support them?

Global variables are accessible from any part, and Java does not support them because: breaks the referential transparency and, creates collisions in namespaces.

How can you convert a string into int?

Besides the fact that it has to be a number, you can do it using this code:

String numString = "1000";
int id=Integer.valueOf(numString).intValue();

Explain the different types of access modifiers.

The access modifiers are four:

  • Public: Accessible from all classes.
  • Protected: Accessible from all the cases of the same package and any subclass.
  • Private: Accessible only from the class they belong.
  • Default: Accessible to the class they belong to and subclasses within the same package.

What's the difference between declaring and defining a variable?

Defining involves declaration and definition, example:

Declaring: string var1;
Defining: string var1 = "variable 1";

What is a variable?

A variable can be explained by different ways, but the most important is to understand that it is a named space of memory that we reserve to save different types of data, for example "int var1;" or "string var2;".

What's the function of the "trim()" method with a string variable?

It will eliminate whitespaces at the beginning and end of the string.

What is the use of the class "math"?

Math class is used to access mathematical functions.

What is the functionality of vectors?

Compared to arrays, vectors do not have a predefined size, they can resize and relocate automatically.

How do you make an array larger?

It is not possible, what you can do is create a new larger one and copy the data from the older, you can use System.arraycopy().

Explain what is and how "substring()" works

It creates a new string object out of another string, here you will see a very simple example of how substring works:

string var1 = "0123456789";
System.out.println(var1.substring(5)); // prints 56789
System.out.println(var1.substring(5,8)); // prints 567

Remember that you should focus on the requirements of the company, you can extract them from the advertisements they use, if they are looking for a Java programmer to do this and this, then you can focus on those requirements, if you do not have too much knowledge or experience on them you can take a while to prepare and do great during your interview, the same goes for interviewers, you should focus the interview and advertisements on your requirements, if you need a database expert then ask for a database expert, if you need someone who can develop a custom software on Java, then take a look at your requirements and use the proper Java interview questions to get what you need.

It is also important to prepare exercises, they will measure the creativity and abstraction level of the interviewee, here you will find some useful examples:

How can we know if a year is a leap year?

You can divide the year by 4, if there is no rest, then it is a leap year.

You can also use this code:

public static boolean isLeapYear(int year) {
    GregorianCalendar gc = new GregorianCalendar();
    return gc.isLeapYear(year);
}

How can you swap the values of two variables without using an auxiliar?

Here are two possible answers:

    int a=5;
    b=10;
    a=a+b;
    b=a-b;
    a=a-b;

Or.

    int a = 5; int b = 10;
    a = a ^ b;
    b = a ^ b;
    a = a ^ b;

Create an anagram checker?

An anagram checker would check if the letters in a string, are the same in another, doesn't matter the spaces and capital letters, for example:

    Evelen plus two AND Twelve plus one