What will be the output of the following Java code? [code] class Output { public static void main(String args[])

Thê Hy

New member
What will be the output of the following Java code?
Code:
    class Output 
    {
        public static void main(String args[]) 
        {
           try
           {
               int a = 0;
               int b = 5;
               int c = b / a;
               System.out.print("Hello");
           }
           catch(Exception e) 
           {
               System.out.print("World");
           } 
           finally 
           {
               System.out.print("World");
           } 
        }
    }
A. Hello
B. World
C. HelloWOrld
D. WorldWorld
 
Answer: d
Explanation: finally block is always executed after tryblock, no matter exception is found or not. catch block is executed only when exception is found. Here divide by zero exception is found hence both catch and finally are executed.
Output:
Code:
$ javac Output.javac
java Output
WorldWorld
This section of our 1000+ Java MCQs focuses on Type interface in Java Programming Language.
 

Tìm kiếm

Back
Top