Java Interview Questions - Array
1. What is array and what is the use?
Array is a collection of similar types of elements that have continuous memory location which is generally use to prepare a Liost.
2. What are the types of array?
There are two types of array present in java
- SingleDimensionalArray.
- MultiDimensionalArray.
3. What is jagged array?
Array in which number of columns different in each row is called jagged array.
4. What is location of Arrays class in java and give 5 different methods present in Arrays class?
Array is a predefined class present in java.utilpackage.Some methods of array is:
- Public static int binarySearch().
- public static void sort()
- Public static Boolean[] copyof().
- Public static short[] copyOfRange().
- Public static Boolean equals().
- Public static void fill().
- Public static int hashCode().
- Public static String toString().
5. What will be the default values of all the elements of an array defined as an instance variable?
Default value of data type.
6. How do we allocate an array dynamically in java?
By new keyword
7. Explain with example how to initialize an array of objects?
classnameobject_name[] = new class_name[size]; Test obj[] = new Test[4];
Obj[0] = new Test();
8. What is the first argument of the String array in main method?
In java the length of command line argument is 0. So no argument treated as first argument.
9. How can one prove that the array is not null but empty using one line of code?
Display the value of array index I.e. X [0];
10. Are arrays primitive data types?
No Array are not primitive data types.
11. What is the advantage of Array?
- Code optimization: It makes the code optimized, we can retrieve or sort the data easily.
- Randomaccess:Wecangetanydatalocatedatanyindexposition.
12. What is the disadvantage of array?
Size limit: We can store only fixed size of elements in the array .It does not grow its size at run time. For this problem collection frame work is used in java.
13. What is the class of array?
In java, array is an object .For array object ,an proxy class is created whose name can be obtained by getClass().getName() method on the object.
14. Which concept is used to solve the size limit problem in java?
Collection framework is used to solve the size limit problem in java.
15. How Array construct in java? Ans:
By 3 ways
- Declaration.
- Instantiation.
- Initialization.