Practice questions of converting and casting primitives and objects

Home arrow Java Tutorials arrow SCJP(310-035) arrow Practice questions of converting and casting primitives and objects
Practice questions of converting and casting primitives and objects Print E-mail
Contributed by Joe   
Wednesday, 21 June 2006
Objects     Java 2™ Programmer Exam Cram™ 2 (Exam CX-310-035)

 Terms you'll need to understand:

  • Narrowing conversion
  • instanceof
  • Object hierarchy
  • Widening conversion
  • Interface references
 Question 1 of Java 2™ Programmer Exam Cram™ 2 (Exam CX-310-035)

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

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

Answer A is correct. This is perfectly good code. Answers B, C, and D are incorrect because array references are treated like other Objects by the instanceof operator.


 

Question 2 of Java 2™ Programmer Exam Cram™ 2 (Exam CX-310-035)

Here are three proposed alternatives to be used in a method to return false if the object reference x has the null value. Which statement will work?

  • A. if( x == null ) return false ;
  • B. if( x.equals( null ) ) return false ;
  • C. if( x instanceof null ) return false ;

Answer A is correct. It is the only way to check a reference for the null value. Answer B is incorrect because if x is null, there will not be an object whose equals method can be called. This statement would cause a NullPointerException at runtime when x is null. Answer C is incorrect because only a reference type, such as a class, interface, or array, can be the right operand of the instanceof operator.


 

Question 3 of Java 2™ Programmer Exam Cram™ 2 (Exam CX-310-035)

Here is the class hierarchy showing the java.awt.event.ActionEvent class family tree:

java.lang.Object
  |-- java.util.EventObject
        |-- java.awt.AWTEvent
              |-- java.awt.event.ActionEvent

Suppose you have the following code to count events and save the most recent event:

1. int evtCt = 0 ;
2. AWTEvent lastE ;
3. public void saveEvent( AWTEvent evt ){
4.   lastE = evt ;
5.   evtCt++ ;
6. }

Which of the following calls of saveEvent would run without causing an exception? [Check all correct answers.]

  • A. An AWTEvent object reference
  • B. An ActionEvent object reference
  • C. An EventObject object reference
  • D. A null value

Answers A, B, and D are correct. Answer A is correct because it matches the method signature. Answer B is correct because a subclass reference can always be cast up the hierarchy. Answer D is correct because any reference can be set to null. Answer C is incorrect because the reference cannot be cast to a subclass down the hierarchy.


Question 4 of Java 2™ Programmer Exam Cram™ 2 (Exam CX-310-035)

 Suppose you have two classes defined as follows:

class ApBase extends Object implements
   Runnable
class ApDerived extends ApBase implements
   Observer

Also suppose you have two variables created as follows:

ApBase aBase = new ApBase() ;
ApDerived aDer = new ApDerived() ;

Which of the following Java statements will compile and execute without error? [Check all correct answers.]

  • A. Runnable rn = aDer ;
  • B. Runnable rn2 = (Runnable) aBase ;
  • C. Observer ob = aBase ;
  • D. Observer ob2 = (Observer) aBase ;

Answers A and B are correct. Answer A is correct because the ApDerived class inherits from ApBase, which implements Runnable. Answer B is correct because the inserted cast (Runnable) is not needed but it does not cause a problem. Answer C fails to compile because the compiler can tell that the ApBase class does not implement Observer. Therefore, answer C is incorrect. Answer D is incorrect because it compiles but fails to execute. Because of the specific cast, the compiler thinks you know what you are doing, but the type of the aBase reference is checked when the statement executes and a ClassCastException is thrown.


Question 5 of Java 2™ Programmer Exam Cram™ 2 (Exam CX-310-035)

 What will be the result of trying to compile the following class?

1.  import java.io.* ;
2.  public class Test extends Object{
3.   public static void main(String args[] ){
4.    String[] names = new String[10] ;
5.    if( names instanceof Object[] )
            System.out.println("Obj[] true") ;
6.    if( names instanceof Object )
             System.out.println("Obj true ") ;
7.    double d = Math.sqrt( -1.0 );
                   // creates Double.NaN
8.    int x = (int) d ;
9.    System.out.println("d= " + d +
              " cast to int Value = " + x ) ;
10.   long lx = Long.MAX_VALUE ;
11.   float f = lx ;
12.   int[] nnn = new int[1000] ;
13.   Object obj = nnn ;
14.   Cloneable clobj = nnn ;
15.   Serializable sb = nnn ;
16.   int[] xxx = (int[])nnn.clone() ;
17. }
18. }

  • A. A compiler error is caused by the conversion from long to float in line 11.
  • B. A compiler error is caused by the conversion from int[] to Object in line 13.
  • C. A compiler error is caused by the conversion from int[] to Serializable in line 15.
  • D. The program compiles without error

Answer D is correct. All of the casts and conversions in this code are allowed by the compiler. Because the program compiles without error, answers A, B, and C are incorrect.

Related Articles

SCJP Tutorial

Last Updated ( Wednesday, 21 June 2006 )

  home              contact us

 

©2006-2008 DeveloperZone.biz   All rights reserved     powered by Mambo Designed by Siteground