Java OCA OCP Practice Question 2658

Question

Consider the following program and choose the correct option that describes its output:

import java.util.concurrent.atomic.AtomicInteger;

class Main {/*w ww . ja v  a  2  s  .  co m*/
        public static void main(String []args) {
                if(null instanceof Object)
                        System.out.println("null is instance of Object");
                if(null instanceof AtomicInteger)
                        System.out.println("null is instance of AtomicInteger");
        }
}
a)      This program prints the following:
          null is instance of Object

b)     This program prints the following:
         null is instance of Object
         null is instance of AtomicInteger

c)       This program executes and terminates normally without printing any output in the
      console./*from   w w  w.  j ava2 s  .com*/

d)     This program throws a NullPointerException.

e)      This program will result in compiler error(s).


c)

Note

It is not a compiler error to check null with the instanceof operator.

However, if null is passed for the instanceof operator, it returns false.

Since both the condition checks fail, the program does not print any output in the console.




PreviousNext

Related