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.
Join The Discussion
Comments ( 2 )
A. An error that occurs during runtime
B. An error that occurs during compilation
C. A warning issued by the compiler
D. A type of loop
What is the purpose of the "try" block in exception handling?
A. It catches exceptions and handles them
B. It specifies the exception type
C. It contains the code that might throw an exception
D. It specifies the exception message
In Java, can a method declare multiple exceptions using the "throws" keyword?
A. Only if the method is marked as "static"
B. Yes, a method can declare multiple exceptions separated by commas
C. Only if the exceptions are marked as "final"
D. Only if the method is marked as "static"
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?