1. What is JAVA array?
In JAVA, an array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.
2. What are valid declaration of JAVA array?
int a[5];
int a[]=new int[];
int a[]=new int[5];
int a[]=new int[-5];
int a[]=new int[0];
int[] a=new int[5];
int []a=new int[5];
Answer:
int a[5];
Invaild - Compilation error. Because in JAVA , there is no permenant memory allocation
int a[]=new int[];
Invalid - Compilation error due to array size is not given.
int a[]=new int[5];
Valid declaration, no error
int a[]=new int[-5];
Valid declaration, but NegativeArraySizeException throws during run time
int a[]=new int[0]; // Empty array
Valid declaration, no error
int[] a=new int[5];
Valid declaration, no error
int []a=new int[5];
Valid declaration, no error
3. What will be the output of the below JAVA code?
Output:
[I@18ef96
Explanation:
Array name represents base address or reference of the array. In JAVA, classname@hashcode represents the reference of an array.
4. What will be the output of following JAVA code?
[[I@18ef96 → base reference id(classname@hasecode) of two dimensional array a.In JAVA, an array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.
2. What are valid declaration of JAVA array?
int a[5];
int a[]=new int[];
int a[]=new int[5];
int a[]=new int[-5];
int a[]=new int[0];
int[] a=new int[5];
int []a=new int[5];
Answer:
int a[5];
Invaild - Compilation error. Because in JAVA , there is no permenant memory allocation
int a[]=new int[];
Invalid - Compilation error due to array size is not given.
int a[]=new int[5];
Valid declaration, no error
int a[]=new int[-5];
Valid declaration, but NegativeArraySizeException throws during run time
int a[]=new int[0]; // Empty array
Valid declaration, no error
int[] a=new int[5];
Valid declaration, no error
int []a=new int[5];
Valid declaration, no error
3. What will be the output of the below JAVA code?
class Learn{
public static void main(String[] args){
int a[]={1,2,3,4,5};
System.out.println(a);
}
}
Output:
[I@18ef96
Explanation:
Array name represents base address or reference of the array. In JAVA, classname@hashcode represents the reference of an array.
- The string “[I” is the run-time type signature for the class object “array with component type int“.
- The only direct superclass of any array type is java.lang.Object.
- The string “[B” is the run-time type signature for the class object “array with component type byte“.
- The string “[S” is the run-time type signature for the class object “array with component type short“.
- The string “[L” is the run-time type signature for the class object “array with component type of a String Class”. The Class name is then followed.
- The string “[Z” is the run-time type signature for the class object “array with component type of boolean”. The Class name is then followed.
- The string “[J” is the run-time type signature for the class object “array with component type of long”. The Class name is then followed.
4. What will be the output of following JAVA code?
public class Learn{
static int[] add(){
return new int[]{1,2,3,4};
}
public static void main(String args[]){
int a[]=add();
System.out.println("Array returned by add()");
DisplayArray(a);
System.out.println("Anonymous array");
DisplayArray(new int[]{11,22,33});
}
static void DisplayArray(int x[]){
for(int i=0;i<x.length;i++)
System.out.println(x[i]);
}
}
Output:
Array returned by add()
1
2
3
4
Anonymous array
11
22
33
5. What will be the output of the following JAVA program?
class LWD{
public static void main(String l[]){
int a[][]={{11,22,33},{44,55}};
System.out.println(a);
System.out.println(a[0]);
}
}
Output & Explanation:
[[I@18ef96
[I@6956de9
5. What will be the output of the following JAVA program?
class LWD{
public static void main(String l[]){
int a[][]={{11,22,33},{44,55}};
System.out.println(a);
System.out.println(a[0]);
}
}
Output & Explanation:
[[I@18ef96
[I@6956de9
[I@6956de9→ first row reference id(classname@hasecode) of array a.
6. Find the valid statement to declare two dimensional array.
int[][] a,b;
int a[][],b[][];
int[] []a,b;
int[] []a,[]b;
Answer & Explanation:
int[][] a,b → valid; a and b both are 2D array.
int a[][],b[][] → valid; a and b both are 2D array.
int[] []a,b → valid; a and b both are 2D array.
int[] []a,[]b → Compile time error; If dimension is specified before variable only applicable for the first variable.
7. Find the valid declaration of array size.
int[] W=new int['a'];
short s=10;
int[] X=new int[s];
byte b=20;
int[] Y=new int[b];
long j=10;
int[] Z=new int[j];
Answer & Explanation:
int[] W=new int['a']; →valid;
short s=10;
int[] X=new int[s]; →valid;
byte b=20;
int[] Y=new int[b];→valid;
long j=10;
int[] Z=new int[j];→Invalid;
Array size declaration only possible in the range of char, byte, short and integer; So long variable is not possible for array size declaration.
8. Which are valid declaration of array dimension?
int a[][]=new int[3][3];
int a[][]=new int[][3];
int a[][]=new int[3][];
int a[][]=new int[][];
short s=10;
int[] X=new int[s]; →valid;
byte b=20;
int[] Y=new int[b];→valid;
long j=10;
int[] Z=new int[j];→Invalid;
Array size declaration only possible in the range of char, byte, short and integer; So long variable is not possible for array size declaration.
8. Which are valid declaration of array dimension?
int a[][]=new int[3][3];
int a[][]=new int[][3];
int a[][]=new int[3][];
int a[][]=new int[][];
Answer & Explanation:
int a[][]=new int[3][3];→valid; 3 rows and 3 columns
int a[][]=new int[][3];→Invalid; Row dimension is not declared, array size should have atleast base(row) size.
int a[][]=new int[3][];→valid; row size has mentioned.
int a[][]=new int[][];→Invalid; base size is not mentioned.
int a[][]=new int[3][3];→valid; 3 rows and 3 columns
int a[][]=new int[][3];→Invalid; Row dimension is not declared, array size should have atleast base(row) size.
int a[][]=new int[3][];→valid; row size has mentioned.
int a[][]=new int[][];→Invalid; base size is not mentioned.




It uses strong memory management.