Interview Questions & Answers

Java

No, you can not declare Array without Array size. You will get a compile-time error.

1. An Array is static in nature i.e of fixed length. The size of an array is fixed and cannot be changed after the array has been created. ArrayList is dynamic in nature. If you add elements to an ArrayList, it will automatically increase its size.

2. An Array can contain both primitive and Object data types. ArrayList does not contain primitive data types (but contains primitive Wrapper classes). It only contains object entries.

3. Java provides add() method to insert an element into ArrayList and we can use the assignment operator to store elements into Array.

4. We can not use Generics along with Array whereas ArrayList allows you to use Generics to ensure type safety.

5. Length of the ArrayList is provided by the size() method while Each array object has the length variable which returns the length of the array.

Example:-

public class Main {
    public static void main(String[] args) {
        // Creating an ArrayList of String using
        List<String> animals = new ArrayList<>();
        // Adding new elements to the ArrayList
        animals.add("Lion");
        animals.add("Tiger");
        animals.add("Cat");
        animals.add("Dog");
        System.out.println(animals.size());
    }
}
output: 4

6. The performance of Array and ArrayList depends on the operation you are performing :
resize() operation: Automatic resize of ArrayList will slow down the performance as it will use a temporary array to copy elements from the old array to the new array.
add() or get() operation: adding an element or retrieving an element from the array or ArrayList object has almost the same performance, as for ArrayList object these operations run in constant time.

ArrayOutOfBoundsException is thrown when an attempt is made to access the Array with an illegal index. For example, an illegal index means if the index is either negative or greater than or equal to the size of the Array.

No, you can not pass the negative number as Array size. If you pass a negative number in Array size then you will not get the compiler error. Instead, you will get the NegativeArraySizeException at run time.

There are two types of array.

1. Single Dimensional Array

2. Multidimensional Array

Single Dimensional Array:-

A single dimensional array of Java is a normal array where the array contains sequential elements (of the same type).

Ex:        int[] array = { 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000 };

Multidimensional Array:-

A multi-dimensional array in Java is an array of arrays. A two-dimensional array is an array of one- dimensional arrays and a three-dimensional array is an array of two-dimensional arrays.

Ex:

public class Main {
    public static void  main(String[] args) {

        String[][] names = {{"Mr. ","Mrs. ","Ms."},{"Smith","Jones"}};
        // Mr. Smith
        System.out.println(names[0][0] + names[1][0]);
        // Ms. Jones
        System.out.println(names[0][2] + names[1][1]);
    }
}
output: 
Mr. Smith 
Ms. Jones

Advantage :-

  •  The main use of Array is used to store multiple values in a single variable, instead of declaring separate variables for each value.

Example:-

int[] array = {10,20,30,40,50,60,70,80};

  • We can access any element randomly by using indexes provided by arrays.

Example:-

int[] array = {10,20,30,40,50,60,70,80};

System.out.println(array[5]);

output: 60

  • We can sort multiple elements of Array at the same time.

 

Disadvantage :-

  • Size Limit: We can store the only fixed size of elements in the array. It doesn’t grow its size at runtime.
  • Arrays are Strongly-Typed this means that all elements in the array have the same data type. We can not store different types of data in an Array.

An Array is a data structure that defines an indexed collection of a fixed number of homogeneous data elements. This means that all elements in the array have the same data type and Array a starts from index 0.

The size of an array is fixed and cannot be changed after the array has been created.

In Java, arrays are objects. Arrays can be of primitive data types or reference types. The main use of Array is used to store multiple values in a single variable, instead of declaring separate variables for each value.

Example:-

public class Main {
    public static void  main(String[] args) {
        int[] array = {10,20,30,40,50};
        String[] strArray = {"Ramesh"};
        Book[] bookArray = new Book[10];
    }
}
class Book{
    
}

Yes, you can make the main() method final.

Example:-

public class Main {
    public static final void main(String[] args) {
        System.out.println("Hi");

    }
}

output: Hi