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

Get Java 2™ Programmer Exam Cram™ 2 (Exam CX-310-035) now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.