Quiz 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.

Correct Answer Is:


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.

Correct Answer Is:


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.

Correct Answer Is:


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.

Correct Answer Is:


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.

Correct Answer Is:


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 slot.

Correct Answer Is:


7. Given the declaration

String[] names = new String[10] ;

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

a.    names[0] = "Hello" ;
b.    names[10] = "Hello" ;
c.    names[9] = "Hello" ;
d.    String[ names.length-1 ] = "Hello" ;

Correct Answer Is:


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 slots.

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] );

Correct Answer Is:


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 slots.

Which of the following fragments counts the number of non-null slots 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++ ;

Correct Answer Is:


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 slots.

Which of the following fragments prints out the slots of the array from last to first, skipping slots 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] );

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.)