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)) {
..
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Can you provide try-with resources statements without any explicit catch or finally blocks?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

If an assertion is false, what does it throw?

A

AssertionError, derived from Error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly