Javatpoint Logo
Javatpoint Logo

Array Interview Questions in Java for Freshers

In Java, Array is the most important data structure. It is also used by other data structures such as HashTable, List, etc. From the exams and interviews perspective, it is the most important topic of Java.

In this section, we have covered some important interview questions based on Java Array. These questions are usually asked in Java interviews. If you are a fresher, this section is for you. After reading this section, you will be able to answer the question related to Array without any hesitation.

1) On which memory arrays are created in Java?

Arrays are created on dynamic memory by JVM. There is no question of static memory in Java everything (variable, array, object, etc.) is created on dynamic memory only.

2) Can we call the main() method of a class from another class?

Yes! We can call the main() method of a class from another class using Classname.main(). At the time of calling the main() method, we should pass a string type array to it.

3) What is an array in Java?

An array is a finite and ordered collection of homogeneous data elements. It is finite because it contains a limited number of elements. It is ordered because all the elements are stored one by one in a contiguous location of computer memory (heap) in a linear fashion. It is homogeneous because all elements of an array are of the same data type only. We can store either primitive types or object references into it.

4) What are the types of an array?

Arrays are generally categorized into two parts as described below:

  • Single Dimensional Array
  • Multi-Dimensional Array (2D and 3D arrays)

5) Is it possible to declare array size as negative?

No, it is not possible to declare array size as negative. Still, if we declare the negative size, there will be no compile-time error. But we get the NegativeArraySizeException at run-time.

6) What is the difference between int array[] and int[] array?

There is no difference between array[] and []array. Both array[] and []array are the ways to declare an array. The only difference between them is that if we are declaring more than one array in a line, we should use prefix []. If we are declaring a single array in a line, we should use postfix []. For example, consider the following declaration:

7) How to copy an array in Java?

We can create a copy of an array in two ways, first one is manually by iterating over the array and the second one is by using the arrayCopy() method. Using the arrayCopy() method of the System class is the fastest way to copy an array and also allows us to copy a part of the array. These two methods are the popular ways to copy an array.

The other two methods to copy an array is to use the Arrays.copyOf() method and using clone() method.

8) What is the default value of the array?

When we create a new array, it always initialized with the default values. The default values of the array are:

  • If an array is of byte, short, int, and long type, the default value is 0.
  • If an array is of float and double type, the default value is 0.
  • If an array is of Boolean type, the default value is false.
  • If an array is of an Object type, the default value is null.

9) What do you understand by the jagged array?

A jagged array is a multidimensional array in which member arrays are of different sizes. For example, int array[][]=new int[3][]. The statement creates a two-dimensional jagged array.

10) What is an anonymous array also give an example?

Array reference that is not stored in a variable. It is used in the construction of other objects. Java's Polygon class has a constructor that parses anonymous array as a parameter.

For example:

The above statement creates a triangle that an anonymous array.

11) How many ways to find the duplicate elements in an array?

There are the following five ways to find the duplicate array in Java.

  • Brute Force Method: In this method, we compare each element of an array with the other elements. If any of the two elements are found equal, we consider them as duplicates. The method has time complexity O(n2).
  • Using HashSet: We can also use the HashSet class to find the duplicate elements in an array. To find the duplicate elements, iterate over the array elements and insert them into HashSet by invoking add() method of the HashSet class. If the method returns false it means that the element is already present in the Set. It takes O(n) time to find the duplicate elements.
  • Using HashMap: We know that HashMap uses key-value pair to store an element. When we use HashMap to find the duplicate array, we store the elements of the array as keys and the frequency of the elements as values. If the value of any key is greater than 1, the key is a duplicate element. Its time and space complexity is O(n). Using this method, we can also find the number of occurrences of duplicates.

12) Which operations can be performed on an array?

On an array, we can perform the searching, sorting, traversal, deletion, and insertion operation.

13) Consider the following statements and tell that the declaration is true or not. Also, specify the reason?

