1. Java 7: Language Enhancements Flashcards

1
Q

What are the two things you need to know for using a String in a switch expression?

A
  • check for null value

- the value is case-sensitive (fe: wOrD does not match a case WORD)

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

What are the integer literals?

A

Decimal: 26
Hexadecimal: 0x1A
Binary: 0b11010 (new in Java 7)

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

What is the result of the following:
float f1 = 0b0001;
float f2 = 0B0001F;

A

Compilation fails, Binary literal (new in Java 7) can only be used with integer type (byte, short, int, long)

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

Which of these are valid?

  1. int x1 = 5_2;
  2. float f2 = 3_.1415F;
  3. long l3 = 99_99_L;
  4. int x4 = 0x5_2;
  5. int x5 = 0B0_0_0;
  6. int x6 = 52_;
  7. int x7 = 0_x52;
  8. int x8 = _52;
A

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

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

What are the requirements for a resource that can be used in a try-with-resource construction?

A

Object (the resource) must implement the auto-closable interface (or the closable interface with extends the autoclosable interface.)

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

What is the difference between the AutoClosable and the Closable interface?

A

The void close() method of the AutoClosable throws an Exception while the Closable’s close() method trhwos an IOException.

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

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();
   }
}
A

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.

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

True or False:

A Try-with-resource construction can only be used with one Resource (object that implements AutoClosable interface).

A

False

You declare en initialize multiple resources seperated by semicolons. The resources will be closed in the opposite direction in which they are created.

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

True or False:

The catch are finally block are optional when using the try-with-resource construction.

A

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.

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

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.

A

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.

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

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?

A

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.

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

Describe the methods for adding and retrieving suppressed exceptions from a Throwable instance?

A

public final void addSuppressed(Throwable exception)

public final Throwable[] getSuppressed();

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

Considering the following code:

try {
throw new IOException();
} catch (IOException e | Exception e) {
e.printStackTrace():
}
What is the result?
A

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.

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

Rewrite the following declaration an initzialistation leveraging Diamond operators:
Map> myMap = new HashMap>();

A

Map> myMap = new HashMap<>();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
What is the difference between the following:
List l1 = new ArrayList<>();
List l2 = new ArrayList();
A

l2 is a raw type hashmap, while l1 is of type String using the diamond operator.

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

What is the result of the following:
List l1 = new ArrayList<>();
l1.add(“A”);
l1.addAll(new ArrayList<>());

A

Compiler error (line 3, addAll)!

You can only use automatic type inference if the target type is clear from the context when creating the object.

17
Q

Considering the following code, what should be added at :

public void myMethod(String ex) throws  {
try {
   if(ex.equals("first")) {
      throw new FirstException();
   } else {
      throw new SecondException();
   }
} catch (Exception e) {
   throw e;
}
A

Exception
or
FirstException, SecondException

Since Java 7 you can specify more precise throws clauses for rethrows of exceptions.