1. Consider the following 2-dimensional array. Select the statement that gives t
ID: 3837936 • Letter: 1
Question
1. Consider the following 2-dimensional array. Select the statement that gives the number of columns in the third row.
int[][] counts =
{
{ 0, 0, 1 },
{ 0, 1, 1, 2 },
{ 0, 0, 1, 4, 5 },
{ 0, 2 }
};
a) int cols = counts[2].size();
b) int cols = counts.length[2];
c) int cols = counts.length;
d) int cols = counts[2].length;
2. Consider the following code snippet:
int cnt = 0;
int[][] numarray = new int[2][3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++)
{
numarray[j][i] = cnt;
cnt++;
}
}
What is the value of numarray[1][2] after the code snippet is executed?
a) 2
b) 5
c) 3
d) 4
3. Which one of the following statements is the correct definition for a two-dimensional array of 20 rows and 2 columns of the type integer?
a) int[][] num = new int[20][2];
b) int[][] num = new int[2][20];
c) int[][] num = new int[20,2];
d) int[][] num = new int[2,20];
4. Consider the following code snippet:
int[][] numarray =
{
{ 3, 2, 3 },
{ 0, 0, 0 }
};
System.out.print(numarray[0][0]);
System.out.print(numarray[1][0]);
What is the output of the given code snippet?
a) 00
b) 31
c) 30
d) 03
5. Which one of the following statements is correct for displaying the value in the second row and the third column of a two-dimensional, size 3 by 4 array?
a) System.out.println(arr[1][2]);
b) System.out.println(arr[2][3]);
c) System.out.println(arr[2][1]);
d) System.out.println(arr[3][2]);
6. What will be printed by the statements below?
int[] values = { 4, 5, 6, 7};
values[0] = values[3];
values[3] = values[0];
for (int i = 0; i < values.length; i++)
System.out.print (values[i] + " ");
a)7 5 6 4
b)7 6 5 4
c)7 5 6 7
d)4 5 6 4
7. Assume the method createSomething has been defined as follows:
int [] createSomething (int start, int size)
{
int [] result = new int[size];
for (int i = 0; i < result.length; i++)
{
result[i] = start;
start++;
}
return result;
}
What is printed by the statement below?
System.out.print (Arrays.toString(createSomething(4, 3)));
a)[3, 4, 5, 6]
b)[4, 4, 4]
c)[3, 3, 3, 3]
d)[4, 5, 6]
8. Assume the method doSomething has been defined as follows:
int [] doSomething (int[] values)
{
int [] result = new int[values.length - 1];
for (int i = 0; i < result.length; i++)
{
result[i] = values[i] + values[i + 1];
}
return result;
}
What is printed by the statements below?
int [] nums = {3, 18, 29, -2} ;
System.out.print (Arrays.toString(doSomething(nums)));
a)[4, 19, 30, -1]
b)[21, 47, 27]
c)[21, 50, 48]
d)[3, 21, 47, 27]
9. Assume the following variable has been declared and given a value as shown:
int[][] data = {
{9, 17, -4, 21 },
{15, 24, 0, 9},
{6, 2, -56, 8},
};
Which is the value of data.length?
a)4
b)3
c)12
d)6
10. Assume the following variable has been declared and given a value as shown:
int[][] data = {
{9, 17, -4, 21 },
{15, 24, 0, 9},
{6, 2, -56, 8},
};
Which is the value of data[0].length?
a)21
b)12
c)4
d)3
Explanation / Answer
Question 1
Answer: d) int cols = counts[2].length;
Question 2
Answer: b) 5
Question 3
Answer:a) int[][] num = new int[20][2];
Question 4
Answer:c) 30
Question 5
Answer:a) System.out.println(arr[1][2]);
Question 6
Answer:c)7 5 6 7
Question 7
Answer:d)[4, 5, 6]
Question 8
Answer:b)[21, 47, 27]
Question 9
Answer:b)3
Question 10
Answer:c)4
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.