Question 1
Which of the following is the correct way to declare an array in Java?
int arr[5];
int arr = new int(5);
int[] arr = new int[5];
array<int> arr = new int[5];
Question 2
What will be the output of the following code?
int[] arr = {1, 2, 3, 4, 5};
System.out.println(arr[2]);
1
2
3
4
Question 3
What is the default value of an integer array in Java?
null
0
garbage value
depends on JVM
Question 4
Which of the following is used to determine the length of an array in Java?
arr.length();
arr.size;
arr.length;
arr.count();
Question 5
What will be the output of the following program?
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
System.out.println(matrix[1][2]);
4
5
6
9
Question 6
How can you correctly initialize a 2D array in Java?
int[][] arr = new int(3,3);
int arr[][] = {{1,2}, {3,4}};
int arr[][] = new int[2][2]{{1,2}, {3,4}};
int arr[2][2] = {1,2,3,4};
Question 7
What happens if you access an invalid index of an array in Java?
ArrayIndexOutOfBoundsException
NullPointerException
Segmentation Fault
No Error
Question 8
What is the time complexity for accessing an element in an array by index?
O(1)
O(n)
O(log n)
O(n^2)
Question 9
What will be the output of the following program?
int[] arr = {10, 20, 30, 40, 50};
for (int i : arr) {
System.out.print(i + " ");
}
10 20 30 40 50
0 0 0 0 0
Compilation Error
10 10 10 10 10
Question 10
How do you define a jagged array in Java?
int[][] arr = new int[3][3];
int[][] arr = {{1, 2, 3}, {4, 5}, {6}};
int arr[][] = new int[3][];
Jagged arrays are not supported in Java
There are 10 questions to complete.