ExamVeda
Login
Home
This question belongs to Java Program Exceptions
Exceptions
?

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();
      }
}

Answer & Solution
Correct Answer: Option C

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

Examveda
Question posted by Examveda
Community

Join the Discussion

2 Comments
Tishank Urwasha
Tishank Urwasha 3 years 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.
SWATI GABA
SWATI GABA 9 years ago
can we have a try block without it being followed by a catch block? Shouldn't the above code give an error?