Chapter 6: Arrays
TRUE/FALSE
1. Many newer programming languages such as C++, Java, and C# use subscript 1 to access
the first element of the array.
Answer: False
2. You use subscripts 1 through 10 to access the elements in a ten element array.
Answer: False
3. Many newer programming languages such as C++, Java, and C# use the bracket notation
for arrays.
Answer: True
4. Declaring a named constant makes code easier to modify and understand.
Answer: True
5. A parallel array is an array that stores another array in each element.
Answer: False
6. Parallel arrays must contain the same data type.
Answer: False
7. You can improve the efficiency of a program by leaving a loop as soon as a match is found
in the array.
Answer: True
8. Arrays cannot be used if you need to search for a range of values.
Answer: False
9. When you have a five element array and use subscript 8, your subscript is said to be out of
bounds.
Answer: True
10. The for loop is a good tool when working with arrays because you frequently need to
process every element of an array from beginning to end.
Answer: True
MULTIPLE CHOICE
1. An array is a(n) ____ of values in computer memory.
a. list
b. accumulation
c. set
d. record
Answer: A
2. A(n) ____ is another name for a subscript.
a. pointer
b. sequence
c. index
d. place holder
Answer: C
3. The number of elements in an array is called the ____ of the array.
a. width
b. size
c. height
d. depth
Answer: B
4. All array elements have the same group ____.
a. subscript
b. name
c. memory location
d. value
Answer: B
5. Array elements all have the same ____ in common.
a. pointer
b. memory location
c. value
d. data type
Answer: D
6. Array subscripts are always a sequence of ____.
a. large numbers
b. characters
c. negative values
d. integers
Answer: D
7. In all languages, subscript values must be sequential ____.
a. characters
b. fractions
c. real numbers
d. integers
Answer: D
8. A program contains an array that holds all the names of the days of the week. Which of the
following is true?
a. The highest subscript is 6.
b. The highest subscript is 7.
c. The lowest subscript is 1.
d. The highest subscript is 12.
Answer: A
9. Providing array values is sometimes called ____.
a. creating the array
b. declaring the array
c. accumulating the array
d. populating the array
Answer: D
10. An array can be used to replace ____.
a. records
b. methods
c. nested decisions
d. loops
Answer: C
11. The true benefit of using an array lies in your ability to use a ____ as a subscript to the
array.
a. constant
b. loop
c. command
d. variable
Answer: D
12. Named ____ hold values that do not change during a program’s execution.
a. constants
b. variables
c. objects
d. items
Answer: A
13. Besides making your code easier to modify, using a ____ makes the code easier to
understand.
a. standard constant
b. named constant
c. literal constant
d. named variable
Answer: B
14. One advantage to using a named constant is that the statement becomes ____.
a. self-perpetuating
b. self-documenting
c. self-referencing
d. self-mitigating
Answer: B
15. When you search through a list from one end to the other, you are performing a ____.
a. linear search
b. binary search
c. quadratic search
d. single lookup
Answer: A
16. If you declare a variable to be Boolean, you can set its value to ____.
a. any number
b. true or false
c. any integer
d. 1 or -1
Answer: B
17. Which statement is true of arrays?
a. Only whole numbers can be used as array subscripts.
b. Only whole numbers can be stored in arrays.
c. Arrays cause more work for the programmer, but allow faster program execution.
d. Array elements cannot be reset after the array is declared.
Answer: A
18. A ____ relates parallel arrays.
a. superscript
b. key
c. subscript
d. postscript
Answer: C
19. Parallel arrays are most useful when value pairs have a(n) ____ relationship.
a. direct
b. indirect
c. linked
d. tiered
Answer: B
20. A ____ search starts looking in the middle of a sorted list, and then determines whether it
should continue higher or lower.
a. linear
b. binary
c. quadratic
d. single lookup
Answer: B
21. To search an array for a(n) ____ match, you can store either the highest or lowest value of
each range for comparison.
a. flag
b. index
c. subscript
d. range
Answer: D
22. Every array has a(n) ____ size.
a. infinite
b. finite
c. variable
d. constant
Answer: B
23. The number of bytes in an array is always a multiple of the number of ____ in an array.
a. subscripts
b. elements
c. iterators
d. indexes
Answer: B
24. In every programming language, when you access data stored in an array, it is important
to use a ____ containing a value that accesses memory occupied by the array.
a. superscript
b. subscript
c. key
d. condition
Answer: B
25. When a subscript is not within the range of acceptable subscripts, it is said to be ____.
a. a superscript
b. flagged
c. out of bounds
d. indexed
Answer: C
COMPLETION
1. Use a(n) ____________________ to indicate the position of a particular item within an
array.
Answer: subscript
2. All array elements have the same ____________________ name, but each individual
element also has a unique subscript indicating how far away it is from the first element.
Answer: group
3. Each array ____________________ is a single variable.
Answer: element
4. When working with arrays, you can use ____________________ in several ways: To hold
the size of an array, as the array values, and as a subscript .
Answer: constants
5. A(n) ____________________ is a variable set to indicate whether some event has
occurred.
Answer: flag
MATCHING
Match each term with a statement below.
1. A series of values in computer memory a. range of values
2. Each separate item in an array b. out of bounds
3. Providing array values c. flag
4. Can hold the size of an array d. parallel arrays
5. A variable that you set to indicate whether some event has occurred e. array
6. Starts looking in the middle of a sorted list, and then determines whether it should continue
higher or lower f. element
7. Any series of contiguous values g. for loop
8. A subscript that is not within the range of acceptable subscripts h. constant
9. A particularly convenient tool when working with arrays because you frequently need to
process every element of an array i. binary search
10. In this type of array, each element in one array is associated with the element in the same
relative position in the other array j. populating the array
1. Answer: E
2. Answer: F
3. Answer: J
4. Answer: H
5. Answer: C
6. Answer: I
7. Answer: A
8. Answer: B
9. Answer: G
10. Answer: D
SHORT ANSWER
1. Describe an array and provide an example of a real-life equivalent to an array.
Answer: An array is a series or list of variables in computer memory, all of which have the
same name and data type but are differentiated with special numbers called subscripts.
Usually, all the values in an array have something in common; for example, they might
represent a list of employee ID numbers or a list of prices for items a store sells. A subscript,
also called an index, is a number that indicates the position of a particular item within an
array.
Whenever you require multiple storage locations for objects, you are using a real-life
counterpart of a programming array. For example, if you store important papers in a series of
file folders and label each folder with a consecutive letter of the alphabet, then you are using
the equivalent of an array.
2. How do you reference the elements in an array?
Answer: All array elements have the same group name, but each individual element also has
a unique subscript indicating how far away it is from the first element. Therefore, any array’s
subscripts are always a sequence of integers such as 0 through 4 or 0 through 9.
Depending on the syntax rules of the programming language you use, you place the subscript
within parentheses or square brackets following the group name.
3. Why is it important to learn to use arrays correctly?
Answer: Learning to use arrays correctly can make many programming tasks far more
efficient and professional. When you understand how to use arrays, you will be able to
provide elegant solutions to problems that otherwise would require tedious programming
steps.
4. How can constants be used when working with arrays?
Answer: • To hold the size of an array
• As the array values
• As a subscript
5. How do you find a particular value in an array? Provide an example.
Answer: The technique for verifying that an item number exists involves setting a subscript to
0 and setting a flag variable to indicate that you have not yet determined whether the
customer’s order is valid. A flag is a variable that you set to indicate whether some event has
occurred; frequently it holds a true or false value. For example, you can set a string variable
named foundIt to “N”, indicating “No”. Then you compare the customer’s ordered item
number to the first item in the array. If the customer-ordered item matches the first item in the
array, you can set the flag variable to “Y”, or any other value that is not “N”. If the items do
not match, you increase the subscript and continue to look down the list of numbers stored in
the array. If you check all six valid item numbers and the customer item matches none of
them, then the flag variable foundIt still holds the value “N”. If the flag variable is “N” after
you have looked through the entire list, you can issue an error message indicating that no
match was ever found.
6. Why is it desirable to leave the loop as soon as a match is found?
Answer: Leaving the loop as soon as a match is found improves the program’s efficiency.
The larger the array, the more beneficial it becomes to exit the searching loop as soon as you
find the desired value.
7. Consider a mail-order business in which customers get a discount based on the quantity
they order. In writing a program to compute the discount based on a customer’s order
quantity, why is it not a good idea to construct an array with as many elements as a customer
might want to order, and store the appropriate discount associated with each number?
Answer: This approach has at least three drawbacks:
• It requires a very large array that uses a lot of memory.
• You must store the same value repeatedly. For example, each of the first nine elements
receives the same value, 0, and each of the next four elements receives the same value, 10.
• How do you know when you have enough array elements? Is a customer order quantity of
75 items enough? What if a customer orders 100 or 1,000 items? No matter how many
elements you place in the array, there’s always a chance that a customer will order more.
8. What is an advantage of using an array to match a value in a range of values?
Answer: When using an array to store range limits, you use a loop to make a series of
comparisons that would otherwise require many separate decisions. The program that
determines customer discount rates is written using fewer instructions than would be required
if you did not use an array, and modifications to your method will be easier to make in the
future.
9. What happens when a beginning programmer forgets that array subscripts start with 0?
Answer: A common error by beginning programmers is to forget that array subscripts start
with 0. If you assume that an array’s first subscript is 1, you will always be “off by one” in
your array manipulation.
10. What happens if a subscript value is negative or higher than the number of elements in an
array?
Answer: Some programming languages will stop execution of the program and issue an error
message. Other programming languages will not issue an error message but will access a
value in a memory location that is outside the area occupied by the array. That area might
contain garbage, or worse, it accidentally might contain the name of an incorrect month.
Either way, a logical error occurs.
Test Bank for Programming Logic and Design
Joyce Farrell
9781111969752, 9788131525906, 9781111825959