SET 15 – Default Methods, Static Methods and Functional Interfaces

Java 8 introduced one of the most important changes to interfaces:

  • Default methods
  • Static methods inside interfaces
  • Multiple inheritance conflict resolution
  • Functional interfaces
  • Method references
  • Date-Time API
  • Lambda exception handling

Q1. What is the output?

interface Vehicle {

    default void start() {

        System.out.print(“Vehicle “);

    }

}

class Car implements Vehicle {

}

public class Test {

    public static void main(String[] args) {

        new Car().start();

    }

}

A. Vehicle
B. Car
C. Vehicle Car
D. Compilation Error

Q2. What is the output?

interface A {

    default void show() {

        System.out.println(“A”);

    }

}

class Test implements A {

    public static void main(String[] args) {

        new Test().show();

    }

}

A. A
B. Test
C. Exception
D. Compilation Error

Q3. What is the output?

interface Demo {

    static void display() {

        System.out.println(“Java”);

    }

}

public class Test {

    public static void main(String[] args) {

        Demo.display();

    }

}

A. Java
B. Demo
C. Exception
D. Compilation Error

Q4. What happens?

interface A {

    default void show() {

        System.out.println(“A”);

    }

}

interface B {

    default void show() {

        System.out.println(“B”);

    }

}

class Test implements A, B {

}

A. Prints A
B. Prints B
C. Compilation Error
D. Runtime Exception

Q5. What is the output?

@FunctionalInterface

interface Demo {

    void show();

}

Demo d =

    () -> System.out.println(“Java”);

d.show();

A. Java
B. Demo
C. Exception
D. Error

Q6. What is the output?

List<String> list =

        Arrays.asList(“A”,”B”);

list.forEach(System.out::print);

A. AB
B. A B
C. [A, B]
D. Exception

Q7. Insert missing code.

interface Vehicle {

    __________

}

Desired behavior:

Vehicle

A.

default void start() {

    System.out.println(“Vehicle”);

}

B.

void start();

C.

abstract void start();

D.

static start();

Q8. Exception Question

try {

    Runnable r =

        () -> {

            throw new RuntimeException();

        };

    r.run();

}

catch(Exception e) {

    System.out.println(“Exception”);

}

A. RuntimeException
B. Exception
C. Compilation Error
D. Nothing

Q9. Date-Time API

LocalDate d =

        LocalDate.of(2025,6,20);

System.out.println(

        d.plusMonths(2));

A. 2025-08-20
B. 2025-07-20
C. Exception
D. 2025-06-22

Q10. Which annotation indicates a functional interface?

A. @Override
B. @FunctionalInterface
C. @Lambda
D. @Interface

Answers – SET 15

QAnswerExplanation
1AThe implementing class inherits the default method from the interface. Since Car does not override start(), Vehicle.start() executes.
2ADefault methods become part of the implementing class if not overridden.
3AStatic interface methods belong to the interface and must be called using InterfaceName.method().
4CJava cannot decide which default method to inherit. The class must override show().
5AA lambda expression provides the implementation for the single abstract method.
6AMethod reference prints both elements without spaces.
7ADefault methods allow interfaces to contain behavior.
8BRuntimeException propagates from the lambda and is caught by the catch block.
9ALocalDate is immutable. plusMonths() returns a new object.
10BThis annotation tells the compiler that the interface must contain exactly one abstract method.

SET 16 – Parallel Streams and Collectors

Q1. What is the output?

List<Integer> list =

        Arrays.asList(1,2,3);

System.out.println(

    list.stream()

        .reduce(0, Integer::sum));

A. 3
B. 6
C. 9
D. Exception

Q2. What is the output?

List<String> list =

        Arrays.asList(“A”,”B”,”C”);

String s =

    list.stream()

        .collect(Collectors.joining());

System.out.println(s);

A. ABC
B. A B C
C. [A,B,C]
D. Exception

Q3. What is the output?

List<Integer> list =

        Arrays.asList(10,20,30);

System.out.println(

    list.parallelStream()

        .count());

A. 0
B. 1
C. 3
D. Exception

Q4. What is the output?

List<Integer> list =

        Arrays.asList(1,2,3,4);

System.out.println(

    list.stream()

        .max(Integer::compareTo)

        .get());

A. 1
B. 2
C. 3
D. 4

Q5. What is the output?

List<Integer> list =

        Arrays.asList(10,20,30);

System.out.println(

    list.stream()

        .min(Integer::compareTo)

        .get());

A. 10
B. 20
C. 30
D. Exception

Q6. What is the output?

List<String> list =

        Arrays.asList(“java”,”spring”);

list.stream()

    .map(String::toUpperCase)

    .forEach(System.out::print);

A. javaspring
B. JAVASPRING
C. JAVA SPRING
D. Exception

Q7. Insert missing code.

List<Integer> list =

        Arrays.asList(1,2,3);

___________

Desired output: 6

A.

