1.
Pick runtime exception?....
Correct Answer(s)
A. Class cast exception
C. Nullpointer exception
D. Security 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.
2.
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
}
Correct Answer
B. False
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.
3.
Output of the following program
try {
x.doStuff();
}
int y = 50;
} catch(FooException fe) { }
Correct Answer
C. Compile time error
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.
4.
A try with finally without catch can declare the exception
Correct Answer
A. True
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.
5.
Which one of the following statement is correct?
Correct Answer
C. The ‘try’ block should be followed by either a ‘catch’ block or a ‘finally’ block.
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.
6.
Parent of Error is.......
Correct Answer
C. Throwable
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.
7.
If you throw an exception in your code, then you must declare it using the throws keyword in your method declaration.
Correct Answer
A. True
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.
8.
The subclass exception should precede the base class exception when used within the catch clause.
Correct Answer
A. True
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.
9.
The statements following the throw keyword in a program are not executed.
Correct Answer
A. True
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.
10.
Exceptions can be caught or rethrown to a calling method.
Correct Answer
A. True
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.
11.
Checked exceptions include all subtypes of Exception, including classes that extend RuntimeException.
Correct Answer
B. False
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.
12.
File Not Found Exception class has descendants
Correct Answer
B. False
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.
13.
In Java, exceptions are divided into two categories, namely checked and unchecked exceptions.
Correct Answer
A. True
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.
14.
All subclasses of the RuntimeException and Error classes are unchecked exceptions.
Correct Answer
A. True
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.
15.
When reading or writing a file it throws class not found exception
Correct Answer
B. False
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.
16.
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
Correct Answer
C. A-3,b-1,c-4,d-2
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).
17.
What are checked exceptions
Correct Answer
A. Checked by java compiler
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.
18.
What are un checked exceptions
Correct Answer
B. Checked by java virtual machine
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.
19.
What is throws in exception
Correct Answer
B. A programmer can not handle
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.
20.
Can i use more than one try block
Correct Answer
B. False
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".
21.
Is it possible to re-throw exceptions
Correct Answer
A. True
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.
22.
Creating an exception object and handling it to the run time system is called
Correct Answer
D. Throwing an exception
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.
23.
Finally block will get invoke whether the exception is thrown or not
Correct Answer
A. True
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.
24.
Benefits of java Exception handler(any 3)
Correct Answer(s)
B. Propagating errors up the call stack
D. Grouping & diff error type
E. Separating error handling code from regular business logic code
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.
25.
exception is available in util package
Correct Answer
B. False
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.