Advanced Flashcards

1
Q

What are functional interfaces? List some that come with the JRE for Java 8

A

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 .

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

What are lambda expressions?

A

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.

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

What is try-with-resources? What interface must the resource implement to use this feature?

A

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

How to make numbers in your code more readable?

A

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.

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

Which collections cannot hold null values?

A

HashTable, TreeSet, ArrayDeque, PriortyQueue

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

If 2 interfaces have default methods and you implement both, what happens?

A

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

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

If 2 interfaces have the same variable names and you implement both, what happens?

A

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;

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

Why does HashTable not take null key?

A

The hash table hashes the keys given as input, and the null value cannot be hashed

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

What new syntax for creating variables was introduced with Java 10?

A

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.

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

Is there an interactive REPL tool for Java like there is for languages like Python?

A

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.

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

What are collection factory methods?

A

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.

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