The declaration of the above array is true. Because Java allows us to use two different array-specific syntax shortcuts both to initialize (put explicit values into an array's elements) and construct (instantiate the array object itself) in a single statement. The first statement is used to declare, create and initialize in one statement. The second statement does the following four things:

  • Declares an int array reference variable named number.
  • Creates an int array with a length of five (five elements).
  • Populates the array's elements with the values 12, 34, 90, 56, and 65.
  • Assigns the new array object to the reference variable number.

14) Can a Set be an array?

In Java, a Set is an array, but an array is not necessarily a Set. Because repetition is allowed in array but in Set. For example, consider the following figure:

Array Interview Questions in Java

15) Is it possible to make an array volatile?

Yes, we can make an array volatile in Java. But we only make the variable that is pointing to array volatile. If an array is changed by replacing individual elements that happen before the guarantee provided by volatile variables will not hold.

16) What happens if we declare an array without assigning the size?

It is not possible to declare an array without size. When we declare an array without assigning the size, it throws the compile-time error. For example, height=new int[].

17) Can we declare array size as negative?

No, the array size cannot be negative. If we declare an array with a negative size, it throws NegativeArraySizeException at run time.

18) When ArrayIndexOutOfBoundsException occurs?

The ArrayIndexOutOfBoundsException occurs when the program tries to access the index of an array. The exception also occurs when the index is higher than the size of the array or the index is negative.

19) Which method of the Arrays class can be used to search a specific element in an array?

The binarySearch() method of the Arrays class is used to search a specific element in an array. The method uses the binary search algorithm. The array must be in natural ordering before making this call. It is the simplest and most efficient method to find an element in a sorted array.

20) How to retrieve the class name of an array?

An array is an object. From the object we can retrieve the class name. We invoke the getClass() and getName() method that retrieves the class name of an array. The getClass() is the method of the Object class that returns the runtime class of the object. While the getName() is the method of the Class class that returns the name of the class/array class.

21) What is the difference between Array and ArrayList?

Array: Array is static. It is of fixed size. Its size cannot be changed once it is declared. It contains both primitive data types and objects of a class. Array does not have generic features.

ArrayList: ArrayList is dynamic in size. Its size or capacity automatically grows when we add element into it. It contains only the object entries. It has a generic feature.

22) How can we check an array contains values or not?

Java Arrays class provides two methods isExists() and contains() to check an array has elements or not. Both the methods return true if an array has elements else returns false.

23) What is the equilibrium index of an array also give an example?

If the sum of lower indices is equal to the sum of higher indices is called an equilibrium index of an array. For example, consider the following array: [-7, 1, 5, 2, -4, 3, 0], where:

a[0]=-7, a[1]=1, a[2]=5, a[3]=2, a[4]=-4, a[5]=3, a[6]=0

Let's find the equilibrium index. According to the definition:

The sum of lower indices is = a[0]+a[1]+a[2] = -7+1+5 = -1

The sum of higher indices is = a[4]+a[5]+a[6] = -4+3+0 = -1

We observe that the sum of lower indices is equal to the sum of higher indices. Hence, the equilibrium index is 3.

In the above array, 6 is also an equilibrium index because the sum of a[0] to a[5] is 0, and the value of index a[6] is 0.

24) What is left-rotation in an array?

Left-rotation is an operation that can be performed over an array. In this operation, each element of an array shifts 1 unit to the left. Therefore, the lowest index's value moves to the highest index. We can perform any number of rotations over an array. It is also known as a circular array. Let's perform left-rotation twice over an array [7, 8, 9, 2, 5, 6].

After one rotation, we get the array [8, 9, 2, 5, 6, 7], after the second rotation, we get [9, 2, 5, 6, 8].

25) what are the advantages and disadvantages of an array?

Advantages of Array

  • We can store multiple elements of the same type under a single variable.
  • We can implement other data structures such as Stack, Queue, Tree, etc. with the help of an array.
  • We can fetch data elements using the index at run time.

Disadvantages of Array

  • Before using an array, it is mandatory to declare its size.
  • It is a static structure, so we cannot increase or decreases memory allocation.
  • Insertion and deletion operations are difficult because elements stored at the contiguous memory location. It also increases the cost.
  • Allocate more memory than required is the wastage of memory.