Java Exception Handiling

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Mahipalreddy
M
Mahipalreddy
Community Contributor
Quizzes Created: 1 | Total Attempts: 4,615
| Attempts: 4,615 | Questions: 25
Please wait...
Question 1 / 25
0 %
0/100
Score 0/100
1. In Java, exceptions are divided into two categories, namely checked and unchecked exceptions.

Explanation

In Java, exceptions are categorized into two types: checked and unchecked exceptions. Checked exceptions are the exceptions that must be declared in the method signature or handled using try-catch blocks. On the other hand, unchecked exceptions are not required to be declared or handled explicitly. This categorization helps in distinguishing between exceptions that require immediate attention and those that can be left unhandled. Therefore, the statement "In Java, exceptions are divided into two categories, namely checked and unchecked exceptions" is true.

Submit
Please wait...
About This Quiz
Java Exception Handiling - Quiz

This quiz focuses on Java Exception Handling, assessing knowledge on runtime exceptions, compile-time errors, and correct use of try-catch-finally blocks. It is designed for learners to test and... see moreenhance their understanding of Java's error management mechanisms. see less

2. Finally block will get invoke whether the exception is thrown or not

Explanation

The finally block is a section of code that is always executed, regardless of whether an exception is thrown or not. It is typically used to release resources or perform cleanup operations. In this case, since the question states that the finally block will be invoked whether the exception is thrown or not, the correct answer is True.

Submit
3. Exceptions can be caught or rethrown to a calling method.

Explanation

Exceptions can be caught or rethrown to a calling method. This means that when an exception occurs in a method, it can be caught within that method using a try-catch block. Alternatively, the exception can be rethrown to the calling method, allowing it to handle the exception instead. This allows for more flexibility in how exceptions are handled, as different methods in the call stack can choose to handle or propagate the exception based on their specific requirements.

Submit
4. The subclass exception should precede the base class exception when used within the catch clause.

Explanation

In object-oriented programming, a subclass is a more specialized version of a base class. When handling exceptions in a catch clause, it is generally recommended to catch more specific exceptions (subclasses) before catching more general exceptions (base classes). This is because if a more general exception is caught first, the catch block for the more specific exception will never be reached. By catching the subclass exception first, we can handle it specifically, and then catch the base class exception if necessary. This ensures that exceptions are handled in the most appropriate and specific way possible.

Submit
5. Is it possible to re-throw exceptions

Explanation

Yes, it is possible to re-throw exceptions in programming. When an exception is caught, the catch block has the option to re-throw the exception using the "throw" keyword. This allows the exception to be handled by a higher-level catch block or propagate up the call stack until it is caught by an appropriate handler. Re-throwing exceptions can be useful in situations where the current catch block is not equipped to handle the exception, but another part of the program may be able to handle it better.

Submit
6. If you throw an exception in your code, then you must declare it using the throws keyword in your method declaration.

Explanation

When you throw an exception in your code, it means that you are signaling that an error or unexpected condition has occurred. In Java, if you throw a checked exception (exceptions that are not subclasses of RuntimeException), you must declare it using the "throws" keyword in your method declaration. This is necessary to inform the caller of the method that they need to handle or propagate the exception. Therefore, the statement "If you throw an exception in your code, then you must declare it using the throws keyword in your method declaration" is true.

Submit
7. All subclasses of the RuntimeException and Error classes are unchecked exceptions.

Explanation

All subclasses of the RuntimeException and Error classes are unchecked exceptions because they are not required to be declared in a method's throws clause or caught using a try-catch block. These exceptions are typically caused by programming errors or exceptional conditions that are beyond the control of the programmer, and it is not mandatory for the code to handle or anticipate these exceptions.

Submit
8. Parent of Error is.......

Explanation

The parent of Error is "throwable" because in Java, "throwable" is the superclass of all classes that represent errors and exceptions. The Error class is a subclass of throwable, along with other classes like Exception and RuntimeException. Therefore, throwable is the correct answer as it represents the common parent class for all types of errors and exceptions.

