OCP Flashcards – 6. Exceptions and Assertions

OCP Exam Objective 6 – Exceptions and Assertions

Flashcard OCP.6.Exceptions.and.Assertions.001

What method is defined in the AutoCloseable interface?

Click here to view answer
close()

 

Flashcard OCP.6.Exceptions.and.Assertions.002

What is the throws clause?

Click here to view answer
It is a part of a method declaration. The throws clause lists exceptions that can be thrown by the method.

 

Flashcard OCP.6.Exceptions.and.Assertions.003

What the calling method should do if the called method declares a throws clause?

Click here to view answer
That depends on the exception. If it’s an unchecked exception, the caller can do whatever it wants, even disregard the exception completely. But if it’s a checked exception, the calling method should handle it, or declare it to be re-thrown.

 

Flashcard OCP.6.Exceptions.and.Assertions.004

What the throw statement be used for?

Click here to view answer
The throw statement is used to throw an exception from a method, constructor, or an initialization block.

 

Flashcard OCP.6.Exceptions.and.Assertions.005

What does the Catch-or-Specify Requirement mean?

Click here to view answer
The CSR means that code that might throw checked exceptions must be enclosed by either of the following:

  • a try-catch block that catches the exception by providing an appropriate handler;
  • a method that specifies that it can throw the exception by providing a throws clause that lists the exception.

 

Flashcard OCP.6.Exceptions.and.Assertions.006

Which Throwable types are not subject to the Catch-or-Specify Requirement?

Click here to view answer
RuntimeException and Error

 

Flashcard OCP.6.Exceptions.and.Assertions.007

What is the restriction imposed by the throws clause in respect to the type of the exception thrown by a method?

Click here to view answer
A method can throw a subclass of the exception mentioned in its throws clause but not a superclass.

 

Flashcard OCP.6.Exceptions.and.Assertions.008

When is a finally block executed?

Click here to view answer
After a try(-catch) block, whether an exception is thrown or not

 

Flashcard OCP.6.Exceptions.and.Assertions.009

What is used to separate exception types in a multi-catch statement?

Click here to view answer
The single pipe character, |

 

Flashcard OCP.6.Exceptions.and.Assertions.010

When does the assert statement throw an AssertionError?

Click here to view answer
When its expression evaluates to false

 

Flashcard OCP.6.Exceptions.and.Assertions.011

What must a catch block handle when used with a try-with-resources?

Click here to view answer
Exceptions thrown by the implicit closing of resources

 

Flashcard OCP.6.Exceptions.and.Assertions.012

Is it possible to have catch and finally blocks with a try-with-resources?

Click here to view answer
Yes, a try-with-resources statement can have catch and finally blocks just like an ordinary try statement.

 

Flashcard OCP.6.Exceptions.and.Assertions.013

True or false: in a try-with-resources statement, any catch or finally block is run after the resources declared have been closed.

Click here to view answer
True

 

Flashcard OCP.6.Exceptions.and.Assertions.014

What interface(s) must a resource implement to be eligible for declaration in a try-with-resources statement?

Click here to view answer
AutoCloseable or Closeable

 

Flashcard OCP.6.Exceptions.and.Assertions.015

What restriction is applied to the exception classes that can be declared within the same multi-catch statement?

Click here to view answer
The declared exceptions may not be related by inheritance.

 

Flashcard OCP.6.Exceptions.and.Assertions.016

In what way the AutoCloseable interface differs from Closeable?

Click here to view answer
The AutoCloseable#close() throws an Exception, while the close() method in the Closeable interface throws an IOException.

 

Flashcard OCP.6.Exceptions.and.Assertions.017

When is the close() method of a resource invoked in a try-with-resources block?

Click here to view answer
Whenever the resource is being closed regardless of whether or not an exception has been thrown

 

Flashcard OCP.6.Exceptions.and.Assertions.018

What limitation is applied to the catch parameter in a multi-catch statement?

Click here to view answer
The catch parameter cannot be reassigned because it’s implicitly final

 

Flashcard OCP.6.Exceptions.and.Assertions.019

What is the default setting for assertions?

Click here to view answer
Disabled

 

Flashcard OCP.6.Exceptions.and.Assertions.020

What is the order of the close() method invocation in case of multiple resources declared in a try-with-resources statement?

Click here to view answer
The resources’ close() method is invoked in the reverse order of their instantiation.

 

Flashcard OCP.6.Exceptions.and.Assertions.021

Which method of the Throwable class returns a short description of an exception or error?

Click here to view answer
toString()

 

Flashcard OCP.6.Exceptions.and.Assertions.022

What are suppressed exceptions?

Click here to view answer
If an exception is thrown from the try block and one or more exceptions are thrown from the try-with-resources statement, then those exceptions thrown from the try-with-resources statement are suppressed.

 

Flashcard OCP.6.Exceptions.and.Assertions.023

How can we get the suppressed exceptions?

Click here to view answer
By calling the Throwable#getSuppressed() method.

 

Flashcard OCP.6.Exceptions.and.Assertions.024

Which does getSuppressed() return: an array or List?

Click here to view answer
Throwable[]

 

Flashcard OCP.6.Exceptions.and.Assertions.025

Which Java compiler’s switch enables assert statements?

Click here to view answer
-ea

 

Flashcard OCP.6.Exceptions.and.Assertions.026

What checked exception is typically thrown by read/write methods?

Click here to view answer
IOException

 

Flashcard OCP.6.Exceptions.and.Assertions.027

Will this class compile?

class Test{
    void run(){
        try{ throw new RuntimeException("RTE");}
        catch(RuntimeException e){ throw new IOException("IOE");}
        finally{
            return;
        }
    }
       public static void main(String[] args) {
        new Test().run();
    }
}
Click here to view answer
Yes, the code compiles; it prints nothing, though. And remember: placing return in finally is ALWAYS unadvisable as it will discard unhandled exceptions in try and/or catch. Also note that in this particular case removing return renders the code uncompilable.

Leave Comment

Your email address will not be published.