InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
What is the lambda expression in Java and How does a lambda expression relate to a functional interface? |
|
Answer» Lambda expression is a TYPE of function without a name. It may or may not have results and parameters. It is KNOWN as an anonymous function as it does not have type information by itself. It is executed on-demand. It is beneficial in iterating, FILTERING, and extracting data from a collection. As lambda expressions are similar to anonymous functions, they can only be applied to the single abstract method of Functional Interface. It will infer the return type, type, and several arguments from the SIGNATURE of the abstract method of functional interface. |
|
| 2. |
What are the various categories of pre-defined function interfaces? |
|
Answer» Function: To transform ARGUMENTS in returnable VALUE. Predicate: To PERFORM a test and return a BOOLEAN value. Consumer: Accept arguments but do not return any values. Supplier: Do not accept any arguments but return a value. Operator: Perform a reduction type operation that ACCEPTS the same input types. |
|
| 3. |
What are some standard Java pre-defined functional interfaces? |
|
Answer» Some of the famous pre-defined functional interfaces from previous Java versions are Runnable, Callable, COMPARATOR, and Comparable. While Java 8 introduces functional interfaces like SUPPLIER, Consumer, Predicate, etc. Please refer to the java.util.function doc for other predefined functional interfaces and its description introduced in Java 8. Runnable: use to EXECUTE the instances of a class over another thread with no arguments and no return value. Callable: use to execute the instances of a class over another thread with no arguments and it EITHER returns a value or throws an exception. Comparator: use to sort different objects in a user-defined ORDER Comparable: use to sort objects in the natural sort order |
|
| 4. |
What are static methods in Interfaces? |
|
Answer» Static METHODS, which contains METHOD implementation is owned by the interface and is invoked USING the name of the interface, it is SUITABLE for defining the utility methods and cannot be OVERRIDDEN. |
|
| 5. |
What is the default method, and why is it required? |
|
Answer» A method in the interface that has a predefined body is known as the default method. It uses the keyword default. default methods were introduced in Java 8 to have 'Backward Compatibility in case JDK modifies any interfaces. In case a new abstract method is added to the interface, all classes implementing the interface will break and will have to implement the new method. With default methods, there will not be any impact on the interface implementing classes. default methods can be OVERRIDDEN if NEEDED in the implementation. ALSO, it does not qualify as synchronized or final. @FunctionalInterface // Annotation is optional public interface Foo() { // Default Method - Optional can be 0 or more public default String HelloWorld() { return "Hello WORLD"; } // SINGLE Abstract Method public void bar(); } |
|
| 6. |
Can a functional interface extend/inherit another interface? |
|
Answer» A functional interface cannot EXTEND another interface with ABSTRACT methods as it will void the rule of ONE abstract METHOD per functional interface. E.g: interface Parent { public int parentMethod(); } @FunctionalInterface // This cannot be FunctionalInterface interface Child extends Parent { public int childMethod(); // It will also extend the abstract method of the Parent Interface // Hence it will have more than one abstract method // And will give a compiler error }It can extend other interfaces which do not have any abstract method and only have the DEFAULT, static, another class is overridden, and normal methods. For eg: interface Parent { public void parentMethod(){ System.out.println("Hello"); } } @FunctionalInterface interface Child extends Parent { public int childMethod(); } |
|
| 7. |
What are functional or SAM interfaces? |
|
Answer» Functional Interfaces are an interface with only one ABSTRACT method. Due to which it is also known as the Single Abstract Method (SAM) interface. It is known as a functional interface because it wraps a function as an interface or in other words a function is represented by a single abstract method of the interface. Functional interfaces can have any NUMBER of default, static, and overridden METHODS. For declaring Functional Interfaces @FunctionalInterface annotation is optional to use. If this annotation is used for interfaces with more than one abstract method, it will generate a compiler error. @FunctionalInterface // Annotation is optional public interface Foo() { // Default Method - Optional can be 0 or more public default STRING HelloWorld() { return "Hello World"; } // Static Method - Optional can be 0 or more public static String CustomMessage(String msg) { return msg; } // Single Abstract Method public void bar(); } public class FooImplementation implements Foo { // Default Method - Optional to Override@Overridepublic default String HelloWorld() { return "Hello Java 8"; } // Method Override@Overridepublic void bar() { System.out.println(“Hello World”);} } public static void main(String[] args) { FooImplementation fi = new FooImplementation();System.out.println(fi.HelloWorld());System.out.println(fi.CustomMessage(“Hi”));fi.bar();} |
|
| 8. |
What is MetaSpace? How does it differ from PermGen? |
|
Answer» JVM PremGen: MetaData information of classes was STORED in PremGen (Permanent-Generation) memory TYPE before Java 8. PremGen is fixed in SIZE and cannot be dynamically resized. It was a contiguous Java Heap Memory. MetaSpace: Java 8 stores the MetaData of classes in NATIVE memory CALLED 'MetaSpace'. It is not a contiguous Heap Memory and hence can be grown dynamically which helps to overcome the size constraints. This improves the garbage collection, auto-tuning, and de-allocation of metadata. |
|
| 9. |
What are the significant advantages of Java 8? |
Answer»
|
|
| 10. |
In which programming paradigm Java 8 falls? |
Answer»
|
|
| 11. |
Describe the newly added features in Java 8? |
||||||||||||||||||
|
Answer» Here are the newly added features of Java 8:
|
|||||||||||||||||||