System.out.println(

    list.stream()

        .reduce(0,Integer::sum));

B.

list.sum();

C.

sum(list);

D.

list.total();

Q8. Exception Question

Stream<Integer> s =

        Stream.of(1,2,3);

s.count();

s.forEach(System.out::println);

A. 3
B. Exception
C. 0
D. Compilation Error

Q9. Date-Time API

LocalDateTime dt =

    LocalDateTime.of(

        2025,1,1,10,30);

System.out.println(

    dt.plusHours(5));

A. 2025-01-01T15:30
B. 2025-01-01T10:30
C. Exception
D. 2025-01-01T05:30

Q10. Which collector converts a stream into a List?

A. Collectors.sum()
B. Collectors.toList()
C. Collectors.list()
D. Collectors.array()

Answers – SET 16

QAnswerExplanation
1Breduce() starts with identity value 0 and adds all elements: 0+1+2+3 = 6.
2ACollectors.joining() concatenates all strings without separators.
3CparallelStream() changes execution strategy but not the result.
4Dmax() returns the largest value wrapped inside Optional.
5Amin() returns the smallest element.
6Bmap() transforms each string before printing.
7Areduce() is commonly used to aggregate values in streams.
8BStreams can only have one terminal operation. After count(), the stream is closed.
9ALocalDateTime is immutable. plusHours() returns a new object.
10BCollectors.toList() is the standard collector for producing lists.

SET 17 – Advanced Streams, Collectors and Date-Time API

Q1. What is the output?

List<Integer> list =

        Arrays.asList(1,2,3,4,5);

long count =

    list.stream()

        .filter(x -> x > 2)

        .count();

System.out.println(count);

A. 2
B. 3
C. 4
D. 5

Q2. What is the output?

List<String> list =

        Arrays.asList(“A”,”B”,”C”);

System.out.println(

    list.stream()

        .collect(Collectors.joining(“,”)));

A. ABC
B. A,B,C
C. [A,B,C]
D. Exception

Q3. What is the output?

List<Integer> list =

        Arrays.asList(10,20,30);

System.out.println(

    list.stream()

        .findFirst()

        .get());

A. 10
B. 20
C. 30
D. Exception

Q4. What is the output?

List<Integer> list =

        Arrays.asList(10,20,30);

System.out.println(

    list.stream()

        .anyMatch(x -> x > 25));

A. true
B. false
C. Exception
D. null

Q5. What is the output?

List<Integer> list =

        Arrays.asList(1,2,3);

System.out.println(

    list.stream()

        .allMatch(x -> x > 0));

A. true
B. false
C. Exception
D. Error

Q6. What is the output?

List<String> list =

        Arrays.asList(“java”,”spring”);

System.out.println(

    list.stream()

        .map(String::length)

        .reduce(0,Integer::sum));

A. 10
B. 11
C. 12
D. Exception

Q7. Insert missing code.

List<String> list =

        Arrays.asList(“java”,”spring”);

____________

Desired output:

[4, 6]

A.

System.out.println(

    list.stream()

        .map(String::length)

        .collect(Collectors.toList()));

B.

list.length();

C.

collect();

D.

map();

Q8. Exception Question

Optional<String> op =

        Optional.empty();

System.out.println(op.get());

A. null
B. NoSuchElementException
C. NullPointerException
D. IOException

Q9. Date-Time API

Period p =

        Period.ofDays(10);

LocalDate d =

        LocalDate.of(2025,1,1);

System.out.println(

        d.plus(p));

A. 2025-01-11
B. 2025-01-10
C. Exception
D. 2025-01-01

Q10. Which collector groups data?

A. Collectors.toList()
B. Collectors.joining()
C. Collectors.groupingBy()
D. Collectors.counting()

Answers – SET 17

QAnswerExplanation
1BThree elements (3,4,5) satisfy the condition x > 2.
2Bjoining(”,”) inserts commas between values.
3AfindFirst() returns an Optional containing the first element.
4A30 satisfies the condition x > 25, so anyMatch returns true.
5AAll values are greater than zero.
6ALengths are 4 and 6. Their sum is 10.
7Amap() transforms strings to lengths and collect() gathers results into a list.
8BCalling get() on an empty Optional throws NoSuchElementException.
9APeriod adds 10 days to the LocalDate.
10CgroupingBy() is commonly used to group stream elements by a key.

These three sets complete most important Java 8 interview topics:

✅ Lambdas
✅ Functional Interfaces
✅ Predicate, Function, Consumer, Supplier
✅ Streams
✅ Intermediate Operations
✅ Terminal Operations
✅ Optional
✅ Method References
✅ Default Methods
✅ Static Interface Methods
✅ Parallel Streams
✅ Collectors
✅ Reduce
✅ Date-Time API
✅ Exception Handling
✅ Missing Code Questions
✅ Output Prediction Questions

Leave a Reply

Your email address will not be published. Required fields are marked *