created: May 16, 1998

Quiz 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

Correct Answer Is:


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.

Correct Answer Is:


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 slot 0 to all other slots.
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.

Correct Answer Is:


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 slots in the array, it copies characters one by one from the String to the array.

Correct Answer Is:


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

Correct Answer Is:


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

Correct Answer Is:


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

Correct Answer Is:


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

Correct Answer Is:


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

Correct Answer Is:


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

Correct Answer Is:


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

Click here (If you have just 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 on reload to clear the old answers.)