Test Java Functional Programming (Lambda & Stream) skills

180+ questions on Inner classes, Lambda expressions, Method References, Functional Interfaces & Stream API

Enrollment Links:

Udemy

Original Price: ₹1280.00 / $19.99

To get MAXIMUM DISCOUNT  on this course, please send an email to udayan.khattry@outlook.com or send the request using “Contact” tab.

Description

Learning is incomplete without challenging questions to assess the knowledge gained. There are courses and books available on Functional Programming & Stream API, which cover these topics in detail, but simply watching the video lectures or finishing the book will not give enough confidence unless the knowledge is validated.

Practice tests in this course will not only help you to assess your current knowledge of these topics but will also help you to revise the topics quickly. Questions are designed to challenge your understanding of the topics. Detailed explanations for all the questions are also provided for your reference. 

Functional programming is not a new concept. Lambdas were implemented in other languages much before they were introduced in Java.

Before JDK 8, anonymous inner classes with a single method was the closest Java came to functional programming but with lots of boilerplate code. If anyone wants to really appreciate the implementation of lambda expressions in java, it is necessary to know the anonymous inner classes in depth and to understand anonymous inner class you need to have knowledge of Regular and method-local inner classes. 

You may face lots of questions in interviews or written tests where you are asked to convert anonymous inner class syntax to lambda expression and vice versa. Therefore I decided to  start with questions on inner classes and then go on with Lambda expression, method references, built-in functional interfaces and finally end this test series with questions on Stream API. 

1st practice test covers questions on:

  • Regular Inner class:
    1.  Regular inner class and its syntax 
    2.  Usage of this reference with Regular inner classes 
    3.  Shadowing of Outer class variable by Inner class variable 
    4.  Allowed access and non-access modifiers for Regular inner class 
    5.  Accessing Regular inner class’s name from within outer class and outside of outer class 
    6.  Instantiating Regular inner class 
    7.  Allowed and not allowed components inside a Regular inner class
  • Method-local inner class:
    1.  Method-local inner class and its syntax 
    2.  Modifiers used with method local inner classes 
    3.  Relationship between method local inner class and top-level class 
    4.  Where to create the instance of method local inner class? 
    5.  Usage of local variables with method-local inner class 
  • Anonymous inner class:
    1.  Syntax of anonymous inner class 
    2.  Inheritance and Polymorphism with anonymous inner class 
    3.  Defining non-overriding methods in anonymous inner classes 
    4.  Instance of anonymous inner class can be assigned to static variable, instance variable, local variable, method parameter and return value 
    5.  Anonymous inner class extending from concrete class or abstract class or implementing an interface 
    6.  Constructors and anonymous inner class
  • Static nested class:
    1.  Syntax of static nested class 
    2.  Accessing instance and static members of static nested class 
    3.  Allowed access modifiers with static nested class 
    4.  Nested interface defined within a class 
    5. Nested interface and nested class defined within an interface

2nd practice test will test, how well you know lambda expressions.

2nd Practice test covers questions on:

  • Lambda expression and its syntax
  • Arrow (->) operator
  • Simplified Lambda expression syntax
  • @FunctionalInterface annotation
  • Requirements for an interface to be a Functional interface
  • Convert anonymous inner class code to Lambda expression
  • this within anonymous inner class vs this within lambda expression
  • Usage of local variables with Lambda expression

Once you are comfortable with lambda expressions, then you can take 3rd practice test to assess your method references concepts. 

3rd Practice test covers questions on:

  • Method reference and its syntax
  • Double colon (::) operator
  • 4 types of method references:
  •  Method Reference to Constructor
  •  Method Reference to Static Method
  •  Method reference to an Instance Method of a Particular Object
  •  Method Reference to an Instance Method of an Arbitrary Object of a Particular Type 
  • Possibility of ambiguous call when a method reference syntax refers to both static and instance method of the class

Java 8 has provided various built-in functional interfaces, out of which 4 are most important and rest are dependent upon these 4 interfaces. 

4th practice test covers questions on:

  • Supplier interface
  • Consumer interface and its default method: andThen
  • Predicate interface and its default methods: and, or, negate
  • Function interface and its default methods: compose, andThen 
  • Comparator interface, its static method: comparing and default methods: thenComparing, reversed

