Java - Interview Questions and Answers on Arrays

Q1.  What is the advantage of using arrays over variables ?

Ans. Arrays provide a structure wherein multiple values can be accessed using single reference and index. This helps in iterating over the values using loops.

Q2.  What are the disadvantages of using arrays ?

Ans. Arrays are of fixed size and have to reserve memory prior to use. Hence if we don't know size in advance arrays are not recommended to use.

Arrays can store only homogeneous elements.

Arrays store its values in contentious memory location. Not suitable if the content is too large and needs to be distributed in memory. 

There is no underlying data structure for arrays and no ready made method support for arrays, for every requriment we need to code explicitly

Q3.  Advantage of Collection classes over Arrays ?

Ans. 

Collections are re-sizable in nature. We can increase or decrease the size as per recruitment. 

Collections can hold both homogeneous and heterogeneous data's.

Every collection follows some standard data structures.

Collection provides many useful built in methods for traversing,sorting and search. 

Q4.  What are the Disadvantages of using Collection Classes over Arrays ?

Ans. 

Collections can only hold objects, It can't hold primitive data types.

Collections have performance overheads as they deal with objects and offer dynamic memory expansion. This dynamic expansion could be a bigger overhead if the collection class needs consecutive memory location like Vectors.

Collections doesn't allow modification while traversal as it may lead to concurrentModificationException.

Q5.  What is the difference between int[] x; and int x[]; ?

Ans. No Difference. Both are the acceptable ways to declare an array.