JAVA HOMEWORK HELP 1. Which of the following is true? Select all that apply. Sel
ID: 3684831 • Letter: J
Question
JAVA HOMEWORK HELP
1.
Which of the following is true? Select all that apply.
Select one or more:
A.Checked exceptions must be caught or listed in the throws clause of a method.
B.Unchecked exceptions must be caught or listed in the throws clause of a method.
C.Any kind of exception (checked or unchecked) can be listed in the throws clause of a method.
D.Checked exceptions can be caught in a method.
E.Unchecked exceptions can be caught in a method.
2.
Which is true about the following method?
public void doSomething(int n) {
if(n<0)
throw new Exception("Negative Number");
}
Select one or more:
A.it will compile and run without crashing
B.it will not compile, but it could be fixed by adding a try/catch block
C.it will not compile, but it could be fixed by adding a throws clause to the method header
3.
Which of the following is ways that Java can handle an exception? Select all that apply.
Select one or more:
A.handle (catch) the exception where it occurred
B.throw the exception to the Exception class to be handled
C.ignore the exception
D.propagate the exception to another method and handle it there
4.
The code in a finally clause will execute under which conditions? Select all that apply.
Select one or more:
A.the try block that precedes it throws an exception that is not caught
B.the try block that precedes it throws an exception that is caught
C.the try block that precedes it does not throw an exception
5.
Consider the following code fragments:
public void methodC() {
...
methodB();
}
public void methodB() {
...
methodA();
}
public void methodA() {
...
// an unchecked exception is thrown
}
If methodC is invoked, where can the unchecked exception be caught and handled? Select all that apply.
Select one or more:
A.methodA
B.whatever method invokes methodC
C.methodC
D.methodB
6.
Which of the following methods invoked on a String object str might throw aStringIndexOutOfBoundsException?
Select one:
A.str.equals(str)
B.str.replace('a', 'A')
C.str.length()
D.str.charAt(2)
Explanation / Answer
1. a. Checked exceptions must be caught or listed in the throws clause of a method.
c. Any kind of exception (checked or unchecked) can be listed in the throws clause of a method.
d. Checked exceptions can be caught in a method.
e. Unchecked exceptions can be caught in a method.
2. b. it will not compile, but it could be fixed by adding a try/catch block
c. it will not compile, but it could be fixed by adding a throws clause to the method header
3. a. propagate the exception to another method and handle it there
d. handle (catch) the exception where it occurred
4. a. the try block that precedes it does not throw an exception
b. the try block that precedes it throws an exception that is not caught
c. the try block that precedes it throws an exception that is caught
5. a. methodC
b. methodB
c. methodA
d. whatever method invokes methodC
6. d. str.charAt(2)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.