Java Exception Handling (original) (raw)
Which statement about final, finally, and finalize is correct?
- final is used to handle exceptions, finally is used for garbage collection, and finalize prevents modification.
- final makes a variable constant, finally ensures execution of cleanup code, and finalize is called before garbage collection.
- final is a method, finally is a keyword, and finalize is a block inside try-catch.
- final, finally, and finalize all serve the same purpose.
Which of the following blocks always executes, regardless of an exception occurring or not?
What will be the output of the following code?
Java `
public class Geeks { public static void main(String[] args) { try { System.out.println("Inside try"); throw new RuntimeException("Error"); } finally { System.out.println("Inside finally"); } } }
`
- Inside try Inside finally
- Inside try Inside finally RuntimeException
What will happen in the following code?
Java `
public class Geeks { static void method() throws Exception { throw new Exception("Error occurred"); }
public static void main(String[] args) {
method();
}
}
`
- Exception: Error occurred
- Program runs successfully
What is the difference between throw and throws?
- throw is used to declare exceptions, throws is used to throw exceptions
- throws is used to declare exceptions, throw is used to throw exceptions
- Both are used to declare exceptions
- Both are used to throw exceptions
What will be the output of the following program?
Java `
class CustomException extends Exception { public CustomException(String message) { super(message); } }
public class Geeks { public static void main(String[] args) { try { throw new CustomException("Custom error occurred"); } catch (CustomException e) { System.out.println(e.getMessage()); } } }
`
What happens when finalize() is called on an object?
- The object is immediately garbage collected.
- The garbage collector calls it before collecting the object.
- The object gets permanently deleted from memory.
- It prevents an object from being collected.
What is the output of the following code?
Java `
public class Geeks { public static void main(String[] args) { try { System.exit(0); } finally { System.out.println("Finally executed"); } } }
`
Which of the following is true about custom exceptions?
- Custom exceptions cannot extend Exception class
- Custom exceptions are always checked exceptions
- Custom exceptions can extend Exception or RuntimeException
- Custom exceptions do not require a constructor
Which of the following correctly defines a custom exception?
- Java
class MyException { public MyException(String message) { super(message); } }
- Java
class MyException extends RuntimeException { public MyException(String message) { super(message); } }
- Java
class MyException extends Throwable { public MyException(String message) { super(message); } }
- Java
class MyException extends Error { public MyException(String message) { super(message); } }
There are 10 questions to complete.
Take a part in the ongoing discussion