Stream API made the life of Java developers a lot easier.

5th practice test covers questions on:

  • Generic Stream interface and its primitive counterparts
  • Creating sequential streams
  • Important methods of Stream interface
  • Generic Optional class and its primitive counterparts
  • Convert arrays and collections to streams
  • Sort a collection using Stream API

After Stream support in Collection API, processing collection elements became a lot easier. Parallel streams can run tasks in parallel without writing cumbersome logic.

6th practice test covers questions on:

  • Method stream() of Collection interface
  • Save results to a collection using the collect method and group/partition data using the Collectors class
  • forEach(Consumer) method of Iterator<T> interface
  • Convert arrays and collections to streams
  • stream() and parallelStream() methods of Collection interface
  • parallel() and sequential() method of Stream interface
  • Behavior of various methods such as forEach, reduce, forEachOrdered, findFirst with parallel streams

Below are some of the sample questions covered in this course:

1. For the given code:

interface Operator<T> {
    public abstract T operation(T t1, T t2);
}

public class Test {
     public static void main(String[] args) {
          System.out.println(new Operator<String>() {
               public String operation(String s1, String s2) {
                    return s1 + s2;
               }
          });
     }
}
  

Which of the following options successfully replace anonymous inner class code with lambda expression code?

A. System.out.println((String s1, String s2) -> s1 + s2);
B. System.out.println((s1, s2) -> s1 + s2);
C. System.out.println((s1, s2) -> { return s1 + s2; });
D. Lambda expression cannot be used as the argument of println method

Answer: D

2. What will be the result of compiling and executing Test class?

import java.util.Arrays;
import java.util.Comparator;

public class Test {
     public static void main(String[] args) {
          String [] arr = {"A5", "B4", "C3", "D2", "E1"};
          Arrays.sort(arr, Comparator.comparing(s -> s.substring(1)));
          for(String str : arr) {
               System.out.print(str + " ");
          }
     }
}

A. E1 D2 C3 B4 A5
B. A5 B4 C3 D2 E1
C. A1 B2 C3 D4 E5
D. E5 D4 C3 B2 A1

Answer: A

3. Consider below code:

import java.util.function.Function;

public class Test {
    public static void main(String[] args) {
        Function<Integer, Integer> f = x -> x + 10; 
        Function<Integer, Integer> g = y -> y * y; 
        
        Function<Integer, Integer> fog = g.compose(f); //Line 8
        System.out.println(fog.apply(10));
    }
}

On execution, Test class prints 400 on to the console. Which of the statements can replace Line 8 such that there is no change in the output?

A. Function<Integer, Integer> fog = f.compose(g);
B. Function<Integer, Integer> fog = f.andThen(g);
C. Function<Integer, Integer> fog = g.andThen(f);

Answer: B

4. Consider below code:

public class Test {
     public static void main(String[] args) {
          I01<Integer> obj = Integer::new;
          System.out.println(obj.create("603"));
     }
}

Which of the following functional interface definitions can be used here, so that the output of above code is: 603?

A.

interface I01 {
    Integer create(String str);
}

B.

interface I01<T> {
    T create(String str);
}

C.

interface I01<T extends Integer> {
    T create(String str);
}

D.

interface I01<T extends Number> {
    T create(String str);
}

Answer: B,C,D

NOTE: All the codes in this practice test are tested against JDK 8.

So, grab a cup of coffee or your favourite drink and test your Java Functional Programming skills.

Features of Practice Tests:

  • You can pause the test at any time and resume later.
  • You can retake the test as many times as you would like.
  • The progress bar at the top of the screen will show your progress as well as the time remaining in the test. If you run out of time, don’t worry; you will still be able to finish the test.
  • You can skip a question to come back to at the end of the exam.
  • You can also use “Mark for Review” to come back to questions you are unsure about before you submit your test.
  • If you want to finish the test and see your results immediately, press the stop button.

Target Audience

  • Anyone who wants to assess their Java Functional Programming skills including : Lambda, Method Reference, Built-in Functional interfaces & Stream API
  • Anyone who wants to assess their Java Inner classes skills