Machine-translated by Google. Questions are authored in English.
Top

Discussion

What will be the output of the program?

public class X 
{  
    public static void main(String [] args) 
    {
        try 
        {
            badMethod();  
            System.out.print("A");  
        } 
        catch (RuntimeException ex) /* Line 10 */
        { 
            System.out.print("B"); 
        } 
        catch (Exception ex1) 
        { 
            System.out.print("C"); 
        } 
        finally 
        {
            System.out.print("D"); 
        } 
        System.out.print("E"); 
    } 
    public static void badMethod() 
    { 
        throw new RuntimeException(); 
    } 
}

  • A.BD
  • B.BCD
  • C.BDE
  • D.BCDE

Answer: C

Correct answer: BDE

badMethod throws, so the print("A") after it never runs. The first matching catch handles it and prints B - and only one catch block ever runs, which is why no C appears. The finally then prints D, and because the exception was handled rather than propagated, execution continues past the whole statement and prints E. The output is BDE.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.