CS110 Practice Quiz 9

Arrays




  Choose the best alternative:
  1. An array consists of a collection of memory cells and is used to store data elements.    
  2. Each data element in an array can be accessed by an index.    
  3. A single array can store data elements of different data classes (int's, double's, char's, etc.)    
  4. An example of a correct array definition is char [] myArray = new char[6];    
  5. A example of an incorrect array definition is int myArray = new int [5];    

  6. The array below can be indexed by 0, 1, 2, 3, 4, 5.    
       int [] arr = new int [ 5 ];

  7. All elements of the array below are initialized to 0.    
       int [] arr1 = new int [5];

  8. Given the definition below, arr [3] =
       int [] arr = {1 ,2, 3, 4, 5};

  9. What is the index of the last element of the array below?    
       int [] hold = new int [55] ;

  10. What is the index of the first element of the array below?    
       int [] temp = {2, 7, 4, 4};

  11. Suppose that the index of the last element in an array is [25]. How many elements will the array store?    


    Questions 15 - 18 refer to the following code segment
    int [] arr = new int [4];
    for (int i = 0; i < 4; i++)
       if ( i % 2 == 0)
         arr[ i ] = 2 * i;
       else
         arr[ i ] = i;
  12. What value is assigned to arr[ 0 ]?    
  13. What value is assigned to arr[ 1 ]?    
  14. What value is assigned to arr[ 2 ]?    
  15. What value is assigned to arr[ 3 ]?    

Answers

  1. True
  2. True
  3. False
  4. True
  5. False
  6. False. The index 5 is out of bounds.
  7. True. NOTE: This is usually the case.
  8. 4
  9. 54
  10. 0
  11. 26
  12. 0
  13. 1
  14. 4
  15. 3