© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu

Home Placements Interview Questions Java Interview Questions And Answers Java Interview Questions On Flow Control and Exception Handling ▼

Java Interview Questions On Flow Control and Exception Handling

35. What is the result of the following code when you compile and run?
public class ThrowDemo {
static void demoMethod() {
try {
throw new NullPointerException("demo");
} catch(NullPointerException e) {
System.out.println("Caught inside demoMethod.");
throw e; // re-throw the exception
}
}
public static void main(String args[]) {
try {
demoMethod();
} catch(NullPointerException e) {
System.out.println("Recaught: " + e);
}
}
}

[a] Compilation error
[b] Runtime error
[c] Compile successfully, nothing is printed.
[d] Caught inside demoMethod. followed by Recaught: java.lang.NullPointerException: demo

Answer
[d] Caught inside demoMethod. followed by Recaught: java.lang.NullPointerException: demo

36. Select the correct answer?
public class ThrowsDemo {
static void throwMethod() {
System.out.println("Inside throwMethod.");
throw new IllegalAccessException("demo");
}
public static void main(String args[]) {
try {
throwMethod();
} catch (IllegalAccessException e) {
System.out.println("Caught " + e);
}
}
}
[a] Compilation error
[b] Runtime error
[c] Compile successfully, nothing is printed.
[d] inside demoMethod. followed by caught: java.lang.IllegalAccessException: demo

Answer
[a] Compilation error

Previous End
1 2 3 4 5 6 7 8 9
SLogix Student Projects
bottom