PPL: Array


Array: An array is a homogeneous collection of data elements in which an individual

element is identified by its position in the, relative to the first element.
The individual data elements of an array are of the same type.

  • In the languages, such as C, C++, Java, Ada, and C#, all of the elements of an array are required to be of the same type.
  • In some other languages, such as JavaScript, Python, and Ruby, variables are typeless references to objects or data values. In these cases, arrays still consist of elements of a single types, but the elements can reference objects or data values of different types. Such arrays are still homogeneous, because the array elements are of the same type.
type, but the elements can reference objects or data values of different types.

Some of the types of array:

1.    Single Dimensional Array
2.    Two Dimensional Array
3.    Three Dimensional array

What are the design issues for arrays?
The design issues for arrays are: 
  • What type are legal for subscripts? 
  • When are subscript ranges bound? 
  • When does array allocation takes place? 
  • Are ragged or rectangular multidimensioned arrays allowed or both? 
  • Can arrays can be initialized, when they have their storage allocated?
  • What kinds of slices are allowed, if any?

Viva Voce on Array

Q1. What do you mean by an Array? 
  • Array is a set of similar data type.
  • Arrays objects store multiple variables with the same type.
  • It can hold primitive types and object references.
  • Arrays are always fixed
Q2. How to create an Array? 
Same as variable, but you need to add [] after the type.
Example: int marks[ ];
Q3. Advantages and disadvantages of Array? 
  • Arrays can sort multiple elements at a time.
  • We can access an element of Array by using an index.
  1. We have to declare Size of an array in advance. However, we may not know what size we need at the time of array declaration.
  2. The array is static structure. It means array size is always fixed, so we cannot increase or decrease memory allocation.
Q4. Can we change the size of an array at run time?
Q5. Can you declare an array without assigning the size of an array? 
Q6. What is the default value of Array? 
Any new Array is always initialized with a default value as follows
  • For byte, short, int, long – default value is zero (0).
  • For float, double – default value is 0.0.
  • For Boolean – default value is false.
  • For object – default value is null.
Q7. Can we declare array size as a negative number? 
Q8. When will we get ArrayStoreException? 
If anybody tries to insert integer element in String Array, then we will get ArrayStoreException at run time.
Q9. Can we add or delete an element after assigning an array? 
Q10. Is there any difference between int[] a and int a[]? 
No .
Q11. What are “jagged” arrays in java?
Jagged Arrays are Arrays are containing arrays of different length. Jagged arrays are also known as multidimensional arrays.
Q12. When ArrayIndexOutOfBoundsException occurs?
It is a run time exception.It will occur when the program tries to access invalid index of an array. Index higher than the size of the array or negative index.
Q13. How do we search a specific element in an array? 
We can use Arrays.binarySearch() method. This method uses binary search algorithm.
Q14. If you do not initialize an array what will happen?
Array will have default value.
Q15. Can we use Generics with the array? 
Q16. Where is the memory allocated for arrays in Java?
On the heap as arrays in Java are objects.
Q17. What is the two-dimensional array?
An array of an array in Java. We can declare them like int p[][] = new int[4][4] which is a matrix of 4×4.
Q18. Do we have 3-dimensional arrays in Java? 
Yes, Java supports N-dimensional array.