O'Reilly logo

Java 2™ Programmer Exam Cram™ 2 (Exam CX-310-035) by Marcus Green, Bill Brogden

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Exam Prep Practice Questions

Question 1 What will happen when you try to compile the following code?
1. public void printArray( Object x ){
2.   if( x instanceof int[] ){
3.     int[] n = (int[]) x ;
4.     for( int i = 0 ; i < n.length ; i++ ){
5.       System.out.println("integers = " +
            n[i] );}
6.   }
7.   if( x instanceof String[] ){
8.     System.out.println("Array of Strings") ;
9.   }
10. }
  • A. It compiles without error.

  • B. The compiler objects to line 2, which compares an Object with an array.

  • C. The compiler objects to line 3, which casts an Object to an array of int primitives.

  • D. The compiler objects to line 7, which compares an Object to an array of Objects.

A1: Answer A is correct. This is perfectly good code. Answers B, C, and D are incorrect because array ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required