Examveda
Examveda

What will be the output?
class MyClass{
      public String test(){
            try{
                  System.out.print("One");
                  return "";
            }
            finally{
                  System.out.print("Two");
            }
      }
}

public class Test{
      public static void main(String args[]){
            MyClass m =  new MyClass();
            m.test();
      }
}

A. One

B. Two

C. One Two

D. Compilation Error

E. None of these

Answer: Option C

Solution(By Examveda Team)

Finally block will execute irrespective of return statement in try block.


This Question Belongs to Java Program >> Exceptions

Join The Discussion

Comments ( 2 )

  1. Tishank Urwasha
    Tishank Urwasha :
    9 months ago

    C. One Two
    program will execute successfully because there is no exception in try block so there is no need of catch block to handle any exception and finally will always executed with or without try and catch block.

  2. SWATI GABA
    SWATI GABA :
    7 years ago

    can we have a try block without it being followed by a catch block? Shouldn't the above code give an error?

Related Questions on Exceptions