Java OCA OCP Practice Question 3084

Question

Consider the following program:

public class Main {
       public static void main(String []args) {
              String str = null;
              if(str instanceof Object)                   // NULLCHK
                      System.out.println("str is Object");
              else
                      System.out.println("str is not Object");
       }
}

Which one of the following options correctly describes the behavior of this program?

  • a) This program will result in a compiler error in line marked with comment NULLCHK.
  • b) This program will result in a NullPointerException in line marked with comment NULLCHK.
  • c) When executed, this program will print the following: str is Object.
  • d) When executed, this program will print the following: str is not Object.


d)

Note

The variable str was declared but not instantiated; hence the instanceof operator returns false.




PreviousNext

Related