1. In Java, arrays are treated as ________ You can create an array using the new keyword similar to objects and populate it using the indices as ?
2. It is NOT possible to directly assign values with in flower braces separating them with commas (,) as shown below.
3. Which of the following statements best describes the following code?
4. Since JDK 1.5, Java introduced a new for loop known as foreach loop or enhanced for loop, which enables you to traverse the complete array sequentially ___________________.
5. What does the following code print out?
6. Four names are printed when we run this code. What is the last name to be printed?
7. This code seeks to print out the contents of the array, in this case, 10 and then 5. What needs to be added or changed?
8. When an array is passed as an argument to a method,
the name of the array refers to its address in memory.
9. What is the output of this code?
public class ArrayLoop
{
// What does this method do?
public static void magic(int[] values, int amt)
{
for (int i = 0; i < values.length; i++)
{
values[i] = values[i] * amt;
}
}
// What does this method do?
public static void printValues(int[] values)
{
for (int i = 0; i < values.length; i++)
{
System.out.println( values[i] );
}
}
public static void main(String[] args)
{
int[] numArray = {2,3,4,5};
magic(numArray, 2);
printValues(numArray);
}
}
4,5,,6,7
4,6,8,10
3,4,5,6
2,3,4,5
10. Which of the following statements best describes the following code that seeks to execute getIndexOfLastElementSmallerThanTarget(values,-13)?
11. What is wrong with the following code? It seeks to execute getIndexOfLastElementSmallerThanTarget(values, 7);
12. Analyse the following code. What does the && I < 5 do?
13. Given the following values of a and the method doubleLast, the values of a after you execute: doubleLast() would be {-40, -30, 4, 8, 16, 32}.
14. The above code loops from the middle to the end doubling each value. Since there are 6 elements it will start at index 3.
15. Given the following values of a and the method mystery, the values of a after you execute: mystery() would be {-40, -15, 4, 8, 16, 33}.
16. The above code loops from the middle to the end and doubles every other element (i+=2 is the same as i = i + 2).
17. The following loop header will cause an ArrayIndexOutOfBounds error while traversing the array scores:
18. The index cannot be equal to scores.length, since (scores.length - 1) is the index of the last element. This loop will therefore cause an error while traversing the array scores.
19. This will cause an error because i++ will continue to increment the index past the end of the array. It should be replaced with i? to avoid this error.
20. Assume that temp is an int variable initialized to be greater than zero and that a is an array of integers. Whenever a includes a value that is less than or equal to zero _________________________.