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 (Exception ex) 
        {
            System.out.print("B"); 
        }  
        finally 
        {
            System.out.print("C"); 
        }  
        System.out.print("D"); 
    }  
    public static void badMethod() {} 
} 

  • A.AC
  • B.BC
  • C.ACD
  • D.ABCD

Answer: C

Correct answer: ACD

Here badMethod does nothing at all, so no exception is thrown: the try block completes and prints A, the catch is skipped, the finally prints C as it always does, and execution continues to print D. The output is ACD - the point being that finally runs whether or not anything went wrong.

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

Post your comment

Please log in or register to post a comment.