Java 8 before Interview (Part 1)

It  is  high - time  to  learn  Java 8

Why Java 8 ?  

     Java 8 is a major feature release for Java programming language. This blog has an introductory question set that explains the Interview Questions asked on Java 8 and their usage in a simple way.
     Recently many of the companies are moving on from Java 1.7 to Java 8 which is leading a Java developer to understand atleast the basics of this Java version.
     So, do not miss going through the question set if you are appearing for Java interview.

1. GENERAL QUESTIONS :

Q1. What are the Features of Java8 ?
  1. Functional Interfaces
  2. Lambda Expression
  3. Method References
  4. Default/Static Methods in Interface
  5. Static Methods in Interface
  6. Stream API
  7. Parallel array sorting
  8. Optional Class
  9. DateTime API
  10. Base64 encode/decode
  11. Collectors class
  12. IO enhancements
  13. Concurrency enhancements
  14. JDBC enhancements
  15. Nashorn JS engine
  16. forEach() Method 
  17. StringJoiner class
  18. Type Inference
  19. Parameter Reflection
  20. etc.

2. FUNCTIONAL INTERFACES :

Q2a. What are Functional Interfaces?
  • Also known as "Single Abstract Method Interfaces" (SAM Interfaces)
  • It is an interface that contains only 1 abstract method.
  • Further, can have any no. of default, static methods.
  • It can declare methods of Object class.
@FunctionalInterface    // Not Mandatory
interface Drawable{
    public void draw(String clr); // #1 Abstract method

    int hashCode();               // #2 Object class methods
    String toString();
}

public class B1_LambdaExpression {
    public static void main(String[] args) {
        int width = 10;    
    
        Drawable d = new Drawable(){
            public void draw(String clr){
                System.out.println("1. Width:"+width+ "\tClr:"+clr);
            }
        };
        d.draw("Red");
    }
}    

Output:
1. Width:8 Clr:Red
2. Width:8 Clr:Blue
Q2b. Can a Functional Interface extend other interface?
  • Yes, only if that interface does not have any abstract method.
{{{CODE HERE}}}

Q2c. Can a functional interface extend a non-functional interface ?
  • Yes.
{{{CODE HERE}}}




3. LAMBDA EXPRESSIONS :

Q3a. What is Lambda Expression ?
  • It is an alternate for "Anonymous inner class".
  • It provides clear way to represent 1-method interface using an expression.
  • It is very useful in Collection library to iterate,filter,extract data from collection.
  • It is treated as a function, so compiler does NOT create .class file.
Q3b. What are advantages of Lambda Expression ?
  1. To provide implementation of Functional Interface.
  2. Less coding.
Q3c. Show simple code with & without lambda expression.
@FunctionalInterface    // Not Mandatory
interface Drawable{
    public void draw(String clr);
}

public class B1_LambdaExpression {
    public static void main(String[] args) {
        int width = 10;    
    
        // Java7
        Drawable d = new Drawable(){
            public void draw(String clr){
                System.out.println("1. Width:"+width+ "\tClr:"+clr);
            }
        };
        d.draw("Red");

        // Java8
        Drawable d2 = clr -> {
            System.out.println("2. Width:"+width+ "\tClr:"+clr);
        };
        d2.draw("Blue");
    }
}    

Output:
1. Width:8 Clr:Red
2. Width:8 Clr:Blue
Q2d. Use ForEach loop with Lambda expression.
interface Drawable{
    public void draw(String clr);
}

public class B1_LambdaExpression {
    public static void main(String[] args) {
        ArrayList list = new ArrayList();  // For Array or Set
        list.add("aaa");
        list.add("ccc");
        list.add("bbb");
        list.forEach(n->System.out.println(n));

        Map map = new HashMap();           // For Map
        map.put("aaa","AAA");
        map.put("ccc","CCC");
        map.put("bbb","BBB");        
        map.forEach((k,v) -> System.out.println("Key : "+k +"\tVal : "+v ));
    }
}


Output:

aaa
ccc
bbb

Key : aaa    Val : AAA
Key : ccc    Val : CCC
Key : bbb    Val : BBB 
Q3e. Create Thread using Lambda Expression.
    public static void main(String[] args) {
        Runnable r1 = new Thread(){             // Java 7
            public void run(){
                System.out.println("t1 runs...");
            }
        };
        r1.run();
        
        Runnable r2 = ()->{                     // Java 8
            System.out.println("t2 running...");
        };
        r2.run();    
    }

Output:
t1 runs...
t2 running...


4. METHOD REFERENCES : 

Q4a. What is Method References ?

  • It is used to refer method of Functional Interface.
  • It is compact and easy form of lambda expr.
  • It is described using :: symbol.
Q4b. What are types of Method References?
  • Reference to 
    1. Static method
    2. Instance method of an object
    3. Instance method of arbitrary object of a type
    4. Constructors using new operator (TreeSet::new)
Q4c. Explain Reference to Static method.
  • Syntax :
              ContainerClass::staticMethodName



    5. DEFAULT/STATIC METHODS IN INTERFACE : 

    Q5a. What are Default-methods?
    • Java 8 provides a facility to create default methods inside the interface.
    • Methods should be tagged default.
    • Those are non-abstract methods.
    Q5b.  What are Static-methods?
    • You can define static methods inside the interface.
    • They are used to define utility-methods.

    Q5c. Give an example of Default & Static Method.
    {{{CODE}}} 

    Q5d. What is difference between Abstract-class & Java8 Interface?
    •  Abstract-class & Interface are almost similar (with default and static methods in Interface), except that you can create Constructor in Abstract-class but not in interface.
    {{{CODE}}}
     



    6. forEach() METHOD :

    Q6a. What is forEach() method?
    • It is used to iterate the elements.
    • It is default method defined in <Iterable> & <Stream> interface.
    • Signature in Iterable Interface : 
         default void forEach(Consumer<super T>action) 
    • {{{CODE}}}

    Q6b. What is Stream forEach() method?
    • It is used to iterate elements in the order specified by the stream.
    • Signature:
         void forEachOrdered(Consumer<? super T> action)  
    • {{{CODE}}}






































    To be continued...

    No comments:

    Post a Comment

    Springboot before Interview

    Hi Techies, Please refer : Springboot