1. Java 7: Language Enhancements Flashcards
What are the two things you need to know for using a String in a switch expression?
- check for null value
- the value is case-sensitive (fe: wOrD does not match a case WORD)
What are the integer literals?
Decimal: 26
Hexadecimal: 0x1A
Binary: 0b11010 (new in Java 7)
What is the result of the following:
float f1 = 0b0001;
float f2 = 0B0001F;
Compilation fails, Binary literal (new in Java 7) can only be used with integer type (byte, short, int, long)
Which of these are valid?
- int x1 = 5_2;
- float f2 = 3_.1415F;
- long l3 = 99_99_L;
- int x4 = 0x5_2;
- int x5 = 0B0_0_0;
- int x6 = 52_;
- int x7 = 0_x52;
- int x8 = _52;
Valid: 1,4,5
Invalid:
2: underscore next to the decimal point
3: underscore next to the L.
6: underscore at the end of the literal.
7: underscore between the 0 and x of the hexadecimal literal.
8: not a integer literal but an identifier
What are the requirements for a resource that can be used in a try-with-resource construction?
Object (the resource) must implement the auto-closable interface (or the closable interface with extends the autoclosable interface.)
What is the difference between the AutoClosable and the Closable interface?
The void close() method of the AutoClosable throws an Exception while the Closable’s close() method trhwos an IOException.
What is the result of the following method, considering the first line of the file contains the text “Hello World”.
public void main(String args[]) { try(BufferedReader br = new BufferedReader(new FileReader("text.txt))) { return br.readLine(); } }
Compile error!
BufferedReader implements the AutoClosable interface, the both the close method and the readLine call could throw an IOException. The IOException is not declared in the method signature or as a catch clause.
True or False:
A Try-with-resource construction can only be used with one Resource (object that implements AutoClosable interface).
False
You declare en initialize multiple resources seperated by semicolons. The resources will be closed in the opposite direction in which they are created.
True or False:
The catch are finally block are optional when using the try-with-resource construction.
True
The catch and finally are optional. But they can be declared. The catch and finnaly block are run after the resources declared have been closed.
True or False:
When using the try-with-resource construction you need to define at least one catch clause catching a IOException/Exception or the method signature should have a throws clause.
True
The creation could throw an exception (fe FileNotFoundException for file handling) and the handling of the IOException/Exception from either the AutoClosable or Closable interface.
Given the following code:
class MyResource implement AutoClosable { public void close() throws IOException { throw new IOException(); } }
try(MyResource r = new MyResource()) { throw new IllegalArgumentException(); } catch(Exception e) { e.printStacktrace(); }
Witch Exception will be catched by the catch?
IllegalArgumentException
When the try-with-resource throws an exception from the close method and the try block also throws an exception. Then the Resource close exception is suppressed.
Describe the methods for adding and retrieving suppressed exceptions from a Throwable instance?
public final void addSuppressed(Throwable exception)
public final Throwable[] getSuppressed();
Considering the following code:
try { throw new IOException(); } catch (IOException e | Exception e) { e.printStackTrace(): } What is the result?
Compilation failer! Syntax error.
The correct syntax for a multi-catch is: try { // } catch (IOException | SQLException e) { }
Also when using a multi-catch the declared exceptions can’t be sub-classes or super classes of eachother.
Rewrite the following declaration an initzialistation leveraging Diamond operators:
Map> myMap = new HashMap>();
Map> myMap = new HashMap<>();
What is the difference between the following: List l1 = new ArrayList<>(); List l2 = new ArrayList();
l2 is a raw type hashmap, while l1 is of type String using the diamond operator.