Language Enhancements Flashcards
When initialize variable from primitive type does new object is used?
No
Using String in switch statement uses equals method to compare Strings
Yes
The integral types byte short int long can be expressed using the binary number system…
Yes
(byte)0b00100001
Using binary literals 0b or 0B is the same
Yes
We can use any number of underscores _ anywhere between digits in numerical literals
Yes
We can place underscore in adjacent to the decimal point literal
No 3.14_5555 is invalid
We can place underscore character prior to F or L suffix
No
int v = _90; is valid…
No
Which classes can be used as part of try with resources block…
Those that implement AutoClosable interface
The compiler can check the exact type of exception can be thrown and catch it in general Exception but declare it in the throws clause as specific types…
Yes
Any object that implements …, which includes all objects which implement java.io.Closeable can be used as a resource.
java.lang.AutoCloseable
The class BufferedReader, in Java SE 7 and later, implements the interface …
java.lang.AutoCloseable
Using try with resources if an exception is thrown from both try and finally blocks the exception from try is thrown and the exception from finally is suppressed the opposite of prior to java 7
Yes
Note that the close methods of resources are called in …
the opposite order of their creation.
Note: A try-with-resources statement can have catch and finally blocks just like an ordinary try statement. In a try-with-resources statement, any catch or finally block is run after the resources declared have been closed.
Yes
The following are the methods and constructors in Throwable that support chained exceptions….
Throwable getCause()
Throwable initCause(Throwable)
Throwable(String, Throwable)
Throwable(Throwable)
Note that it is a common mistake to forget the diamond operator
< > in the initialization expression, as in
Pair worldCup = new Pair (2010, “South Africa”); shows the compiler…
warning when you forget to use the diamond syntax.
Compiler warning when you forget to use diamond syntax…
Yes
subtyping does not work for generic parameters…
Yes
type parameters for generics have a limitation: generic type parameters should match exactly for assignments. to overcome this subtyping problem, you can use wildcard types…
Yes
You cannot instantiate a generic type using new operator. For example, assuming mem is a field, the following statement will result in a compiler error: T mem = new T(); // wrong usage - compiler error
Yes
You can declare non-static fields of type T, but not of static fields of type T. For example, class X { T instanceMem; // okay static T statMem; // wrong usage - compiler error
Yes
It is not possible to have generic exception classes; as a result, the following will not compile: class GenericException extends Throwable { } // wrong usage - compiler error
Yes
To use collections like HashSet, you must override…
the hashCode() and equals() methods correctly.
A class implementing this interface can be used for iterating with a for each statement…
Iterable
In a Deque you can insert or remove elements from both the ends
Yes
Note that Map hierarchy does not extend the Collection interface…
Yes
You can traverse in both forward and reverse directions if a class implements the ….
ListIterator interface
You can traverse over the container in the forward direction if a class implements the …
Iterator interface
ArrayList Fast to search, but slow to insert or delete. Allows duplicates
Yes
Methods in the Iterator Interface…
boolean hasNext() E next() void remove()
Before we call remove on iterator we must call next
Yes
Methods in the ListIterator Interface (in Addition to Iterator Methods…
boolean hasPrevious() Element previous() int nextIndex() int previousIndex() void set(Element) void add(Element
So, insertion and deletion is very fast in…
LinkedList
The container classes store references to objects, so you cannot use primitive types with any of the collection classes. Since you have wrapper classes and auto-boxing, it is not a big problem—that is why you use Character instead of char here
Yes
the methods hashCode() and equals() need to be consistent for a class. For practical purposes, ensure that you follow this one rule: the hashCode() method should return the same hash value for two objects if the equals() method returns true for them
Yes
A Queue follows FIFO mechanism…
Yes
The Deque interface was introduced in Java 6 in java.util.collection package
Yes
Commonly Used Methods in the Deque Interface…
void addFirst(Element) void addLast(Element) Element removeFirst() Element removeLast() Element getFirst() Element getLast()
Using deque, they do not raise exception on failure…
boolean offerFirst(Element) boolean offerLast(Element) Element pollFirst() Element pollLast() Element peekFirst() Element peekLast
There are three concrete implementations of the Deque interface…
LinkedList, ArrayDeque, and LinkedBlockingDeque
remember that you cannot add elements to the List returned by the Arrays.asList() method. But, you can make changes to the elements in the returned List, and the changes made to that List are reflected back in the array
Yes
You use the extends keyword for both class type as well as an interface when specifying bounded types in generics. For specifying multiple base types, you use the & symbol. For example, in List, ? will match types, extending both
Yes
An interface can be declared in a class or interface as inner…
Yes
A rule of thumb is to use has-a and is-a phrases for composition and inheritance, respectively to assess when to use composition or inheritance…
Yes
Class inheritance implies an is-a relationship, interface inheritance implies an is-like-a relationship, and composition implies a has-a relationship…
Yes
Use inheritance when a subclass specifies a base class, so that you can exploit dynamic polymorphism. in other cases, use composition to get code that is easy to change and loosely coupled. in summary, favor composition over inheritance…
Yes
In composition, the lifetime of the contained object and the container object is the same, whereas that is not the case with aggregation. For example, a computer object and a CPU object share a composition relationship, while a library object and a book object share an aggregation relationship…
Yes