Java Homework! Using the provided code consider the following 3 questions: What
ID: 3686047 • Letter: J
Question
Java Homework!
Using the provided code consider the following 3 questions:
What is the output of the following code?
What is output if the following line in main is replaced?
int num = -99;
What is output if the following line in main is replaced?
int num = 0;
(NOTE: Assume there is a classNegativeNumberException defined in this package.)
public static void main(String[] args) {
try {
int num = 99;
sampleMethod(num);
} catch(Exception ex) {
System.out.print("CaughtMain");
}
}
public static void sampleMethod(int n) throws Exception {
try {
if(n>0)
throw new Exception();
else if(n<0)
throw new NegativeNumberException();
else
System.out.print("Good");
System.out.print("InSample");
}catch(NegativeNumberException ex) {
System.out.print("CaughtSample");
} finally {
System.out.print("InFinal");
}
System.out.print("AfterFinal");
}
}
Explanation / Answer
num = 99
InFinalCaughtMain
num = -99
CaughtSampleInFinalAfterFinal
num = 0
GoodInSampleInFinalAfterFinal
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.