Java OCA OCP Practice Question 763

Question

Which of the given lines can be inserted at // 1 of the following program ?

public class Main{     
   public static void main (String [] args){ 
       short s = 9; 
       //1 
    } 
} 

Select 2 options

A. Short k = new Short (9); 
   System.out.println (k instanceof Short); 

B. System.out.println (s instanceof Short); 

C. Short k = 9; //ww  w . j  a va2s  . c o m
   System.out.println ( k instanceof s); 
   
D. int i = 9; 
   System.out.println (s == i); 
   
E. Boolean b = s instanceof Number; 

F. Short k = 9; 
   Integer i = 9; 
   System.out.println (k == i); 
   
G. Integer i = 9; 
   System.out.println ( s == i ); 


Correct Options are  : D G

Note

For Option A.

9 is considered an int. This should be:

Short s = new Short (  (short) 9 ); 

For Option B.

The left operand of instanceof must be an object and not a primitive.

For Option C.

Right operand of instanceof must be a class name.

For Option D.

Any two integral primitives can be compared using == operator.

For Option E.

Left operand of instanceof must be an object and not a primitive.

For Option F.

This will not compile because k and i are referring to objects that have no IS-A relationship among themselves.




PreviousNext

Related