## Arrays

Published:  June 30, 2007
By Richard G. Baldwin

File: Alice0180PracticeTest.htm

## Questions

1.  After the execution of the code in Listing 16, what does idx contain?

Listing 1. 180-116.
 ```public void main ( ) { Number[] myArray = [5,7,3,10,2]; Number x = 0 ; Number y = 0 ; Number idx = 0 ; for (int index=0;index < (myArray.length);index++){ x.set( value , ( myArray [ index ] ) ); if ( ( x > y ) ) { idx .set( value , index ); y .set( value , x ); } else { Do Nothing } } print( idx ); print( y ); }```

• A. idx contains the largest element in the array.
• B. idx contains the smallest element in the array.
• C. idx contains the subscript of the largest element in the array.
• D. idx contains the subscript of the smallest element in the array.

2.  If score is an array containing 5 values, in the following order: 80, 75, 90, 60, 100, what is the content of score[1] ?

• A. 0
• B. 80
• C. 75
• D. 100

3.  An array is a collection of values:

• A. with the same name, but with varying data types.
• B. with varying names, but each with an identical data type shared by all values.
• C. with a common name and identical data types, but each with a unique index associated with it, which is used to uniquely identify it.
• D. defined at run time.

4.  Which of the following would be used to store a list of 50 test scores?

• A. a variable of type String
• C. a variable of type Number
• B. a class
• D. an array

5.  In an array, we access the data value we are searching for by its

• A. field name.
• B. ordinal value.
• C. subscript or index
• D. data value.

6.  True or False:  Arrays can be used as data structures or containers for data of any valid Alice type.

7.  True or False:  Whenever you need to use the data contained in an array in your program, you drag the array tile up from the bottom of the code edit pane and drop it into the edit pane.

8.  True or False:  If the declared type of an Alice array is Object, the individual objects that are stored in the array can include a variety of different kinds of objects such as penguins, monkeys, lemurs, ladybugs, kangaroos, the camera, the light, the ground, etc.

9.  True or False:  You can mix elements of type Boolean with type Number in an Alice array.

10.  True or False:  Each variable in an array is commonly called an element.

11.  True or False:  Each element in an array has a subscript or index that differentiates it from every other element in the array.  (Some people call it a subscript while others call it an index.)

12.  True or False:  An array index is negative integer that indicates the position of a particular element relative to the first element in the array.  The first element in the array always has an index value of 0.

13.  True or False:  It is not necessary to declare an array before you can use it.  Arrays are created automatically by the Alice runtime system when needed.

14.  True or False:  You specify the number of elements (the size) of an array when you declare it.  Once created, the size of an array cannot be changed at runtime.

15.  True or False:  If the size of the array is N, the index values for the elements in the array always extend from 0 to N inclusive

16True or False:  You access an individual element in an array by:

• Calling out the name of the array and
• Specifying the index of the element of interest inside a pair of square brackets ([]).

17True or False:  If you attempt to access an array element at an index value that is equal to or larger than the size of the array, the program will abort with an error at runtime.  Similarly, if you attempt to access an array element using a negative index, the program will abort with an error at runtime.

18True or False:  Once an array element is populated, it is impossible to remove the contents of the element.  If you don't want those contents to be there, you must overwrite those contents with something else.

Copyright 2007, Richard G. Baldwin.  Faculty and staff of public and private non-profit educational institutions are granted a license to reproduce and to use this material for purposes consistent with the teaching process.  This license does not extend to commercial ventures.  Otherwise, reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

The following image is the splash screen from Alice 2.0, and is the property of the developers of Alice at Carnegie Mellon.

## Answers and Explanations

True

True

True

### Explanation 16

Back to Question 16

False

True

False

False

True

True

False

True

False

True

### Explanation 6

C. subscript or index

D. an array

### Explanation 4

C. with a common name and identical data types, but each with a unique index associated with it, which is used to uniquely identify it.

C. 75