Given the following code segment, what output would be generated? int rupees = 5
ID: 3844203 • Letter: G
Question
Given the following code segment, what output would be generated?
int rupees = 500;
String [] inventory = new String[10];
try
{
inventory[10] = "Deku Mask";
rupees -= 100;
}
catch (Exception ex)
{ rupees += 100; }
catch (IndexOutOfBoundsException ex2)
{ rupees -= 200; }
finally
{ rupees += 300; }
System.out.println(rupees);
600
800
900
700
ONE OF THE ANSWERS IS CORRECT
int rupees = 500;
String [] inventory = new String[10];
try
{
inventory[10] = "Deku Mask";
rupees -= 100;
}
catch (Exception ex)
{ rupees += 100; }
catch (IndexOutOfBoundsException ex2)
{ rupees -= 200; }
finally
{ rupees += 300; }
System.out.println(rupees);
600
800
900
700
ONE OF THE ANSWERS IS CORRECT
Explanation / Answer
First of all the questin gives you error as beacuse
catch (IndexOutOfBoundsException ex2)
{ rupees -= 200; }
catch (Exception ex)
{ rupees += 100; }
always catch Exception should be written in at the end if nor it gives you error stating that exception IndexOutOfBoundsException has already been caught
Answer is 600
why?
rupees is 500
in the try block inventory[10] is trying to access 10 index which is not yet defined
so it gives you error IndexOutOfBoundsException then rupees decremented by 200
rupees is 300
then after finally block executes where reupees is incremented by 300
rupees is 600
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.