Submit
9. Which one of the following statement is correct?

Explanation

The correct answer is that the 'try' block should be followed by either a 'catch' block or a 'finally' block. This is because the purpose of a 'try' block is to enclose a section of code that may potentially throw an exception. By following it with a 'catch' block, we can handle any exceptions that are thrown within the 'try' block. Alternatively, by following it with a 'finally' block, we can ensure that certain code is executed regardless of whether an exception is thrown or not. Therefore, the 'try' block should always be followed by either a 'catch' block or a 'finally' block.

Submit
10. A try with finally without catch can declare the exception

Explanation

In Java, a try block with a finally block but without a catch block can still declare an exception. This means that if an exception occurs within the try block, it will be caught and handled by an appropriate catch block outside of the try-finally construct. The finally block will always execute, regardless of whether an exception is thrown or not. Therefore, the statement "A try with finally without catch can declare the exception" is true.

Submit
11. Output of the following program try { x.doStuff(); } int y = 50; } catch(FooException fe) { }

Explanation

The given code snippet is missing a closing bracket after the try block. This will result in a compile time error because the code is not properly structured.

Submit
12. What are checked exceptions

Explanation

Checked exceptions are a type of exceptions in Java that are checked by the Java compiler. This means that when a method throws a checked exception, the compiler will require the caller of that method to handle or declare that exception. If the caller does not handle or declare the exception, a compilation error will occur. This mechanism ensures that the programmer is aware of and handles potential exceptions that can occur during program execution, promoting more robust and reliable code.

Submit
13. When reading or writing a file it throws class not found exception

Explanation

When reading or writing a file, it does not throw a "class not found" exception. This exception typically occurs when the Java Virtual Machine (JVM) cannot find a class that is referenced in the code. In the context of file operations, exceptions such as FileNotFoundException or IOException are more commonly thrown when there is an issue with reading or writing a file. Therefore, the correct answer is False.

Submit
14. Checked exceptions include all subtypes of Exception, including classes that extend RuntimeException.

Explanation

The given statement is false. Checked exceptions do not include all subtypes of Exception, including classes that extend RuntimeException. Checked exceptions are a type of exception that must be declared in a method's signature or handled using a try-catch block. They are typically used for exceptional conditions that a well-behaved application should anticipate and handle. RuntimeException and its subclasses, on the other hand, are unchecked exceptions and do not need to be declared or caught explicitly.

Submit
15.  exception is available in util package

Explanation

The statement is false because the "exception" class is not available in the "util" package. The "exception" class is part of the "java.lang" package, which is automatically imported in every Java program. The "util" package, on the other hand, contains classes for utility purposes, such as collections, sorting, and date/time manipulation.

Submit
16. The statements following the throw keyword in a program are not executed.

Explanation

The statement is true because when the throw keyword is used in a program, it immediately stops the execution of the current code block and transfers control to the nearest catch block. Therefore, any statements following the throw keyword will not be executed.

