Exception and Assertions Flashcards
1
Q
Suppose you have to instantiate a scanner object, what is the syntax for the try-with-resource block?
A
try(Scanner consoleScanner = new Scanner(System.in)) { .. }
2
Q
Can you provide try-with resources statements without any explicit catch or finally blocks?
A
Yes
3
Q
What is the requirements for a resource to be used with a try-with-resource statement?
A
That resource must implement java.lang.AutoCloseable interface
4
Q
If an assertion is false, what does it throw?
A
AssertionError, derived from Error
5
Q
How do you enable assertions for class/packages
?
A
To specify a class, use the class name. To specify the package name, use the package name followed by three dots …
java -ea:{Class} myPackage.MyProgram
java -ea:{package}… myPackage.MyProgram
- When you use package name in the ea/da flag, the flag applies to its sub packages as well