Data Structures Unit Test ANSWER KEY

ICS 3M                                                                         Name: ________________________

 / 32

A.  True / False  (5 marks)

1)
The proper way to declare an array of integers called img is:
new int img[];
a) True
b) False

2)
Once declared an array cannot be resized without writing additional functions.
a) True
b) False

3)
An array is good for holding the name, social security number, age, and income of one individual.
a) True
b) False

4)
An array is NOT good for holding the temperature readings taken every hour throughout a day.
a) True
b) False

5)

To be effective, a binary search must be performed on a sorted array

a) True
b) False










B. Multiple Choice - Circle the most appropriate answer given. (5 marks)

6)

What are the legal indexes for the array ar, given the following declaration: 

 

int[] ar = {2, 4, 6, 8 }

 

a) 0, 1, 2, 3 b) 1, 2, 3, 4
c) 2, 4, 6, 8 d) 0, 2, 4, 6

7)

What is the output of the following code fragment ( indicates a space):

 

int[] ar = {2, 4, 6, 8 };

 

ar[0] = 23;

ar[3] = ar[1];

 

 System.out.println( ar[0] + "" + ar[3] );

 

a) 232 b) 28
c) 31 d) 234

8)

What is the output of the following code fragment:

 

int[] zip = new int[5];

 

zip[0] = 7;

zip[1] = 3;

zip[2] = 4;

zip[3] = 1;

zip[4] = 9;

 

 System.out.println( zip[ 2 + 1 ] );

 
a) 4 3 b) 3 7
c) 4 d) 1

9)

How many objects are present after the following code fragment has executed?

 

double[] ann = new double[ 7 ];

double[] bob;

 

bob = ann;

 

a) 2 b) 1
c) 14 d) 7
10)

Examine the following program fragment, what does the fragment write to the monitor? :

 

  int[] array = { 1, 4, 3, 6, 8, 2, 5};

 int what = array[0];

 

 // scan the array

 for ( int index=0; index < array.length; index++ )

 {

   if ( array[ index ] > what )  

     what = array[ index ]; 

 }

 System.out.println( what );

 

a) 1 b) 5
c) 8 d) 1 4 3 6 8 2 5

 

C.  Fill in the Blanks - Fill in the blanks with the most appropriate answer from the list below:  (7 marks)

 
Word Bank
null index-- egArray.length-1
array [index] = 2*array; index array [index]  = 2*index;
names[9] names[10] egArray.length
index>=0 index>0 void

11)

A(n) index represents a position of a member in an array.


12)
A reference variable that contains null is not referring to anything.

13)

Fill in the blank so that the value "Jane Doe" is placed in the last slot in the array.

String [] names = new String[10];

names [9] = "Jane Doe";










14)

Fill in the blank in the following code fragment so that each element of the array is assigned twice the value of its index.

 

  int[] array = new int[10];

 

  // scan the array

  for ( int index=0; index < array.length; index++ )

  {

      array [index]  = 2*index;

  }

 


15)

Fill in the blanks of the following code fragment so that the elements of the array are printed in reverse order, starting with the last element.

 

    int[] egArray = { 2, 4, 6, 8, 10, 1, 3, 5, 7, 9 };

 

    for ( int index= egArray.length-1 ; index>=0 ; index-- )

      System.out.print(  egArray[ index ] + "  "  );


 

D.  Matching (5 marks)

Match up the code fragment with the correct output.

16)    d  

int[] egArray = { 2, 4, 6, 8, 10, 1, 3, 5, 7, 9 };
for ( int index= 0 ; index < 5 ; index++ )
System.out.print( egArray[ index ] + "," );

17)    e  

int[] egArray = { 2, 4, 6, 8, 10, 1, 3, 5, 7, 9 };
for ( int index= 0 ; index < egArray.length ; index++ )
System.out.print( egArray[ index ] + "," );

18)    b  

int[] egArray = { 2, 4, 6, 8, 10, 1, 3, 5, 7, 9 };
for ( int index= 0 ; index < egArray.length ; index = index + 2 )
System.out.print( egArray[ index ] + "," );

19)    c  

int[] egArray = { 2, 4, 6, 8, 10, 1, 3, 5, 7, 9 };
for ( int index= 5 ; index < egArray.length ; index = index +1 ) System.out.print( egArray[ index ] + "," );

20)    a  

int[] egArray = { 2, 4, 6, 8, 10, 1, 3, 5, 7, 9 };
for ( int index= egArray.length ; index > 0 ; index = index -- ) System.out.print( egArray[ index ] + "," );

a. 9,7,5,3,1,10,8,6,4,
b. 2,6,10,3,7,
c. 1,3,5,7,9,
d. 2,4,6,8,10,
e. 2,4,6,8,10,1,3,5,7,9,







E.  Short Answer - Answer each question to the best of your ability, any answer will be considered for partial marks.  (10 marks)

21)
Why does the binary search require that the array be sorted for it to be effective?  (1 mark)
- binary search continually divides the array in half comparing to see if the value fits in the upper or lower
half, it must be in order so that when the array is divided, all the lower numbers are in one half, the higher
numbers are in the other half.
 

22)
What changes would you have to make to the sequential search if the array is to be in descending order?  (1 mark)
- no changes will be necessary.
 
 
 

23)

What changes would you need to make to the binary search if the array is to be in descending order?  (1 mark)

- change the comparison operator from < to > when comparing the middle point to the item that is
being searched for in the array.
 
 

24)

Complete the following program so that it computes the sum of the numbers in the array and displays the answer to the user.  (3 marks)

class Exercise1
{
  public static void main ( String[] args )
  {
    int[] val = {0, 1, 2, 3};

    for (int index = 0; index < val.length; index++)
    sum = sum + val[index];
    system.out.println("The sum of all values in the array is: " + sum);
}}

25)

Create a new array called twice. Fill the array with the values from the once array, but doubled.  Print out the doubled array. (4 marks)

class doubledArray
{
  public static void main ( String[] args )
  {
    int[] once = {13, -4, 82, 17}; 
    
    System.out.println( "Original Array: "
        + once[0] + " " + once[1] + " " + once[2] + " " + once[3] );

    int[] twice = new int[once.length];
    for (int index = 0; index <twice.length; index++)
    twice[index] = (once[index]*2);
    System.out.println( "Doubled Array: "
        + twice[0] + " " + twice[1] + " " + twice[2] + " " + twice[3] );
   }
}