Welcome Guest | Sign in | Register

Home > Java Programming > Exceptions > Questions and Answers

01. 1. What will be the output of the program?
public class MyProgram{
public static void main(String args[]){
try{
System.out.print("Hello world ");
}
finally{
System.out.println("Finally executing ");
}
}
}
A. Nothing. The program will not compile because no exceptions are specified. B. Nothing. The program will not compile because no catch clauses are specified.
C. Hello world. D. Hello world Finally executing

Answer and Explanation

Answer: Hello world Finally executing

Explanation:
Finally clauses are always executed. The program will first execute the try block, printing Hello world, and will then execute the finally block, printing Finally executing.
Option A, B, and C are incorrect based on the program logic described above. Remember that either a catch or a finally statement must follow a try. Since the finally is present, the catch is not required.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
02. 1. 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() {}


A. AC B. BC 
C. ACD D. ABCD

Answer and Explanation

Answer: ACD

Explanation:
There is no exception thrown, so all the code with the exception of the catch statement block is run.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
03. 1. 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 ");
}
}
A. hello throwit caught B. Compilation fails
C. hello throwit RuntimeException caught after D. hello throwit caught finally after

Answer and Explanation

Answer: hello throwit caught finally after

Explanation:
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.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
04. Which statement is true?
A. A try statement must have at least one corresponding catch block B. Multiple catch statements can catch the same class of exception more than once.
C. An Error that might be thrown in a method must be declared as thrown by that method, or be handled within that method. D. Except in case of VM shutdown, if a try block starts to execute, a corresponding finally block will always start to execute.

Answer and Explanation

Answer: Except in case of VM shutdown, if a try block starts to execute, a corresponding finally block will always start to execute.

Explanation:
A is wrong. A try statement can exist without catch, but it must have a finally statement.
B is wrong. A try statement executes a block. If a value is thrown and the try statement has one or more catch clauses that can catch it, then control will be transferred to the first such catch clause. If that catch block completes normally, then the try statement completes normally.
C is wrong. Exceptions of type Error and RuntimeException do not have to be caught, only checked exceptions (java.lang.Exception) have to be caught. However, speaking of Exceptions, Exceptions do not have to be handled in the same method as the throw statement. They can be passed to another method.
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.
The last three could be classified as VM shutdown.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
05. System.out.print("Start ");
try{
System.out.print("Hello world");
throw new FileNotFoundException();
}
System.out.print(" Catch Here "); /* Line 7 */
catch(EOFException e){
System.out.print("End of file exception");
}
catch(FileNotFoundException e){
System.out.print("File not found");
}
and given that EOFException and FileNotFoundException are both subclasses ofIOException, and further assuming this block of code is placed into a class, which statement is most true concerning this code?
A. The code will not compile B. Code output: Start Hello world File Not Found.
C. Code output: Start Hello world End of file exception. D. Code output: Start Hello world Catch Here File not found.

Answer and Explanation

Answer: The code will not compile

Explanation:
Line 7 will cause a compiler error. The only legal statements after try blocks are either catch or finally statements.
Option B, C, and D are incorrect based on the program logic described above. If line 7 was removed, the code would compile and the correct answer would be Option B.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
06. class A{
static void test() throws RuntimeException {
try {
System.out.print("test ");
throw new RuntimeException();
}catch (Exception ex) {
 System.out.print("exception "); 
}
}
public static void main(String[] args) {
try { test(); }
catch (RuntimeException ex) { System.out.print("runtime "); }
System.out.print("end ");
}
What is the result?
A. test end B. Compilation fails.
C. test runtime end D. test exception end
E. A throwable is thrown by main at runtime.

Answer and Explanation

Answer: test exception end

Explanation:
From main ( ) method it will go to test ( ) method. Test ( ) method is throwing an exception and it has been handled here only. And when it comes back to main ( ) method it will not execute catch block. It will just execute last line that is System.out.println.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
07. class A{
public static void main(String[] args) {
try {
args = null;
args[0] = "test";
System.out.println(args[0]);
} catch (Exception ex) {
System.out.println("Exception");
} catch (NullPointerException npe) {
System.out.println("NullPointerException");
}
}
What is the result?
A. test B. Exception
C. Compilation fails. D. NullPointerException

Answer and Explanation

Answer: Compilation fails.

Explanation:
Whenever you write number of catch blocks, catch block with super class (Exception)should be always at the end. Because if you write first then rest of the catch blocks will not going to execute. If you write super class Exception first then rest of the exception type next then it will throw compile time error. So in our program NullPointerException should come first then Exception class.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
08.
1. try{
2. // some code here
3.} catch (NullPointerException e1) {
4. System.out.print("a");
5.} catch (Exception e2) {
6. System.out.print("b");
7.} finally {
8. System.out.print("c");
9.}

If some sort of exception is thrown at line 2, which output is possible?
A. a B. b
C. c D. ac
E. abc

Answer and Explanation

Answer: ac

Explanation:
You can have any number of catch blocks, if exception is occurred from try, then it will check in immediate catch , if immediate catch block is able to handle then it will go inside that catch block. If not then it will go and check next available catch block. and finally will execute whether exception occurs or not. So answer is D).

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
09. Given:

1. // some code here
2. try{
3. // some code here
4.} catch (SomeException se) {
5. // some code here
6.} finally {
7. // some code here
8.}

Under which three circumstances will the code on line 7 be executed? (Choose three.)
A. The instance gets garbage collected.
B. The code on line 3 throws an exception.
C. The code on line 5 throws an exception.
D. The code on line 1 throws an exception.
E. The code on line 3 executes successfully.

A. A,B,C B. B,C,E
C. A,B,E D. C,D,E

Answer and Explanation

Answer: B,C,E

Explanation:
When finally block will execute, as we know if no exception occurs (means in 3 line) then directly finally block will execute.
If exception occurs then try, catch, finally (means line 3 as well as 5) block will execute.
Or else directly after 1 line finally block will execute.
So A) is correct.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
10. class A{
public static void main(String args[]){
try{
System.out.println(“one”); 
}
System.out.println(“four”);
catch(Exception e){
System.out.println(“two”);
}finally{
System.out.println(“three”); }
}
}
A. one ,four , two , three B. one , four , three
C. one , three D. compile time error

Answer and Explanation

Answer: compile time error

Explanation:
In any program as soon as you complete try block you have to start catch block or finally block, in between if you try to write anything it will throw compile time error.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum



Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved 2012-2015 SoftLucent.