a) The subscript or index operator, [], can
take any number as its argument.
False. A subscript or index must be of type
int.
b) myName[1] accesses the first character of
the String myName.
False. It accesses the
second character; myName[0] accesses the first character.
c) If a subscript of a Lookout string or array class goes out of bounds,
Lookout yells whoa!
False. Lookout will not yell whoa, but it will generate
code that will generate an out of bounds error message at run-time.
d) If a subscript of a Java string or array goes out of bounds, the Java compiler
will yell whoa!
False. Java interpreters will check if array indices go out
of bounds at run time though.
e) String is more abstract than C strings
(which is good!), but a little less efficient.
True. String encapsulate low level C strings and provides run-time subscript bounds checking.
Bounds checking does have a cost which programs using C strings don't pay.
f) IntArray ia(10,20) constructs an IntArray whose lower bound is 10 and upper is 20.
False. That might be an interesting way to construct arrays, but the Lookout
class library keeps things simple. The constructor IntArray(int sz,int init=0)
assumes that the lower bound is zero and the upper bound is sz-1.
g) If quizScores is a partially filled array,
then quizScores.size() is not the right
upper bound for a definite loop iterating through all its elements.
True.
Since a partially filled array does not have legitimate values in all its
elements, a loop should use an upper bound which subscripts the last legitimate
value, size(), the last element.
h) Good rule of thumb: initialize a definite loop counter just before
entering the loop.
True. Otherwise the definite loop counter will be
undefined upon entering the loop and incrementing it will give an unpredictable
result.
i) "Real" Java programmers prefer for loops
because then they don't have to remember the aforementioned rule.
True. For
example: for (i=0; i < last; i++) The first part of the for loop, i=0,
initializes the counter and the last part, i++, increments it, making a for loop
a compact way to represent definite counter loops. But such loops can be
implemented just as effectively by while loops, once you learn the definite loop
pattern.
j) If LinearSearch() fails to find a key in
an array, a run-time error occurs.
False. Instead, this function returns
NotFound, a const value set to -1, to indicate this condition. The caller can
test for this value and act accordingly.
k) LinearSearch() is fine for little arrays,
but a dog for telephone books!
True. Searching every single element
from the beginning is inefficient for large arrays.
l) SelectionSort() swaps the smallest value in an array with the first element of the array. True, assuming it's sorting in ascending order. This swap arranges the smallest element to the front of the array; the sort then swaps the next smallest element to the second position, and so forth.
m) BinarySearch() works by examining the
binary values of array elements.
False. It divides a previously sorted array
in half, then in half again, and so forth, until it finds the desired element.
The "binary" in the name has to do with division by two.
n) LinearSearch() and BinarySearch(), as implemented in the book, only
search for int values.
True. But it's not
hard to modify these algorithms to work with other element types. Even more
generality can be achieved using templates.
o) A StringArray is a two-dimensional array.
True. It's an array of String and each String is in turn an array of char.
p) The expression WordList[2][1] gets the
element at column 2, row 1.
False. It's row-major, so this expression gets
the element at row 2, column 1. Bear in mind that there is also a row 0 and
column 0.
q) In Java, access to two-dimensional arrays is row-major. True, as pointed out by the answer to the previous question.