Submit
17. Say true or false.The following program will compile: try { // do risky IO things } catch (IOException ioe) { // handle general IOExceptions } catch (IOException ioe) { // handle general IOExceptions }

Explanation

The given program will not compile because it has duplicate catch blocks for the same exception type (IOException). Each catch block should handle a unique exception type, so having two catch blocks for the same exception type is not allowed.

Submit
18. File Not Found Exception class has descendants

Explanation

The statement "File Not Found Exception class has descendants" is false. The File Not Found Exception class is a specific type of exception that is thrown when a file is not found. It does not have any descendants or subclasses.

Submit
19. What are un checked exceptions

Explanation

The correct answer is "checked by java virtual machine". In Java, there are two types of exceptions: checked exceptions and unchecked exceptions. Checked exceptions are checked by the Java compiler at compile-time to ensure that they are properly handled or declared in the code. On the other hand, unchecked exceptions are not checked by the compiler, but they are still checked by the Java virtual machine at runtime. Therefore, the correct answer is that unchecked exceptions are checked by the Java virtual machine.

Submit
20. Creating an exception object and handling it to the run time system is called

Explanation

Throwing an exception refers to the action of generating an exception object and transferring it to the runtime system. When an exception is thrown, it interrupts the normal flow of the program and transfers control to an appropriate exception handler. Therefore, "throwing an exception" is the correct term to describe this process.

Submit
21. Can i use more than one try block

Explanation

In Java, it is possible to use more than one try block in a program. This allows for handling different types of exceptions in separate blocks. Each try block can be followed by multiple catch blocks to handle specific exceptions. Therefore, the statement "False" is incorrect and the correct answer should be "True".

Submit
22. Question: Match each situation in the first list with an item in the second list. a)int[] A;  A[0] = 0; b)The JVM starts running your program, but the JVM can't find the Java platform classes. (The Java platform classes reside in classes.zip or rt.jar.) c)A program is reading a stream and reaches the end of stream marker. d)Before closing the stream and after reaching the end of stream marker, a program tries to read the stream again. 1__error 2__checked exception 3__compile error 4__no exception

Explanation

In situation a, the code `A[0] = 0;` is trying to access an element of the array `A` without initializing it first, which would result in a compile error (option 3).

In situation b, the JVM is unable to find the Java platform classes, which is an error (option 1).

In situation c, the program reaches the end of the stream marker, which is a normal condition and does not cause any error or exception (option 4).

In situation d, the program tries to read the stream again after reaching the end of the stream marker, which would result in an exception (option 2).

Submit
23. What is throws in exception

Explanation

The correct answer is "a programmer can not handle". This means that when an exception is thrown, it cannot be handled by the programmer's code. Instead, it is the responsibility of the JVM (Java Virtual Machine) to handle the exception. The programmer can only catch and handle exceptions that are explicitly specified in the code using try-catch blocks.

Submit
24. Benefits of java Exception handler(any 3)

Explanation

The benefits of using a Java Exception handler include propagating errors up the call stack, which allows for better error handling and debugging by identifying the source of the error. Grouping and differentiating error types helps in categorizing and handling different types of errors separately, making the code more maintainable and readable. Separating error handling code from regular business logic code improves code modularity and makes it easier to manage and update error handling routines independently.

Submit
25. Pick runtime exception?....

Explanation

The correct answer is a combination of class cast exception, null pointer exception, and security exception. These are all examples of runtime exceptions that can occur during the execution of a program. A class cast exception occurs when there is an attempt to cast an object to a subclass of which it is not an instance. A null pointer exception occurs when a null reference is accessed. A security exception occurs when a security violation is detected. All of these exceptions can cause the program to terminate abruptly if not handled properly.

Submit
View My Results

Quiz Review Timeline (Updated): Sep 26, 2023 +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Sep 26, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Oct 18, 2011
    Quiz Created by
    Mahipalreddy
Cancel
  • All
    All (25)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
In Java, exceptions are divided into two categories, namely checked...
Finally block will get invoke whether the exception is thrown or not
Exceptions can be caught or rethrown to a calling method.
The subclass exception should precede the base class exception when...
Is it possible to re-throw exceptions
If you throw an exception in your code, then you must declare it using...
All subclasses of the RuntimeException and Error classes are unchecked...
Parent of Error is.......
Which one of the following statement is correct?
A try with finally without catch can declare the exception
Output of the following program ...
What are checked exceptions
When reading or writing a file it throws class not found exception
Checked exceptions include all subtypes of Exception, including...
 exception is available in util package
The statements following the throw keyword in a program are not...
Say true or false.The following program will compile:...
File Not Found Exception class has descendants
What are un checked exceptions
Creating an exception object and handling it to the run time system is...
Can i use more than one try block
Question: Match each situation in the first list with an item in the...
What is throws in exception
Benefits of java Exception handler(any 3)
Pick runtime exception?....
Alert!

Advertisement