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();
}
}
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.
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.
can we have a try block without it being followed by a catch block? Shouldn't the above code give an error?