1. Which keyword is used to handle an exception in Java?
Answer:
Explanation:
The 'catch' block is used to handle the exception. It contains the code to handle the exception that might have occurred in the try block.
2. Which of these is a superclass of all exception types in Java?
Answer:
Explanation:
Throwable is the superclass of all errors and exceptions in the Java language.
3. What is the purpose of the finally block in exception handling?
Answer:
Explanation:
The finally block is executed after the try and catch blocks, regardless of whether an exception was caught or not.
4. Which of these is not a checked exception?
Answer:
Explanation:
ArithmeticException is an unchecked exception, as it is derived from RuntimeException.
5. Which statement is true about checked exceptions?
Answer:
Explanation:
Checked exceptions are checked at compile-time. The compiler ensures that such exceptions are either caught or declared in the method signature.
6. What is the purpose of the throw keyword in Java?
Answer:
Explanation:
The throw keyword is used to explicitly throw an exception, either a newly instantiated one or an exception that was caught.
7. What does the throws keyword do in a method signature?
Answer:
Explanation:
The throws keyword in a method signature is used to declare one or more exceptions that might be thrown by the method.
8. Which of these exception types is used to indicate a problem with code logic?
Answer:
Explanation:
NullPointerException is thrown when an application attempts to use null where an object is required, indicating a logic error or a bug.
9. What is a custom exception in Java?
Answer:
Explanation:
A custom exception is an exception defined by the user for specific application needs, typically extending Exception or RuntimeException.
10. Which method is commonly used to print the stack trace of an exception?
Answer:
Explanation:
printStackTrace() prints the stack trace of the Throwable object to the standard error stream, which is helpful for debugging.
11. What happens if an exception is not caught in the catch block?
Answer:
Explanation:
If an exception is not caught in the catch block, it is propagated back to the caller method.
12. Which of these is a runtime exception?
Answer:
Explanation:
NumberFormatException is an unchecked exception and is a subclass of RuntimeException.
13. What is the correct way to declare a method that might throw an IOException?
Answer:
Explanation:
The correct syntax to declare a method that might throw an IOException is to use the throws keyword followed by the exception type.
14. Can a try block exist without a catch block?
Answer:
Explanation:
A try block can exist without a catch block only if it is followed by a finally block.
15. What type of exceptions do Errors represent?
Answer:
Explanation:
Errors represent severe problems like hardware failure that applications should not attempt to catch.
16. What is the main reason to use a finally block in Java?
Answer:
Explanation:
The finally block is used for cleanup code that needs to run regardless of whether an exception occurred or not.
17. What will happen if both try and catch blocks have return statements in Java?
Answer:
Explanation:
If the finally block has a return statement, it will override the return statements from both the try and catch blocks.
18. How can multiple exceptions be caught in a single catch block in Java?
Answer:
Explanation:
Multiple exceptions can be caught in a single catch block using the pipe character (|) to separate the exception types.
19. What is exception chaining in Java?
Answer:
Explanation:
Exception chaining in Java is a technique where one exception causes another exception. It can be implemented by setting a cause in an exception.
20. Which keyword is used to manually throw an exception in Java?
Answer:
Explanation:
The throw keyword is used to manually throw an exception in Java.
21. What is the difference between final, finally, and finalize in Java?
Answer:
Explanation:
'final' is a keyword used to apply restrictions, 'finally' is a block that executes code regardless of exceptions, and 'finalize' is a method called by the garbage collector before an object is destroyed.
22. In which scenario is a ClassNotFoundException thrown?
Answer:
Explanation:
ClassNotFoundException is thrown when an application tries to load a class through its string name but no definition for the class with the specified name could be found.
23. Which exception is thrown when an attempt is made to access an illegal array index?
Answer:
Explanation:
ArrayIndexOutOfBoundsException is thrown to indicate that an array has been accessed with an illegal index.
24. What does the term "exception propagation" refer to in Java?
Answer:
Explanation:
Exception propagation refers to the process where an exception is automatically forwarded up the call stack if not caught in the current method.
25. What is the result of compiling and running a Java program with a try block having only a return statement and no catch or finally block?
Answer:
Explanation:
A try block without a catch or finally block is not valid in Java and will result in a compilation error.
26. Which exception is thrown when an application tries to create an instance of a class using the newInstance method of class Class, but the specified class object cannot be instantiated?
Answer:
Explanation:
InstantiationException is thrown to indicate that the application tried to create an instance of a class using the newInstance method in class Class, but the specified class object cannot be instantiated because it is an interface or is an abstract class.
27. What is the significance of the InterruptedException in Java?
Answer:
Explanation:
InterruptedException is thrown when a thread that is sleeping, waiting, or otherwise paused is interrupted.
28. In Java, under which circumstance will the catch block for ArithmeticException catch a NullPointerException?
Answer:
Explanation:
Catch blocks are structured to catch specific exception types. A catch block for ArithmeticException will not catch NullPointerException, as they are different types of exceptions.
29. Which of these is a direct subclass of the Exception class (not RuntimeException)?
Answer:
Explanation:
IOException is a subclass of Exception and is not a RuntimeException. It's a checked exception that must be either caught or declared in the method signature.
30. What is the output of a program that catches an IOException, but an ArithmeticException is thrown?
Answer:
Explanation:
If an exception is thrown that does not match the type caught by the catch block, the catch block will not execute, and the exception will propagate up the call stack.
31. How does the try-with-resources statement benefit exception handling in Java?
Answer:
Explanation:
The try-with-resources statement automatically closes the resources (like files, database connections) used within the try block, eliminating the need for a finally block to close these resources.
32. What will be the result of compiling and running a Java program that has a try block with an unreachable catch block for ArithmeticException?
Answer:
Explanation:
An unreachable catch block (e.g., when there's no code in the try block that can throw the caught exception) will not cause a compilation error. The program will compile and run normally, just not entering the catch block.
33. Which statement is true about the Error class in Java?
Answer:
Explanation:
Error is a subclass of Throwable, like Exception. Errors indicate serious problems and are not meant to be caught and handled by applications.
34. What is the effect of declaring an exception in the throws clause of a method signature?
Answer:
Explanation:
Declaring an exception in the throws clause of a method signature does not handle the exception within the method. Instead, it indicates that the method may throw this exception, and the responsibility to handle it is passed to the method that calls this method.
35. Which among the following best describes a checked exception?
Answer:
Explanation:
Checked exceptions are exceptions that are checked at compile-time, ensuring that they are either caught or declared in the method signature. This helps in reducing runtime errors.
36. In a method declaration, what does it mean to use 'throws Exception'?
Answer:
Explanation:
Using 'throws Exception' in a method declaration signals that the method might throw any checked exception during its execution.
37. Which method is used to retrieve the detailed message of an exception?
Answer:
Explanation:
The getMessage() method is used to retrieve the detailed message of the Throwable object, which can provide more information about the exception.
38. What is the primary difference between throw and throws in Java?
Answer:
Explanation:
The throw keyword is used to actually throw an exception, whereas throws is used in a method declaration to indicate that the method might throw the specified exceptions.
39. What will happen if a try block is followed by multiple catch blocks?
Answer:
Explanation:
When a try block is followed by multiple catch blocks, each catch block is checked in the order they appear. The first catch block with a matching exception type will handle the exception, and the rest will be ignored.
40. What kind of exception is thrown by the JVM when a method call is made on a null reference?
Answer:
Explanation:
When a method call is made on a null reference, the JVM throws a NullPointerException.
41. What is the result of trying to compile a program with an empty try block and no catch or finally blocks?
Answer:
Explanation:
A try block without either a catch or finally block is not valid in Java and will result in a compilation error.
42. When should the 'throws' keyword be used in a method signature?
Answer:
Explanation:
The 'throws' keyword should be used in a method signature when the method might throw a checked exception. This informs the caller of the method that they need to handle or declare the exception.
43. What is the correct way to handle multiple exceptions of the same type in Java?
Answer:
Explanation:
Java 7 introduced the ability to catch multiple exceptions in a single catch block using the pipe (|) character to separate the exception types.
44. Which exception is thrown when an illegal operation is performed on an empty container, such as a Queue or a Stack?
Answer:
Explanation:
NoSuchElementException is thrown when one tries to access or remove an element from an empty container like Queue or Stack.
45. What is the correct syntax to create a custom exception in Java?
Answer:
Explanation:
To create a custom exception in Java, you extend either Exception (for checked exceptions) or RuntimeException (for unchecked exceptions).
46. What is the effect of an uncaught exception in the main thread of a Java application?
Answer:
Explanation:
An uncaught exception in the main thread of a Java application causes the main thread to terminate, which generally results in the termination of the application.
47. Which of the following is not a purpose of the finally block in Java exception handling?
Answer:
Explanation:
The finally block is not for catching exceptions but for executing code regardless of whether an exception occurs, typically used for cleaning up resources like closing file streams or database connections.
34. What is the effect of declaring an exception in the throws clause of a method signature?
Answer:
Explanation:
Declaring an exception in the throws clause of a method signature does not handle the exception within the method. Instead, it indicates that the method may throw this exception, and the responsibility to handle it is passed to the method that calls this method.
35. Which among the following best describes a checked exception?
Answer:
Explanation:
Checked exceptions are exceptions that are checked at compile-time, ensuring that they are either caught or declared in the method signature. This helps in reducing runtime errors.
36. In a method declaration, what does it mean to use 'throws Exception'?
Answer:
Explanation:
Using 'throws Exception' in a method declaration signals that the method might throw any checked exception during its execution.
37. Which method is used to retrieve the detailed message of an exception?
Answer:
Explanation:
The getMessage() method is used to retrieve the detailed message of the Throwable object, which can provide more information about the exception.
38. What is the primary difference between throw and throws in Java?
Answer:
Explanation:
The throw keyword is used to actually throw an exception, whereas throws is used in a method declaration to indicate that the method might throw the specified exceptions.
39. What will happen if a try block is followed by multiple catch blocks?
Answer:
Explanation:
When a try block is followed by multiple catch blocks, each catch block is checked in the order they appear. The first catch block with a matching exception type will handle the exception, and the rest will be ignored.
40. What kind of exception is thrown by the JVM when a method call is made on a null reference?
Answer:
Explanation:
When a method call is made on a null reference, the JVM throws a NullPointerException.
41. What is the result of trying to compile a program with an empty try block and no catch or finally blocks?
Answer:
Explanation:
A try block without either a catch or finally block is not valid in Java and will result in a compilation error.
42. When should the 'throws' keyword be used in a method signature?
Answer:
Explanation:
The 'throws' keyword should be used in a method signature when the method might throw a checked exception. This informs the caller of the method that they need to handle or declare the exception.
43. What is the correct way to handle multiple exceptions of the same type in Java?
Answer:
Explanation:
Java 7 introduced the ability to catch multiple exceptions in a single catch block using the pipe (|) character to separate the exception types.
44. Which exception is thrown when an illegal operation is performed on an empty container, such as a Queue or a Stack?
Answer:
Explanation:
NoSuchElementException is thrown when one tries to access or remove an element from an empty container like Queue or Stack.
45. What is the correct syntax to create a custom exception in Java?
Answer:
Explanation:
To create a custom exception in Java, you extend either Exception (for checked exceptions) or RuntimeException (for unchecked exceptions).
46. What is the effect of an uncaught exception in the main thread of a Java application?
Answer:
Explanation:
An uncaught exception in the main thread of a Java application causes the main thread to terminate, which generally results in the termination of the application.
47. Which of the following is not a purpose of the finally block in Java exception handling?
Answer:
Explanation:
The finally block is not for catching exceptions but for executing code regardless of whether an exception occurs, typically used for cleaning up resources like closing file streams or database connections.
48. What is the result of catching an Exception type before catching its subclass type?
Answer:
Explanation:
If an Exception type (superclass) is caught before its subclass, the subclass catch block becomes unreachable, leading to a compilation error.
49. What kind of exceptions are subclasses of RuntimeException in Java?
Answer:
Explanation:
Subclasses of RuntimeException are unchecked exceptions, meaning the compiler does not require them to be declared or caught.
50. Which method in Java is used to manually cause an exception during testing?
Answer:
Explanation:
The throw keyword followed by a new exception instance (e.g., 'throw new Exception()') is used to manually trigger an exception, often used in testing or to indicate a special error condition.
Comments
Post a Comment
Leave Comment