Java OCA OCP Practice Question 2707

Question

Consider the following program and predict its output:

class Main {/*from w  w  w .jav  a 2s .  co  m*/
        public static void main(String []args) {
               String str = null;

               switch(str) {  // #1
                       case "null":
                               System.out.println("null string"); // #2
                               break;
               }
        }
}
  • a) This program results in a compiler error in statement #1.
  • b) This program results in a compiler error in statement #2.
  • c) This program results in throwing a NullPointerException.
  • d) This program prints the following: null string.


c)

Note

If a null value is passed to a switch statement, it results in a NullPointerException.




PreviousNext

Related