Exam 1z0-819 All QuestionsBrowse all questions from this exam
Question 162

Given the code fragment:

What is the output?

    Correct Answer: C

    The output of the code will be a NullPointerException at runtime. This is because the variable 's' is declared but not initialized, so its value is null. When the switch statement attempts to switch on the value of 's', it invokes the hashCode method on 's' to determine which case to execute. Since 's' is null, invoking hashCode() on it results in a NullPointerException.

Discussion
d7bb0b2Option: C

C is correct, Switch use a .hascode so when is executed thown NUllPointR

OmnisumemOption: C

Tested: Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.hashCode()" because "<local1>" is null.

[Removed]Option: C

C. is correct None of the actual cases or default statements will cause a NullPointerException. But using a String with value null in the 'switch(s)' clause.

StavokOption: C

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.hashCode()" because "Question162.s" is null C is correct

KiraguJohnOption: C

C as String s is null it will throw nullpointer exception