nerdexam
Oracle

1Z0-851 · Question #263

Given a method that must ensure that its parameter is not null: 11. public void someMethod(Object value) { 12. // check for null value ... 20. System.out.println(value.getClass()); 21. } What…

The correct answer is D. if (value == null) { throw new IllegalArgumentException("value is null"); }. See the full explanation below for the reasoning.

Question

Given a method that must ensure that its parameter is not null: 11. public void someMethod(Object value) { 12. // check for null value ... 20. System.out.println(value.getClass()); 21. } What, inserted at line 12, is the appropriate way to handle a null value?

Options

  • Aassert value == null;
  • Bassert value != null, "value is null";
  • Cif (value == null) { throw new AssertionException("value is null"); }
  • Dif (value == null) { throw new IllegalArgumentException("value is null"); }

How the community answered

(42 responses)
  • A
    2% (1)
  • B
    10% (4)
  • C
    5% (2)
  • D
    83% (35)

Community Discussion

No community discussion yet for this question.

Full 1Z0-851 Practice