revised: 10/05/03


on Arrays of Objects

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. What does the following statement do?     String glarch;

A.    It declares a reference variable glarch which is initialized to null.
B.    It constructs a String object named glarch.
C.    It constructs a String object which will contain the characters "glarch" .
D.    It declares an array of String objects named glarch.

2. What is the meaning of null?

A.    It is another name for zero.
B.    It is the String object that contains no characters.
C.    A reference variable that contains null is not referring to an object.
D.    It is a special value used to indicate an error condition.

3. What does the following statement do?     String[] widget;

A.    It declares an array of references to widget objects named String.
B.    It creates a String object named widget.
C.    It creates an array of length zero named widget.
D.    It declares a variable widget which may in the future hold a reference to an array of references to String objects but is initialized to null.

4. What is the difference between

String rats;

and

String[] rats;

A.    There is no difference; both declare rats to be a reference variable.
B.    The first declares rats to be a reference to a String object, the second declares rats to be a reference to an array of String references.
C.    The first constructs a single String object; the second constructs an array of String objects.
D.    The first initializes rats to null; the second initializes rats to an array of nulls.

5. What does the following statement do?

int[] values = new int[10] ;

A.    It declares   values   to be a reference to an array object and constructs an array object containing 10 integers which are initialized to zero.
B.    It declares   values   to be a reference to an array object, but initializes it to null.
C.    It declares   values   to be a reference to an array object which does not yet exist,
but will contain 10 zeros when it does.
D.    It declares   values   to be a reference to an array which contains 10 references to int variables.

6. What does the following statement do?

String[] names = new String[10] ;

A.    It declares   names   to be 10 String objects.
B.    It declares   names   to be a reference to an array of String references and constructs an array object which can contain references to 10 String objects.
C.    It declares   names   to be a reference to an array of String references and constructs an array object which contains references to the 10 String objects which it also constructs.
D.    It declares   names   to be a reference to an array of String references and constructs an array object which contains "10" in its first cell.

7. Given the declaration

String[] names = new String[10] ;

Which of the following statements puts a reference to the String "Hello" in the last cell of the array?

A.    names[0] = "Hello" ;
B.    names[10] = "Hello" ;
C.    names[9] = "Hello" ;
D.    String[ names.length-1 ] = "Hello" ;

8. Say that   names   has been declared

String[] names = new String[10] ;

and that further statements (not shown) have put String references into some of the cells.

Which of the following fragments prints out every String, but skips null references?

A.   
for ( int j = 0; names[j] != null; j++ )
    System.out.println( names[j] );
B.   
for ( int j = 0; j < names.length; j++ )
    System.out.println( names[j] );
C.   
for ( int j = 0; j < names.length && names[j] != null ; j++ )
    System.out.println( names[j] );
D.   
for ( int j = 0; j < names.length; j++ )
    if ( names[j] != null )
        System.out.println( names[j] );

9. Say that   names   has been declared

String[] names = new String[10] ;

and that further statements (not shown) have put String references into some of the cells.

Which of the following fragments counts the number of non-null cells in the array?

A.   
int count = 0;
for ( int j = 0; j < names.length; j++ )
    if ( names[j] != null )
        count++ ;
B.   
int j = 0;
int count = 0;
while ( names[ ++j ] != null )
  count++ ;
C.   
int count = 0;
while ( names[ count ] != null )
{
  count++ ;
}
D.   
int j = 0;
for ( int count = 0; count < names.length; count++ )
    if ( names[j] != null )
        j++ ;

10. Say that   names   has been declared

String[] names = new String[10] ;

and that further statements (not shown) have put String references into some of the cells.

Which of the following fragments prints out the cells of the array from last to first, skipping cells that contain null?

A.   
for ( int j = 0; j < names.length; j++ )
    if ( names[j] != null )
        System.out.println( names[j] );
B.   
for ( int j = names.length; j < names.length; j++ )
    if ( names[j] != null )
        System.out.println( names[j] );
C.   
for ( int j = names.length-1; j >= 0; j-- )
    if ( names[j] != null )
        System.out.println( names[j] );
D.   
for ( int j = names.length; j >= 0; j++ )
    if ( names[j] != null )
        System.out.println( names[j] );

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.