Created: May 16, 1998; revised: 10/05/03


on Array Parameters

This is a practice quiz. The results are not recorded anywhere and do not affect your grade. The questions on this quiz might not appear in any quiz or test that does count toward your grade.

Instructions: For each question, choose the single best answer. Make your choice by clicking on its button. You can change your answers at any time. When the quiz is graded, the correct answers will appear in the box after each question.



1. Java uses call by value. What is the value that is being passed into routine by the method call in the following?

double[] rats = {1.2, 3.4, 5.6};

routine( rats );

A.    A copy of the array rats.
B.    The value of the elements of rats.
C.    A reference to the array object rats.
D.    1.2

2. What type parameter must the following method be called with?

int myMethod ( double[] ar )
{
  . . . .
}

A.    An empty double array.
B.    A reference to an array that contains elements of type double.
C.    A reference to an array that contains zero or more elements of type int.
D.    An array of any length that contains double and must be named ar.

3. What does the following method do?

void spread ( int[] values )
{
  for ( int index= 1 ; index < values.length ; index++  )
    values[index] = values[0];
}

A.    It changes an array by copying the element in cell 0 to all other cells.
B.    It changes an array by making every element the same as the value of its index.
C.    It changes an array by making every element zero.
D.    It makes a change to the formal parameter but does not make a change to the caller's array.

4. What does the following method do?

void blur ( char[] z, String st )
{
  if ( z.length < st.length() ) return;

  for ( int j=0; j < st.length; j++ )
    z[j] = st.charAt( j );
    
}

A.    It determines if the array contains the same characters as the String.
B.    It copies characters from the array to the String.
C.    It creates a new array that contains the same characters as the String.
D.    If there are enough cells in the array, it copies characters one by one from the String to the array.

5. What is the output of the following?

class ChangeIt
{
  static void doIt( int[] z )
  {
    z[0] = 0;
  }    
}

class TestIt
{
  public static void main ( String[] args )
  {
    int[] myArray = {1, 2, 3, 4, 5} ;

    ChangeIt.doIt( myArray );

    for (int j=0; j<myArray.length; j++ )
      System.out.print( myArray[j] + " " ) ;
  }
}

A.    1 2 3 4 5
B.    0 1 2 3 4
C.    0 2 3 4 5
D.    2 3 4 5 0

6. What is the output of the following?

class ChangeIt
{
  static void doIt( int[] z )
  {
    z[0] = z[ z.length-1 ];
  }    
}

class TestIt
{
  public static void main ( String[] args )
  {
    int myArray = {1, 2, 3, 4, 5} ;

    ChangeIt.doIt( myArray );

    for (int j=0; j<myArray.length; j++ )
      System.out.print( myArray[j] + " " ) ;
  }
}

A.    1 2 3 4 5
B.    0 2 3 4 5
C.    5 2 3 4 1
D.    5 2 3 4 5

7. What is the output of the following?

class ChangeIt
{
  static void doIt( int[] z )
  {
    int temp = z[ z.length-1 ] ;
    z[0] = temp;
  }    
}

class TestIt
{
  public static void main ( String[] args )
  {
    int myArray = {1, 2, 3, 4, 5} ;

    ChangeIt.doIt( myArray );

    for (int j=0; j<myArray.length; j++ )
      System.out.print( myArray[j] + " " ) ;
  }
}

A.    1 2 3 4 5
B.    0 2 3 4 5
C.    5 2 3 4 1
D.    5 2 3 4 5

8. What is the output of the following?

class LowHighSwap
{
  static void doIt( int[] z )
  {
    int temp = z[ z.length-1 ] ;
    z[ z.length-1 ] = z[0] ;
    z[0] = temp;
  }    
}

class TestIt
{
  public static void main ( String[] args )
  {
    int[] myArray = {1, 2, 3, 4, 5} ;

    LowHighSwap.doIt( myArray );

    for (int j=0; j<myArray.length; j++ )
      System.out.print( myArray[j] + " " ) ;
  }
}

A.    1 2 3 4 5
B.    5 2 3 4 1
C.    1 2 3 4 1
D.    5 2 3 4 5

9. What is the output of the following?

class ChangeIt
{
  static void doIt( int[] z )
  {
    int[] A = z ;
    A[0] = 99;
  }    
}

class TestIt
{
  public static void main ( String[] args )
  {
    int myArray = {1, 2, 3, 4, 5} ;

    ChangeIt.doIt( myArray );

    for (int j=0; j<myArray.length; j++ )
      System.out.print( myArray[j] + " " ) ;
  }
}

A.    1 2 3 4 5
B.    99 2 3 4 5
C.    0 2 3 4 5
D.    99 99 99 99 99

10. What is the output of the following?

class ChangeIt
{
  static void doIt( int[] z )
  {
    z = null ;
  }    
}

class TestIt
{
  public static void main ( String[] args )
  {
    int[] myArray = {1, 2, 3, 4, 5} ;

    ChangeIt.doIt( myArray );

    for (int j=0; j<myArray.length; j++ )
      System.out.print( myArray[j] + " " ) ;
  }
}

A.    1 2 3 4 5
B.    Nothing will be printed.
C.    The program will halt with a run time error.
D.    0 0 0 0 0

The number you got right:       Percent Correct:       Letter Grade:   


Click here

If you have returned here from another page, or have re-loaded this page, you will need to click again on each of your choices for the grading program to work correctly. You may want to press the SHIFT KEY while clicking to clear the old answers.