1) All the elements of an array in Java MUST conform to the same data type. 2) W
ID: 3836757 • Letter: 1
Question
1) All the elements of an array in Java MUST conform to the same data type.
2) What is the value stored in the variable z by the statements below?
int[ ] q = {3, 2, -2, 4, 7, 0, 1};
int z = q.length;
3 Which of the following statements declares a new array variable with no syntax errors?
4) What is printed by the following code?
int[ ] x = {3, 4, 5, 6, 7};
int[ ] y = x;
for (int i = 0; i < 3; i++)
y[i + 2] = 3 * y[i];
System.out.println( x[3] );
5) In the following method call, which argument is an array?
myObject.some_method(x, y, 3);
6) Which of the following statements correctly uses a reference to the entire array myArray as the argument of the method call? (The method call comes from within the same class, so no qualifier is needed.)
public void myMethod( int[ ] a ) { ... }
7) An array of doubles is stored in the variable v as shown below. Which statement below will print some or all of the contents of the array with NO errors (syntax or logical)?
int vSize = 5;
double[ ] v = new double[vSize];
for (int i = 0; i < vSize; i++)
v[i] = 1.1;
8) Which of the following method signatures correctly specifies an array as a return type?
9) Which of the following is the BEST description of a 2-D array in Java?
10) A loop is being used to find the lowest value in a double array named numbers. Which of the following statements performs a useful initialization of the accumulator?
a) TrueExplanation / Answer
Answers:
(1). (a) True
The definition of array says array means the collection of similar kind of data types so this means that all the elements confirm to the same data type.
(2). (d) 7
length property of an array gives number of elements stored inside that array. As here q[ ] contains 7 elements, z will have value 7.
(3). (e) all of the above
First 2 statements are declaring array that will get allocated the space at next steps in program whereas in option (c) and (d), the arrays are declared and at the same time they are allocated the memory and all of these are valid declarations.
(4). (d) 12
This is somewhat more logical.
At first, array x[ ] is defined and in the next line, y = x is written. This means that now y and x both points to the same memory locations because y is not allocated some new space, it is assigned the same space where x is pointing to. So, whether you write x[0] or y[0] both points to the same location now.
So, in the program snippet, at the end, x[3] is printed and if you see the for loop, it is changing the value using 'y' so what is the value of y[3], it will be the same for x[3].
So in the loop find y[3].
y[i+2] = 3 * y[i] is written in the loop so i+2 = 3 and so i=1.
So, y[3] = 3 * y[1] and y[1] = x[1] = 4 so y[3] becomes 12.
So x[3] = 12
So, 12 is printed here.
(5). (e) can't say without seeing more code
Here, it is possible that x and y can be a variable or an array, both are passed in the same way so you will require to see more code in order to determine this.
(6). (a) myMethod( myArray );
When you pass the array in this way, it is the correct way. If you pass myArray[0], it is an integer value, not the entire array so it is not passing entire array here. So option (c) is not correct.
If you pass it as myArray[ ], it is also not correct. So, (b) is also not correct option. And so (d) is also not possible as (b) and (c) are not correct.
So (a) is the right option here.
(7). (b) System.out.println( v[0] );
In option (a), it prints the hashcode of array v[ ].
Option (b) prints its first value.
Option (c) tries to print v[5] but v has only 5 elements : v[0] to v[4]. So this will give an error.
So option (d) is also not possible and as (b) is the right option, option (e) is not correct so (b) is the right option.
(8). (b) private int[ ] testMethod ( int x )
'array' is not a recognized word in Java so option (a) is not possible.
Option (b) is the valid one.
Option (c) is defining array size which is not the way you specify the return type.
In option (d), [ ] is written after testMethod word which seems return type int but this is also not serving the purpose of defining array as return type.
So, as we were able to find it from (b) is the correct one, (e) is also not correct.
So option (b) is the correct one.
(9). (c) a 2-D array is a matrix
2-D array is considered as a matrix i.e. the grid of numbers that is identified as rows and columns. So option (c) is the best suitable option.
(10). (c) double lowestValue = numbers[0];
This initialization is useful as the number of comparisons is reduced by 1.
See the logic: assign the first value of array to one variable.
Using for loop, loop through second i.e. numbers[1] to last value and if the value is smaller than lowestValue, save it as lowestValue and continue till the last value of numbers[ ] . So this how 1 comparison is also less. If you had initialized as option (a), say for example, numbers[ ] have all the values greater than 0, then 0 will be returned from here, not the lowest of numbers[ ].
So, option (c) is the valid one.
Do comment if there is any query. Thank you. :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.