Preview

12 - Array Creation and Access

 1. An array is a block of memory that stores a collection of data items (elements) of __________ under one name.

  the same type

  the string object only

  a different data type

  integer type (requires an index)

 2. You can store a value in an array using an _______ (location in the array).

 3. In the following code listing the array is _____.
public class ArrayExample {
  public static void main(String[] args) {
    for (int i = 0; i < args.length; ++i) {
      System.out.println("Argument #" + (i + 1) + ": " + args[i]);
    }
  }
}

  Array

  Argument

  args

  classArray

 4. The above code shows an array of _________________. Specifically, those objects are the words that have been typed by the user at program launch.

  string objects

  void type

  length=0

  int type

 5. What is happening in this line?
System.out.println("Argument #" + (i + 1) + ": " + args[i]);

  A semi colon : is being added between each item.

  One contained object is accessed using its index in the array.

  The array is being turned into an integer array.

  The array is being lengthened by i+1.

 6. In Java, an array is an object. This object has a given type for the contained primitive types or objects (int, char, String, ...)

  TRUE

  FALSE

 7. At line 1, we instantiate an array of 10 items that get the default value (which is 1 for int). At lines 2 and 3, we instantiate array with a maximum value of 10.
array1 = new int[10];
int[] array0 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; //this only works in the declaration
array1 = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

  TRUE

  FALSE

 8. Arrays and lists in most programming languages start counting elements from the number 1, so the first element in an array in Java is at index 1.

  TRUE

  FALSE

 9. What is the correct declaration for an array of ints? Select from the options below.
// Declaration for a single int variable
int score;
// Declaration for an array of ints
???????????????/

  int: scores[]

  int[] scores;

  [scores];

  [int]:scores;

 10. The declarations actually create the array. Arrays are objects in Java, so any variable that declares an array holds the values in the array directly.

  FALSE

  TRUE

 11. If the array has not been created yet and you try to print the value of the variable, it will print _____(meaning it does not reference any object yet).

  the array name only

  null

  the length of the array

  error

 12. To actually create an array after declaring the variable, use the__________with the type and the size of the array (the number of elements it can hold).

  arr keyword

  double square brackets: []

  this keyword

  new keyword

 13. The size of an array is set at the time of creation and cannot be changed after that.
//declare an array variable
int[] highScores;
// create the array
highScores = new int[5];
// declare and create array in 1 step!
String[] names = new String[5];

  TRUE

  FALSE

 14. Which of the following creates an array of 10 doubles called prices?

  double[] prices;

  double[] prices = new double[10];

  double[10] prices = new double[];

  int[] prices = new int[10];

 15. Fill in the blanks for no. 4 below.
Array elements are initialized to default values 
like the following.

1) 0 for elements of type int

2) 0.0 for elements of type double

3) false for elements of type boolean

4) ____for elements of type String

  Text

  Java

  string objects

  null

 16. Another way to create an array is to use an initializer list. You can initialize (set) the values in the array to a list of values in ___________ when you create it, like below
int[ ] highScores = {99,98,98,88,68};
String[ ] names = {"Jamal", "Emily", "Destiny", "Mateo", "Sofia"};

 17. Which index is for the last element of an array called highScores?

  len(highScore)

  highScores.length

  highScores.length - 1

  highScore:length

 18. Since the first element in an array is at index 0 the last element is the length ______.

  itself

  plus 1

  multiplied by the length-1

  minus 1

 19. Fill in the blank with the code to access the cars array. String v =
String[] cars = {"Honda", "Volvo", "BMW"};

  cars[1:0]

  [cars]1

  cars(1)

  cars[1]

 20. Using an index value outside of 0 - (length-1) will result in an ArrayIndexOutOfBoundsException being thrown.

  FALSE

  TRUE

 21. Which statement best describes what is happening below on line 2.
int index = 3;
System.out.println(  highScores[index] );

  A variable is being used for the index

  The variable 'index' is being used as an int (incorrectly)

  The result of the array declaration will be null, as the index has no value

  An error will be generated as the index number is a string

 22. Arrays can store object reference data but never primitive data.

  TRUE

  FALSE

 23. The length of a two-dimensional array is the number of one-dimensional arrays it contains.

  TRUE

  FALSE

 24. In the following example, weirdTwoDimArray.length is 3, whereas weirdTwoDimArray[2].length is ___.
String[][] weirdTwoDimArray = {{"10", "11", "12"},
                               null,
                               {"20", "21", "22", "23", "24"}}; 

 25. What is the output of the code section below?
int nbItems = 10;
Object[] array3 = new Object[nbItems];
System.out.println(array3.length);