Top 55 Java Array Interview Questions Answers

24. What is the default value of elements of an array at the time of creation?

Ans: When we create an array, it is always initialized with the default values. The default values of an array of different types are as follows:

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

25. Is there any error in the below code? If not, what is the output of the following program?

import java.util.Arrays;
public class Test {
public static void main(String[] args)
{
  String[ ] str = new String[5];
   str[0] = "A";
   str[1] = "20";
   str[3] = "B";
   str[4] = "C";
  System.out.println(Arrays.toString(str));
  }
}

Ans: No, there is no error in the above lines of code. The output will be [A, 20, null, B, C].

26. What is the difference between length() method and length in java?

Ans: The length() method is provided by String class that is used to return the number of characters in the string. For example:

String str = "Hello world";
System.out.println(str.length());

str.length(); will return 11 characters including space.

Whereas, length is an instance variable in arrays that will return the number of values or objects in array. For example:

String[ ] names = {"John", "Herry", "Deepak", "Ivaan", "Ricky"};
System.out.println(names.length);

names.length; will return 5 since the number of values in names array is 5.


27. What is the important difference between an Array and ArrayList in Java?

Ans: There are the following important differences between Arraya and ArrayList in Java. They are as follows:

Array:

  1. Array is static in size. It has a fixed size.
  2. We cannot change the size (or length) of the array after creating it.
  3. An array can contain both primitive data types as well as reference types.
  4. Array in java does not support generics.
  5. Elements of an array can be iterated using for loop.
  6. An Array can be multi-dimensional.

ArrayList:

  1. ArrayList is dynamic in size.
  2. The size of the ArrayList grows or shrinks automatically as we insert or remove elements.
  3. ArrayList cannot contain primitive data types. It contains only objects. That is, we can store only reference types in ArrayList.
  4. ArrayList in java supports generics.
  5. In an ArrayList, we can use an Iterator object to traverse elements.
  6. Arraylist is always of a single dimension.

28. Which one is the preferred collection for storing objects between an Array and ArrayList?

Ans: ArrayList is backed up by array internally. There are several usability advantages of using an ArrayList over an array in Java. They are:

a) An array has a fixed length at the time of creation. Once it is created we cannot modify its length.
ArrayList is dynamic in size. Once it reaches a threshold, it automatically allots a new array and copies the contents of old array to the new array.

b) Also, ArrayList supports Generics. But Array does not support Generics.

The array should be used in the place of an ArrayList if

  • We know the size of an array in advance and do not need re-sizing the collection.
  • We need to store the same type of elements.

29. Why does not an array support generics in Java?
Or, why cannot we create generic array in java?

Ans: Java does not allow the creation of a generic array because an array carries type information of its elements at runtime. This information is used at runtime to throw ArrayStoreException if data type of elements does not match with the defined type of Array.

In the case of Generics, the type information of a collection gets erased at runtime by Type Erasure. Due to which an array cannot use generics.

30. What are the types of arrays in java?

Ans: Generally, arrays are categorized into two forms that are:

  • Single dimensional arrays (or 1D array called one-dimensional array)
  • Multidimensional arrays (or 2D, 3D arrays)

31. What are the main advantages of arrays in java?

Ans: There are several advantages of arrays in Java. They are:

a) The main advantage of array structure is that it organizes data in such a way that it can be easily manipulated.
b) We can access randomly nth element in an array at a constant time.
c) Arrays generally use less space because no variable needs to support navigation.
d) They help to optimize the code, thereby, we can retrieve or sort the data efficiently.

32. What are the drawbacks/disadvantages/limitations of Arrays in Java?

Ans: There are many drawbacks of arrays in java that are as follows:

a) Since arrays in java are fixed in size, we can store only the fixed size of elements in the array.
b) Once the size of an array is created, it cannot be changed. That is, an array cannot expand or shrink at runtime.
c) We cannot store different kinds of elements in an array.


33. What will be the output of the following program?

public class Test {
public static void main(String[] args)
{
   int x = 10;
   int[ ] nums = new int[x];
     x = 30;
    System.out.println("x: " +x);
    System.out.println("Size of nums: " +nums.length);
   }
}

Ans: x: 30, Size of nums: 10

34. What will be the output of the following code?

public class Test {
public static void main(String[] args)
{
    int arr[ ] = {1, 2, 3, 4, 5, 6};
       for (int i = 1; i < arr.length; i++)
           arr[i] = arr[i - 1];
             for (int i = 0; i < arr.length; i++)
             System.out.print(arr[i] + " ");
   }
}

Ans: Output: 1 1 1 1 1 1

35. What is a jagged array in java?

Ans: A two-dimensional array having different rows is called jagged array. It is also called ragged array in java.

36. How many ways of copying an array into another array?

Ans: There are four ways available in java to copy an array into another array. They are:

  • Using for loop
  • Using Arrays.copyOf() method
  • Using System.arraycopy() method
  • Using clone() method

37. Will the below code compile fine? If yes, what will be the output of the following program?

public class Test {
public static void main(String[] args)
{
   int[ ] arr;
     arr = new int[1];
     arr = new int[2];
       arr[0] = 20;
       arr[1] = 30;
       arr[2] = 40;
   }
}

Ans: Yes, the code will compile fine but at runtime, we will get ArrayIndexOutOfBoundsException because once an array is created, its size cannot be resized.

Please share your love