Welcome Guest | Sign in | Register

Home > Java Programming > Exceptions > Questions and Answers

01. class A{
public static void main(String args[]){
try{
System.out.println(“one”); 
}catch(Exception e){
System.out.println(“two”); }
finally{
System.out.println(“three”); }
}
}
A. one , two , three B. one
C. one , three D. compile time error

Answer and Explanation

Answer: one , three

Explanation:
In the above example only try and finally will execute, as there is no exception catch block will not execute.
So c) is correct.

Report Errors

Name:

Loading...

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

Answer and Explanation

Answer: one  

Explanation:
In the above program inside a try block they have used System.exit(0), if you use this code then rest of the code will get skipped from the execution.
So c) is correct answer.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
03. class A{
public static void main(String args[]){
try{
System.out.println(“one”);
 }
}
Output?
A. one    B. runtime error
C. compile time error D. none of the above

Answer and Explanation

Answer: compile time error

Explanation:
If you write a try block there should be a catch block or finally block, if there is no catch or finally it will throw compile time error.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
04. class A{
void display() throws IOException,FileNotFoundException {
System.out.println(“class A”); }
}
class B extends A {
void display() throws IOException, FileNotFoundException,NullPointerException {
System.out.println(“class B”); }
}
class Test {
Public static void main(String args[]){
B b=new B();
b.display(); }
}
Output?
A. class A B. class B
C. class A, class B D. compile time error

Answer and Explanation

Answer: compile time error

Explanation:
In the above program class A is super class and it is throwing two exceptions. In class B which is child class of A must throw less number of exceptions compare to A. If child class throws more number of exceptions compare to super then the program will not compile successfully.
So d) is correct.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
05. class A{
void display() throws IOException,FileNotFoundException {
System.out.println(“class A”); }
}
class B extends A {
void display() throws ClassNotFoundException, SQLException {
System.out.println(“class B”); }
}
class Test {
Public static void main(String args[]){
B b=new B();
b.display(); 
}
}
Output?
A. class A B. class B
C. class A, class B D. compile time error

Answer and Explanation

Answer: compile time error

Explanation:
In the above program class A is super class and it is throwing two exceptions, in class B which is child class of A must throw less number of exceptions compare to A and child class B must throw same type of exceptions as class A , if the child class throws other type of exceptions compare to super then the program will not compile successfully.
So d) is correct.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
06. Given a valid DateFormat object named df, and

1. Date d = new Date(0L);

2. String ds = "December 15, 2004";

3. // insert code here

What updates d's value with the date represented by ds?
A. 3. d = df.parse(ds); B. 3. d = df.getDate(ds);
C. 3. try {
4. d = df.parse(ds);
5.} catch(ParseException e) { };
D. 3. try {
4. d = df.getDate(ds);
5.} catch(ParseException e) { };

Answer and Explanation

Answer: 3. try {
4. d = df.parse(ds);
5.} catch(ParseException e) { };

Explanation:
Whenever you use parse( ) function it always throws ParseException. We have to enclose with try, catch block.
So C) is correct.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
07. Given:
public static void parse(String str) {
try {
float f = Float.parseFloat(str);
} catch (NumberFormatException nfe) {
f = 0;
} finally {
System.out.println(f);
}
}
public static void main(String[] args) {
parse("invalid");
}

What is the result?
A. 0.0 B. Compilation fails.
C. A ParseException is thrown by the parse method at runtime. D. A NumberFormatException is thrown by the parse method at runtime.

Answer and Explanation

Answer: Compilation fails.

Explanation:
Compilation fails because all the code is not there in any of the class. One more reason is inside a try they have declared a variable f and they are trying to access from finally block, it will throw compile time error.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
08. Given:
1. class A {
2. public void process() { System.out.print("A,"); }}
3. class B extends A {
4. public void process() throws IOException {
5. super.process();
6. System.out.print("B,");
7. throw new IOException();
8.}
9. public static void main(String[] args) {
10. try { new B().process(); }
11. catch (IOException e) { System.out.println("Exception"); }}}

What is the result?
A. Exception B. A,B,Exception
C. Compilation fails because of an error in line 10. D. Compilation fails because of an error in line 4.
E. A NullPointerException is thrown at runtime.

Answer and Explanation

Answer: Compilation fails because of an error in line 4.

Explanation:
process( ) in super class A is not throwing any exception. When we override in child class it should not throw any exception. If in child class overridden method throws an exception then it fails during compilation time.
So D) is correct.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
09. 1 class TestException extends Exception { }
2 class A {
3 public String sayHello(String name) throws TestException {
4 if(name == null) throw new TestException();
5 return "Hello " + name; }
6 }
7 public class TestA {
8 public static void main(String[] args) {
9 new A().sayHello("Aiko");
10 } }
A. Compilation succeeds. B. Class A does not compile
C. The method declared on line 9 cannot be modified to throw TestException. D. TestA compiles if line 9 is enclosed in a try/catch block that catches TestException.

Answer and Explanation

Answer: TestA compiles if line 9 is enclosed in a try/catch block that catches TestException.

Explanation:
From class A there is a method sayHello() which is throwing an exception. From main ( ) when we call that sayHello() it should be enclosed with try block.
So D) is correct.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
10. class A{
static void test() {
try {
String x = null;
System.out.print(x.toString() + " ");
}finally {
 System.out.print("finally "); 
}
}
public static void main(String[] args) {
try { test();
}
catch (Exception ex) {
 System.out.print("exception "); }
}
}

What is the result?
A. null B. finally
C. null finally D. Compilation fails.
E. finally exception

Answer and Explanation

Answer: finally exception

Explanation:
Just normal flow of execution.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum



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