Answers to Array error practice
by Tim Bahls
1. It won't compile because arr has not been declared. Variables
with an array type, like any other variables, must be declared.
2. It won't compile because arr has not been initialized--no array has been created. Array variables, like any other variable, must be initialized before they can be used.
3. This illustrates the correct syntax for using arrays. It compiles,
runs, and prints 3.
4. Even though arr[0] and arr[1] have not been initialized, this still
compiles and runs. Similar to data members, the values of an array start
with default values. The default value for numbers is 0, so the program
prints 0 + 0, which is 0.
5. This will compile just fine--negative one is an int just as three is an
int. In general, the compiler checks types, not values. However,
the program will throw an ArrayIndexOutOfBoundsException and stop running.
6. This will not compile. Array indexes can only be ints, but 1.5 is a
double. The compiler will tell us we can't convert from double to int.
7. The compiler checks types, not values. So this is the same as the
previous question--it won't compile.
8. The compiler will catch this just like the previous one. Since it
just checks types, it doesn't matter that six is an integer since 6.0 is a
double.
9. This actually won't compile. It tells us it can't convert from
double[] to int[]. This is a little surprising, but it makes
sense--suppose the next line is arr[0]=2.5;
10. This works. Like any other variable, array variables can be declared
and initialized on separate lines. The code won't print anything, of
course.
11. This works too. Just as you can have a String with zero length, you can have an array with zero length.
12. Let's see--you can have an array of any type, right? Since
boolean is a type, boolean[] is a type. Since boolean[] is a type, boolean[][]
is a type. It's an array of boolean arrays. This is also called a
2D array. The code just declares a varible of that type--it doesn't
create one.
13. This will compile, but it will crash with a bounds exception. Arrays
count starting at 0, so the valid index of an array with length 7 is 0, 1, 2,
3, 4, 5, and 6. Seven is too big.
14. This will not compile. It's arr.length, not arr.length(). It
is not a method, it's a field.
15. This will work. Arrays are Objects and all objects have an equals()
method. It will not check the values though--it is the same as == with
arrays, so it's pretty useless. It will print true.
16. This will work. The L means it is a long and any decimal type
can be stored in any integer type.
float[] arr = new float[3];
17. This will not compile. The f makes it a float and any no integer
type can be stored in a decimal type.
18. This will compile, but it won't print the values of the array. It
will hello and the memory location of the array, which we probably don't care
about. Arrays are Objects, and we can concatenate any Object with a
String. It will call the Objects toString() method, which is generally
useless if no one wrote a good one for this class.
19* Since new int[2] is a valid array, we can access it's values. This is silly since the array is never stored and will be garbage collected. So this is really the same as saying int x = 0; This is never a need to write this when programming.
20* Again, new int[2] is a valid array, so this compiles. However, the
array is garbage collected, so this is rather pointless--the statement
effectively does nothing. This is never a need to write this when
programming.
21. This won't compile--we didn't specify the length of the array and all arrays must have lengths.
22. Hehehe. This will not compile. Remember that short and long
are reserved words. However, they would compile otherwise--the middle
line is fine.
23. This will compile. One billion is a valid int. However, it
requires 4 bytes * on billion = four gigabytes of heap space. My
computer only has 1 gigabytes of RAM so when my computer runs, it throws an
error and tells me I'm out of memory. If your computer is really hot (or
you are reading this in many years) it might run. You had better figure
out why you need an array that could (approximately) store the number of hairs
on the head of everyone in India as of 2008. It's also enough memory to
hold around 800 copies of the complete works of Shakespeare.
24. This won't compile--ten billion is too big to be an int, so the number itself won't compile. If you want a number bigger than an int can hold, you should add L on the end to make it a long.
25. The compiler checks types, not values. This will not compile since we
can't convert from a long to an int.
26. This will compile, but it will give you a bounds exception. It
should be < 5, not <=5.
27. This will compile and run. It's not true that the length is 4,
but that just means it evaluates to false and prints false.
28. You can't change the length of an array. You can think of it
as a final field. This will not compile.
29. This won't compile--it should be ==, not =. (This will attempt
to assign 4 to a.length, which we can't do).
30.This won't compile. The condition must have parenthesis around it.
31. This will compile. Notice that the braces are not needed since there
is only one statement. However, it will loop forever because i is never
updated.
32. This will compile--the compiler checks type, not values. However, it
will crash and throw a NegativeArraySizeException. It makes no sense to
have an array with -3 elements.
33* Notice the square brackets come after the name instead of after the type. This will compile--it is an alternate way to write an array. This is for compatibility with languages such as C and C++. The type is int[], not int. The name is arr, not arr[]. So this style seems confusing and inaccurate. Please do not use it.
34. This won't compile. The different pieces of a for loop need to be
separated by semicolons, not commas. Otherwise, it would work.
35. Just as it is okay to have x = x + 2; it's okay to use an array on both
sides of an assignment. Since the values start at the default of zero,
arr[3] is still zero.
36. Since arr[3] +=5; is merely a shortcut for arr[3] = arr[3] = 5; this
works. It's really the same question as the previous one. arr[3]
will end up at 9.
37. Notice that the increment goes down, so this will be an infinite
loop (Actually, it will continue until the integers wrap around to positives)
38. This will compile, but it's an infinite loop again. Notice the
sneaky semicolon after the while--it's really executing the "do nothing"
command forever.