Java OCA OCP Practice Question 2331

Question

Given the following code.

class Shape {} 
class Rectangle extends Shape implements Resizable{} 
class Shorts extends Shape {} 
interface Resizable {} 

class Main { /*  w  ww  .  j  a v  a 2  s  .c om*/
    public static void main(String sr[]) { 
        Rectangle s = new Rectangle(); 
        //INSERT CODE HERE 
        System.out.println(res); 
    } 
 } 

Which options will print true?.

a  boolean res = new Shape() instanceof Rectangle; 

b  boolean res = new Rectangle() instanceof Resizable; 

c  boolean res = null instanceof Main; 

d  Shape cloth = new Shape(); 
   Rectangle shirt = new Rectangle(); 
   boolean res = shirt instanceof cloth; 


b

Note

Option (a) prints false.

Option (c) prints false.

It doesn't fail to compile because null is a valid literal value that can be used for objects.

Option (d) fails to compile.

The instanceof operator must be followed by the name of an interface, class, or enum.




PreviousNext

Related