Question #63999

what is the output of the following code segment? show your work
public int DoIt(){
int []a=new int[10];
int temp=a[2];
try{
String a=null;
String b=a.toUpperCase();
System.out.println("\n departing DoIt()");
System.out.println("\n in try zone");
}
catch (ArithmeticException e)
{
System.out.println("\n caugh arithmetic exception inside DoIt()");
}
catch (Exception w)
{
System.out.println("\n caugh exception inside doit()");
throw new IndexOutOfBoundsException("temp=a[12]");
}
finally{
System.out.println("\n finally inside doit()");
}
System.out.println("\n after try ::doit()");
return temp;
}
void one()
{
try
{
int x=DoIt();
System.out.println("\n after calling DoIt()");
}
catch (ArithmeticException e)
{
System.out.println("\n caugh arithmetic exception ");
}
catch (Exception w)
{
System.out.println("\n caugh exception ");
}
finally{
System.out.println("\n done doing it");
}
System.out.println("\n outta here...bye");
}

Expert's answer

Answer on Question #63999, Programming & Computer Science / Java | JSP | JSF


public class Main {
    public static void main(String[] args)
    {
    }
    public int DoIt(){
        int [] a = new int[10];
        int temp = a[2];
        try
        {
            String a1 = null;
            String b = a1.toUpperCase();
            System.out.println("\n departing DoIt()");
            System.out.println("\n in try zone");
        }
        catch (ArithmeticException e)
        {
            System.out.println("\n caught arithmetic exception inside DoIt()");
        }
        catch (Exception w)
        {
            System.out.println("\n caught exception inside doIt()");
            throw new IndexOutOfBoundsException("temp = a[12]");
        }
        finally
        {
            System.out.println("\n finally inside DoIt()");
        }
        System.out.println("\n after try ::doIt()");
        return temp;
    }
    void one()
    {
        try
        {
            int x = DoIt();
            System.out.println("\n after calling DoIt()");
        }
        catch (ArithmeticException e)
        {
            System.out.println("\n caught arithmetic exception ");
        }
    }
}
catch (Exception w)
{
    System.out.println("\n caught exception ");
}
finally
{
    System.out.println("\n done doing it");
}
System.out.println("\n outta here...bye");
}


**Error: (13, 20) java: variable a is already defined in method DoIt()**

Pic. 1 Results

We can't use two different variables with the same name.

Answer provided by www.AssignmentExpert.com

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS