In java language Question 1 Which of the following is true? Select all that appl
ID: 3684855 • Letter: I
Question
In java language
Question 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.
Question 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
Question 3
Which of the following is ways that Java can handle an exception? Select all that apply.
Select one or more:
a. propagate the exception to another method and handle it there
b. throw the exception to the Exception class to be handled
c. ignore the exception
d. handle (catch) the exception where it occurred
Question 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 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
Question 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. methodC
b. methodB
c. methodA
d. whatever method invokes methodC
Question 6
Which of the following methods invoked on a String object str might throw aStringIndexOutOfBoundsException?
Select one:
a. str.replace('a', 'A')
b. str.equals(str)
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
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.