NEW
Font size
WorksheetsJAVA - Arrays
Total questions: 15
Worksheet time: 13mins
What will this code print?
int arr[] = new int [5];
System.out.print(arr);
0
value stored in arr[0].
0
Class name@ hashcode in hexadecimal form
What is the output of this program?
class array_output {
public static void main(String args[]) {
int array_variable [] = new int[10];
for (int i = 0; i < 10; ++i) {
array_variable[i] = i;
System.out.print(array_variable[i] + " ");
i++;
} } }
0 2 4 6 8
1 3 5 7 9
0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
What is the output of this program?
class multidimention_array {
public static void main(String args[]) {
int arr[][] = new int[3][];
arr[0] = new int[1];
arr[1] = new int[2];
arr[2] = new int[3];
int sum = 0;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < i + 1; ++j)
arr[i][j] = j + 1;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < i + 1; ++j)
sum + = arr[i][j];
System.out.print(sum);
} }
11
10
13
14
What is the output of this program?
class evaluate {
public static void main(String args[]) {
int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};
int n = 6;
n = arr[arr[n] / 2];
System.out.println(arr[n] / 2);
} }
3
0
6
1
What is the output of this program?
class array_output {
public static void main(String args[]) {
int array_variable[][] = {{ 1, 2, 3}, { 4 , 5, 6}, { 7, 8, 9}};
int sum = 0;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3 ; ++j)
sum = sum + array_variable[i][j];
System.out.print(sum / 5);
} }
8
9
10
11
Predict the output?
class Test {
public static void main(String args[]) {
int arr[2];
System.out.println(arr[0]);
System.out.println(arr[1]);
} }
0 0
garbage value garbage value
Compiler Error
Exception
Output of following Java program?
class Test {
public static void main (String[] args) {
int arr1[] = {1, 2, 3};
int arr2[] = {1, 2, 3};
if (arr1 == arr2)
System.out.println("Same");
else
System.out.println("Not same");
} }
Same
Not Same
Determine output:
public class Test{
public static void main(String[] args){
int[] x = {1, 2, 3, 4};
int[] y = x;
x = new int[2];
for(int i = 0; i < x.length; i++)
System.out.print(y[i] + " ");
} }
1 2 3 4
Compile Time Error
1 2
Runtime Error
Analyze the following code and choose the correct answer.
int[] arr = new int[5];
arr = new int[6];
The code has compile errors because the variable arr cannot be changed once it is assigned.
The code has runtime errors because the variable arr cannot be changed once it is assigned.
The code can compile and run fine. The second line assigns a new array to arr.
The code has compile errors because we cannot assign a different size array to arr.
Which two cause a compiler error?
1)float[ ] f = new float(3);
2)float f2[ ] = new float[ ];
3)float[ ]f1 = new float[3];
4)float f3[ ] = new float[3];
5)float f5[ ] = {1.0f, 2.0f, 2.0f};
2, 4
3, 5
4, 5
1, 2
What is the value of a[1] after the following code is executed?
int[] a = {0, 2, 4, 1, 3};
for(int i = 0; i < a.length; i++)
a[i] = a[(a[i] + 3) % a.length];
0
1
2
3
4
class Test{
public static void main (String[] args) {
int arr1[] = {1, 2, 3};
int arr2[] = {1, 2, 3};
if(arr1.equals(arr2))
System.out.println("Same");
else
System.out.println("Not same"); }}
Same
Not same
