Given the code fragment:
What is the output?
Given the code fragment:
What is the output?
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.
C is correct, Switch use a .hascode so when is executed thown NUllPointR
Tested: Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.hashCode()" because "<local1>" is null.
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.
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.hashCode()" because "Question162.s" is null C is correct
C as String s is null it will throw nullpointer exception