Java: KIỂM TRA KIẾN THỨC EXCEPTION
QUESTIONS
1. ___________ is a try statement that declares one or more resources.
(A) |
try |
(B) |
try-catch |
(C) |
try-with-resources |
(D) |
try-catch-finally |
Choose answer:
(A) |
A |
(C) |
C |
(B) |
B, C |
(D) |
B, D |
2. Which of the following statements about assertions are true?
(A) |
Assertions should be used to check the parameters of a public method |
(B) |
Do not use assertions to do any task that the application requires for correct operation. |
(C) |
An assertion is a statement in the Java that allows the programmer to test his/ her assumptions about the program. |
(D) |
Assertion checking is enabled by default. |
Choose answer:
(A) |
A |
(C) |
A, C |
(B) |
B, C |
(D) |
B, D |
3. Match the following keywords with the corresponding descriptions with respect to exception handling.
Access Specifier |
Description |
||
(A) |
throw |
1. |
Used to test an assumption about a code in a program. |
(B) |
throws |
2. |
Used to throw the exception declared by a method. |
(C) |
assert |
3. |
Used to handle the appropriate exception raised in the try block. |
(D) |
catch |
4. |
Used to declare the exceptions that a method is liable to throw. |
Choose answer:
(A) |
A-4, B-1, C-2, D-3 |
(C) |
A-3, B-2, C-1, D-4 |
(B) |
A-1, B-2, C-3, D-4 |
(D) |
A-2, B-4, C-1, D-3 |
4. Which of the following is the correct syntax to create a user-defined exception class?
(A) |
public class <ExceptionName> implements Exception {} |
(B) |
public static final class <ExceptionName> extends Exception {} |
(C) |
public class <ExceptionName> extends Throwable {} |
(D) |
public class <ExceptionName> extends Exception {} |
Choose answer:
(A) |
A |
(C) |
C |
(B) |
B |
(D) |
D |
5. Consider the following code:
public void testException(){ if(condition==true) System.out.println("True"); else throw new MyException(); }
The code is giving the warning ‘unreported exception project.MyException. Must be caught or declared to be thrown’. What change must be made in line 1 to resolve the issue?
(A) |
public void testException() throw MyException{ |
(B) |
public void testException()throws MyException(){ |
(C) |
public void testException() throws MyException{ |
(D) |
public void throw MyException testException(){ |
Choose answer:
(A) |
A |
(C) |
C |
(B) |
B |
(D) |
D |
6. Consider the following code:
public void addEmployee{ ArrayLise empNames = new ArrayList(); try{ empNames.remove(0); } catch(IndexOutOfBoundsException|RuntimeException ex){ throw ex; }
The code consists of a method to remove employee names from an ArrayList. Which of the following programming concept(s) have been used in the code?
(A) |
try block with single catch block |
(B) |
try block with multiple catch blocks |
(C) |
Single catch block for multiple exceptions |
(D) |
Assertion |
Choose answer:
(A) |
A |
(C) |
A, C |
(B) |
B |
(D) |
C, D |