Java Exception Handling Test 1 - Java Question and Answers
Finish Quiz
0 of 20 questions completed
Questions:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
Information
Java Exception Handling Test 1 – Java Question and Answers. Free Java Exception Handling Quiz. JDBC Online Quiz – Learning JDBC in simple and easy steps using this beginner’s Online Test. Take free online Test Quiz 1. Java Exception Handling Test Quiz 1 Question and Answers 2019. Java online Test Quiz 1. Java Exception Handling Quiz 1 Free Mock Test 2019. Java Exception Handling Test Quiz 1 Question and Answers in PDF. The Java online mock test paper is free for all students. Java Exception Handling Test is very useful for exam preparation and getting for Rank. Java Exception Handling Test Quiz 1 Question and Answers in English. Java Exception Handling Mock test for topic via Exception Handling Mode. Here we are providing Java Exception Handling Mock Test in English Now Test your self for “Java Exception Handling Test in English” Exam by using below quiz…
This paper has 20 questions.
Time allowed is 25 minutes.
The Java Exception Handling Mock Test is Very helpful for all students. Now Scroll down below n click on “Start Quiz” or “Start Test” and Test yourself.
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading...
You must sign in or sign up to start the quiz.
You have to finish following quiz, to start this quiz:
Results
0 of 20 questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 points, (0)
Average score |
|
Your score |
|
Categories
- Not categorized 0%
Pos. | Name | Entered on | Points | Result |
---|---|---|---|---|
Table is loading | ||||
No data available | ||||
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- Answered
- Review
-
Question 1 of 20
1. Question
What will be the output of the program?
public class X
{
public static void main(String [] args)
{
try
{
badMethod(); /* Line 7 */
System.out.print(“A”);
}
catch (Exception ex) /* Line 10 */
{
System.out.print(“B”); /* Line 12 */
}
finally /* Line 14 */
{
System.out.print(“C”); /* Line 16 */
}
System.out.print(“D”); /* Line 18 */
}
public static void badMethod()
{
throw new RuntimeException();
}
}Correct
(1) A RuntimeException is thrown, this is a subclass of exception.
(2) The exception causes the try to complete abruptly (line 7) therefore line 8 is never executed.
(3) The exception is caught (line 10) and “B” is output (line 12)
(4) The finally block (line 14) is always executed and “C” is output (line 16).
(5) The exception was caught, so the program continues with line 18 and outputs “D”.Incorrect
(1) A RuntimeException is thrown, this is a subclass of exception.
(2) The exception causes the try to complete abruptly (line 7) therefore line 8 is never executed.
(3) The exception is caught (line 10) and “B” is output (line 12)
(4) The finally block (line 14) is always executed and “C” is output (line 16).
(5) The exception was caught, so the program continues with line 18 and outputs “D”. -
Question 2 of 20
2. Question
We know that the method printStackTrace of an exception prints the stack of methods that have been call when that exception has ocurred. What stack trace will be printed after calling method1?
public void method1() throws Exception {
method2();
}public void method2() throws Exception {
throw method3();
}public Exception method3() {
return new Exception();
}Correct
Incorrect
-
Question 3 of 20
3. Question
What will be the output of the program?
public class Test
{
public static void aMethod() throws Exception
{
try /* Line 5 */
{
throw new Exception(); /* Line 7 */
}
finally /* Line 9 */
{
System.out.print(“finally “); /* Line 11 */
}
}
public static void main(String args[])
{
try
{
aMethod();
}
catch (Exception e) /* Line 20 */
{
System.out.print(“exception “);
}
System.out.print(“finished”); /* Line 24 */
}
}Correct
This is what happens:
(1) The execution of the try block (line 5) completes abruptly because of the throwstatement (line 7).
(2) The exception cannot be assigned to the parameter of any catch clause of thetry statement therefore the finally block is executed (line 9) and “finally” is output (line 11).
(3) The finally block completes normally, and then the try statement completes abruptly because of the throw statement (line 7).
(4) The exception is propagated up the call stack and is caught by the catch in the main method (line 20). This prints “exception”.
(5) Lastly program execution continues, because the exception has been caught, and “finished” is output (line 24).Incorrect
This is what happens:
(1) The execution of the try block (line 5) completes abruptly because of the throwstatement (line 7).
(2) The exception cannot be assigned to the parameter of any catch clause of thetry statement therefore the finally block is executed (line 9) and “finally” is output (line 11).
(3) The finally block completes normally, and then the try statement completes abruptly because of the throw statement (line 7).
(4) The exception is propagated up the call stack and is caught by the catch in the main method (line 20). This prints “exception”.
(5) Lastly program execution continues, because the exception has been caught, and “finished” is output (line 24). -
Question 4 of 20
4. Question
What will be the output of the program?
public class Foo
{
public static void main(String[] args)
{
try
{
return;
}
finally
{
System.out.println( “Finally” );
}
}
}Correct
If you put a finally block after a try and its associated catch blocks, then once execution enters the try block, the code in that finally block will definitely be executed except in the following circumstances:
1. An exception arising in the finally block itself.
2. The death of the thread.
3. The use of System.exit()
4. Turning off the power to the CPU.
I suppose the last three could be classified as VM shutdown.Incorrect
If you put a finally block after a try and its associated catch blocks, then once execution enters the try block, the code in that finally block will definitely be executed except in the following circumstances:
1. An exception arising in the finally block itself.
2. The death of the thread.
3. The use of System.exit()
4. Turning off the power to the CPU.
I suppose the last three could be classified as VM shutdown. -
Question 5 of 20
5. Question
What will be the output of the program?
public class X
{
public static void main(String [] args)
{
try
{
badMethod();
System.out.print(“A”);
}
catch (RuntimeException ex) /* Line 10 */
{
System.out.print(“B”);
}
catch (Exception ex1)
{
System.out.print(“C”);
}
finally
{
System.out.print(“D”);
}
System.out.print(“E”);
}
public static void badMethod()
{
throw new RuntimeException();
}
}Correct
A Run time exception is thrown and caught in the catch statement on line 10. All the code after the finally statement is run because the exception has been caught.
Incorrect
A Run time exception is thrown and caught in the catch statement on line 10. All the code after the finally statement is run because the exception has been caught.
-
Question 6 of 20
6. Question
Following code will result in: float num = 5/0;
Correct
Incorrect
-
Question 7 of 20
7. Question
What will be the output of the program?
public class RTExcept
{
public static void throwit ()
{
System.out.print(“throwit “);
throw new RuntimeException();
}
public static void main(String [] args)
{
try
{
System.out.print(“hello “);
throwit();
}
catch (Exception re )
{
System.out.print(“caught “);
}
finally
{
System.out.print(“finally “);
}
System.out.println(“after “);
}
}Correct
The main() method properly catches and handles the RuntimeException in the catch block, finally runs (as it always does), and then the code returns to normal.
A, B and C are incorrect based on the program logic described above. Remember that properly handled exceptions do not cause the program to stop executing.Incorrect
The main() method properly catches and handles the RuntimeException in the catch block, finally runs (as it always does), and then the code returns to normal.
A, B and C are incorrect based on the program logic described above. Remember that properly handled exceptions do not cause the program to stop executing. -
Question 8 of 20
8. Question
What will be the output of the program?
public class X
{
public static void main(String [] args)
{
try
{
badMethod();
System.out.print(“A”);
}
catch (Exception ex)
{
System.out.print(“B”);
}
finally
{
System.out.print(“C”);
}
System.out.print(“D”);
}
public static void badMethod()
{
throw new Error(); /* Line 22 */
}
}Correct
Error is thrown but not recognised line(22) because the only catch attempts to catch an Exception and Exception is not a superclass of Error. Therefore only the code in the finally statement can be run before exiting with a runtime error (Exception in thread “main” java.lang.Error).
Incorrect
Error is thrown but not recognised line(22) because the only catch attempts to catch an Exception and Exception is not a superclass of Error. Therefore only the code in the finally statement can be run before exiting with a runtime error (Exception in thread “main” java.lang.Error).
-
Question 9 of 20
9. Question
What exception will be thrown from the following block of code?
try {
throw new TryException();
}
catch {
throw new CatchException();
}
finally {
throw new FinallyException();
}Correct
Incorrect
-
Question 10 of 20
10. Question
What will be the output of the program?
try
{
int x = 0;
int y = 5 / x;
}
catch (Exception e)
{
System.out.println(“Exception”);
}
catch (ArithmeticException ae)
{
System.out.println(” Arithmetic Exception”);
}
System.out.println(“finished”);Correct
Compilation fails because ArithmeticException has already been caught.ArithmeticException is a subclass of java.lang.Exception, by time theArithmeticException has been specified it has already been caught by theException class.
If ArithmeticException appears before Exception, then the file will compile. When catching exceptions the more specific exceptions must be listed before the more general (the subclasses must be caught before the superclasses).Incorrect
Compilation fails because ArithmeticException has already been caught.ArithmeticException is a subclass of java.lang.Exception, by time theArithmeticException has been specified it has already been caught by theException class.
If ArithmeticException appears before Exception, then the file will compile. When catching exceptions the more specific exceptions must be listed before the more general (the subclasses must be caught before the superclasses). -
Question 11 of 20
11. Question
You are writing a program to check people into a hotel. People over 65 get a 10 percent discount. How would you write the program?
Correct
Incorrect
-
Question 12 of 20
12. Question
Which of the following is a correct outline of a method that throws an exception when it finds a problem?
Correct
Incorrect
-
Question 13 of 20
13. Question
What method of an Exception object returns a message string?
Correct
Incorrect
-
Question 14 of 20
14. Question
A method must do either of two things with a checked exception. What are those two things?
Correct
Incorrect
-
Question 15 of 20
15. Question
Say that a method catches an IOException in a catch{} block. Is it possible for that block to do some processing and then throw the same exception to the caller?
Correct
Incorrect
-
Question 16 of 20
16. Question
MethodX might encounter an IOException or an AWTException, but handles neither. How should the header for methodX be written?
Correct
Incorrect
-
Question 17 of 20
17. Question
What method of an Exception object prints a list of methods that were called before the exception was thrown?
Correct
Incorrect
-
Question 18 of 20
18. Question
What happens during execution if an negative value is used for an array index?
Correct
Incorrect
-
Question 19 of 20
19. Question
What is the only type of exception that is NOT checked?
Correct
Incorrect
-
Question 20 of 20
20. Question
Say that methodA calls methodB, and methodB calls methodC. MethodC might throw a NumberFormatException. Can the program be written so that methodA handles the exception?
Correct
Incorrect