And given that line 7 will assign the value 0, 1, or 2 to sw, which are true? (Choose all that apply.)
A. Compilation fails
B. A ClassCastException might be thrown
C. A StackOverflowError might be thrown
D. A NullPointerException might be thrown
E. An IllegalStateException might be thrown
F. The program might hang without ever completing
G. The program will always complete without exception
D and F are correct. Because i was not initialized, case 1 will throw an NPE. Case 0 will
initiate an endless loop, not a stack overflow. Case 2's downcast will not cause an exception.
Please explain this question ...which exception will b thrown and why....???
At Line 5., the Static Integer i is declared, but not initialized anywhere in the code.Default value would be null.
So, if the output of the expression at line 7. assigns the value for sw as 1, then case 1: would be executed where x is having null value and when the int value of x is tried to be automatically unboxed, it will throw a Null pointer exception.
For this case, D will be the answer.
If the sw value is assigned a value of 0 at line 7., then case 0: would be executed where x is set to 10 and the condition checked in the for loop will always be true.Since x is only incremented in this block, the condition x> 5 will always be true.So, this will be an endless loop.