WorksheetsTwo-Dimensional Array in Java
Total questions: 20
Worksheet time: 20mins
Given the code, what is in values[2][1] ?
7.9
9.2
7.3
0.5
Given the code, what is the value of things.length ?
4
3
12
7
Given the following:
double [][] stuff ;
Which of the following statements constructs an array with 5 rows of 7 columns each and assign its reference to stuff ?
stuff = new stuff[5][7] ;
stuff = new double[5][7] ;
new stuff = double[5][7] ;
stuff = double new[5][7] ;
How would you get the value "6" out of the following array?
a [0][3]
a [1][3]
c) a[0][2]
d) a[2][0]
e) a[3][1]
What would the statement a[1].length return?
3
4
2
1
Which of the following statements correctly declares a two-dimensional integer array?
int Matrix[ ] = new int[5,4];
int Matrix[ ]; Matrix = new int[5,4];
int Matrix[ ][ ] = new int[5][4];
int Matrix[ ][ ] = int[5][4];
How would you get the value 6 out of the following array int[][] a = {{2, 4, 6, 8}, {1, 2, 3, 4}};? Remember each set of curly braces represents a row, or a single dimensional array.
a [0][3]
a [1][3]
c) a[0][2]
d) a[2][0]
e) a[3][1]
How many columns does a have if it is created as follows
int[][] a = {{2, 4, 6, 8}, {1, 2, 3, 4}};?
3
4
2
1
import java.util.Arrays;
public class GfG
{
public static void main(String[] args)
{
int[][] mat = new int[2][2];
mat[0][0] = 99;
mat[0][1] = 151;
mat[1][0] = 30;
mat[1][1] = 5;
System.out.println(Arrays.deepToString(mat));
}
}
[[99, 151], [30, 5]]
[99,30],[151,5]
[99,151,5,30]
Compile error
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
Which is the correct way:
int[] arr or int arr[] ?
int[] arr
int arr[]
Both
None
import java.util.*;
class Array {
public static void main(String args[]) {
int array[] = new int [5];
for (int i = 5; i > 0; i--)
{
array[5 - i] = i;
Arrays.sort(array); System.out.print(Arrays.binarySearch(array, 4));
}
}
}
2
3
4
5
In a two-dimensional array, each row is an array.
true
false
What is the default value of a 2d array of integers?
null
0.0
0
false
Arrays must be made up of the same data types.
true
false
double[][] values =
{ {1.2, 9.0, 3.2},
{9.2, 0.5, 1.5, -1.2},
{7.3, 7.9, 4.8} } ;
what is in values[2][1] ?
7.3
9.2
7.9
3.2
Examine the following:
double[][] values =
{ {1.2, 9.0, 3.2},
{9.2, 0.5, 1.5, -1.2},
{7.3, 7.9, 4.8} } ;
what is in values[3][0] ?
1.2
9.2
7.3
No such element exists in array
Given the following:
double[][] things =
{ {1.2, 9.0},
{9.2, 0.5, 0.0},
{7.3, 7.9, 1.2, 3.9} } ;
What is the value of things[2].length ?
2
3
4
9
Given the following:
int[][] items =
{ {0, 1, 3, 4},
{4, 3, 99, 0, 7 },
{3, 2} } ;
Which of the following statements replaces the 99 with 77?
items[1][2] = 77;
items[2][1] = 77;
items[ 99 ] = 77;
items[2][3] = 77;
Which of the following constructs and assigns to array a 2D array with 7 rows, but does not yet construct the rows?
int[][] array = new int[7][];
int[][] array = new int[7];
int[][] array = new int[][7];
int[] array[7] = new int[];
