Java OCA OCP Practice Question 2654

Question

Consider the following program:

class Main {
        public static void main(String []args) {
                String str = null;
                System.out.println(str.valueOf(10));
        }
}

Which of the following statements correctly describes the behavior of this program?

  • a) This program will result in a compiler error.
  • b) This program will throw a NullPointerException.
  • c) This program will print 10 in console.
  • d) This program will print null in console.


c)

Note

The valueOf(int) method is a static method in String that returns the String representation of the integer value that is passed as its argument.

Since calling a static method does not require dereferencing the reference variable on which it is called, this program does not throw a NullPointerException.




PreviousNext

Related