Advanced Flashcards
What are functional interfaces? List some that come with the JRE for Java 8
A functional interface in Java is an interface that contains only a single abstract (unimplemented) method. A functional interface can contain default and static methods which do have an implementation, in addition to the single unimplemented method.
Some of the useful java 8 functional interfaces are Consumer , Supplier , Function and Predicate .
What are lambda expressions?
A lambda expression is a short block of code which takes in parameters and returns a value. Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method.
What is try-with-resources? What interface must the resource implement to use this feature?
The try-with-resources statement is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.
How to make numbers in your code more readable?
You can use underscores to make numbers more readable. Note
int x = 5_2
or
int x = 5______________2
Both of the above examples are valid.
Which collections cannot hold null values?
HashTable, TreeSet, ArrayDeque, PriortyQueue
If 2 interfaces have default methods and you implement both, what happens?
The code will NOT compile unless you override the method. However, the code WILL compile if one interface is implemented further up in the class hierarchy than the other - in this case, the closest method implementation in the hierarchy will be called
If 2 interfaces have the same variable names and you implement both, what happens?
The code will compile unless you make a reference to the variable (this is an ambiguous reference). You must explicitly define the variable by using the interface name: int a = INTERFACENAME.a;
Why does HashTable not take null key?
The hash table hashes the keys given as input, and the null value cannot be hashed
What new syntax for creating variables was introduced with Java 10?
You know, var keyword is added to the Java language since Java 10 (JDK 10), supporting a new feature called local variable type inference, in which Java compiler guesses the type of the variables based on the surround context - allowing programmers to not declare the type explicitly.
Is there an interactive REPL tool for Java like there is for languages like Python?
Oracle corp has come up with this tool called jshell. To display the “Hello World” in JShell, all you have to write is this: jshell> System.
What are collection factory methods?
Factory methods are special type of static methods that are used to create unmodifiable instances of collections. It means we can use these methods to create list, set and map of small number of elements.