Test: Computer Science

Consider the following code:

Object[] objects = new Object[20];

for(int i = 0; i < objects.length; i++) {

     switch(i % 4) {

     case 0:

          objects[i] = new Integer(i + 3);

          break;

     case 1:

          objects[i] = "This val: " + i;

          break;

     case 2:

          objects[i] = new Double(i * 4.4);

          break;

     case 3:

          objects[i] = "That val: " + (i*12);

          break;

     }

}

String s = (String)objects[8];

System.out.println(s);

1.

What is the error in the code above?

There is an array overrun.

There will be a NullPointerException.

There are no errors.

There will be a ClassCastException thrown.

You cannot assign various types to the array in that manner.

1/2 questions

0%
Learning Tools by Varsity Tutors