Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Top 55 Java Array Interview Questions Answers

Tags: array

Here, we have listed the most important 55 java array interview questions with the best possible answers.

These Array interview questions can be asked in any Java tests or interviews from freshers or 1 to 3 years of experience.

We have also covered coding related questions that are generally asked in Java interviews.

If you prepare the answers of these array questions in java, you can easily answer the questions asked by the interviewer in the company.

Most Important Array Interview Questions in Java


1. What is an Array in Java?

An array is a collection of elements (or data) of the same type that are referenced by a common name. For example, we can store a group of strings, a group of int values, or a group of float values in the array. But, we cannot store some int values or some float values in the array.

2. How can an array reference variable be declared in java?

Ans: An array can be declared by specifying its data type, name, and size. For example, int[ ] a;

3. What is an array variable?

Ans: An array variable is a reference variable that points to an array object. The memory that is allocated to an array variable actually stores a reference to an array object. It does not actually store an array itself.

4. Name the keyword that is used for allocating memory space to an array?

Ans: new keyword

5. How to create an array in java?

Ans: We can create an array object by using a new keyword and assign its reference to the variable. The general syntax for the creation of array object is as follows:

datatype[ ] arrayname = new datatype[size];
For example:
int[ ] myList = new int[10]; // It can store 10 elements of int type.

6. Which element is positioned at num[9] of an array num?

Ans: Tenth element.

7. If, array[ ] = {10, 20, 5, 30, 25};

a) What is array.length?
b) What is array[2]?

Ans: (a) 5, (b) 5

8. How to declare a 2d array of size 2 * 3 to store only characters?

Ans: char[ ][ ] chrs = new char[2][3];

9. How to create an integer array of size 3 * 2 and initialize with values from 1 to 9?

Ans: int[ ][ ] x = {{2, 3}, {3, 5}, {5, 9}};

10. Where are elements of an array stored?

Ans: Elements of an array are stored in the contiguous memory locations of RAM during runtime by JVM. They are stored in heap memory.

11. What is the total size of an array chrs having 25 elements of char type?

Ans: 50 bytes

12. What is the total size of an array num[100] of int type?

Ans: 400 bytes.

13. What is sorting of array in Java?

Ans: The process of arranging elements of an array in a specified array is called sorting of array.

14. Can we specify array size as negative?

Ans: No, it is not possible to specify array size as negative. Still, if we try to specify the negative size, there will be no compile-time error, but at runtime, we will get an exception named NegativeArraySizeException.

15. In which of the following lines of code snippets, there will be compile-time error and why?

int[ ][ ] x = new int[2][ ]; // Line 1
int[ ] y = new int[2]; // Line 2
int num = 2; // Line 3
x[1] = y; // Line 4
x[1] = num; // Line 5

Ans: Line 1 will compile fine and it will create two dimensional array.

Line 2 will also compile fine and it will create one dimensional array.

There is no compilation problem in line 3. It declares and initializes an int variable.

Line 4 also compiles fine since a two-dimensional array is an array of arrays. It assigns a one-dimensional array to one of the dimensions in the two-dimensional array.

Line 5 will create a compilation error problem because we cannot assign an int variable to an int array variable.


16. Can we modify the size (or length) of the array once set it?
Or, can we insert or delete elements after creating an array?

Ans: No. Once the size of an array is defined, we cannot modify it. We cannot add or delete elements after creation of an array. In other words, an array cannot expand or shrink at runtime.

17. What will happen when the following lines of code are compiled and executed?

int i = -3;
int[ ] arr = new int[i];
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;

Ans: The code above will compile fine but at runtime, it will throw an exception named NegativeArraySizeException because i is a negative number that cannot be used as an array index.

18. Will the below code compile and execute successfully?

int i = 3;
int[ ] arr = new int[i];
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
arr[3] = 4;

Ans: Yes, the lines of code will be compiled successfully but at runtime, there will be ArrayIndexOutOfBoundsException. This exception occurs when we attempt to use an index that is less than zero or greater than or equal to the length of an array.

19. Will the above lines of code compile and execute successfully? If yes, what will be the output?

public class Test {
public static void main(String[] args)
{
   int i = 4;
   String[ ] arr = new String[i];
    arr[0] = "A";
    arr[1] = "B";
    arr[2] = "C";
   System.out.print(Arrays.toString(arr));
  }
}

Ans: Yes, the code will be compiled successfully. The output is [A, B, C, null].

20. Which exception will be thrown at runtime when the below code will be compiled?

public class Test {
public static void main(String[] args)
{
  Object[ ] arr = new String[3];
    arr[0] = "A";
    arr[1] = "B";
    arr[2] = 20;
    System.out.print(Arrays.toString(arr));
   }
}

Ans: The code will compile fine but at runtime, we will get ArrayStoreException (runtime exception). This exception generally occurs when we attempt to store non-compatible elements in an array object.

The type of the elements must be compatible with the type of array object. For example, we can store only string elements in an array of type String. If we will attempt to insert integer elements in an array of String, an ArrayStoreException will be thrown at runtime.


21. For the below code, what will be the total size taken by the whole array in the memory?

double[ ] num = new double[10];

Ans: Each element in the num array is a variable of double type that needs 8 bytes in the memory. So, the whole array will take space 80 bytes in the memory, plus 8 bytes for the num variable to store the reference of array in the memory.

22. What is the difference between int[ ] arr and int arr[ ]?

Ans: There is no difference between int[ ] arr and int arr[ ]. Both are the legal ways to declare an array in java. int[ ] arr is a standard declaration.

23. What is an anonymous array in java?

Ans: An array without reference is called an anonymous array. For example:

// Creating an anonymous array.
     System.out.print(new int[]{1, 2, 3}.length); // It will display 3.

The post Top 55 Java Array Interview Questions Answers appeared first on Scientech Easy.



This post first appeared on Scientech Easy, please read the originial post: here

Share the post

Top 55 Java Array Interview Questions Answers

×

Subscribe to Scientech Easy

Get updates delivered right to your inbox!

Thank you for your subscription

×