Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What is the expected output of the following program? Assume valid definitions o

ID: 3623497 • Letter: W

Question

What is the expected output of the following program? Assume valid definitions of BarException and FooException. (PLEASE EXPLAIN ALL STEPS TO FINAL ANSWER IN DETAIL)

public class ExTest
{
   public static int m1(int v, int x) throws FooException
   {
          int val = 0;
          try
          {
                 x = m2(v,x-1);
                 System.out.println("456");
           }
           catch(FooException b)
           {
                  System.out.println("123");
            }
          return val;
     }

     public static int m2(int v, int x) throws FooException
     {
          if(x==0)
          {
               throw new FooException();
          }
          else
          {
                 return v/x;
          }
    }

public static void main(String[] args)
{
      try
      {
              System.out.println("abc");
             System.out.println(m1(4,1));
            System.out.println("cde");
       }
      catch (FooException e)
      {
           System.out.println("ghi");
      }
 }

}

Explanation / Answer

please rate - thanks

public class ExTest
{
public static int m1(int v, int x) throws FooException
{
int val = 0;                                                     val=0
try
{
x = m2(v,x-1);                                             calls m2 with 4,0
System.out.println("456");                         prints 456
}
catch(FooException b)
{
System.out.println("123");                          prints 123
}
return val;
}

public static int m2(int v, int x) throws FooException
{
if(x==0)                                                                x is 0 so throws FOO exception and returns to m1
{
throw new FooException();
}
else
{
return v/x;
}
}

public static void main(String[] args)
{
try
{
System.out.println("abc");                           outputs abc
System.out.println(m1(4,1));                       goes to m1
System.out.println("cde");                           outputs cde
}
catch (FooException e)
{
System.out.println("ghi");                         outputs ghi
}
}

}

will output

abc
what is output in m1
cde
ghi

lets see what is output in m1 - go to top to see trace

final output

abc
FOO exception taken - I'm assuming it is something that code continues
456
123
cde
ghi

if FOO exception stops execution

onlything output is

abc

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote