SET 4 – Java 8, Streams, Optional and Advanced OOP

Q1. What is the output?

List<Integer> list =

        Arrays.asList(1,2,3);

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

A. 1 2 3
B. 123
C. [1,2,3]
D. Compilation Error

Q2. What is the output?

Stream.of(1,2,3,4)

      .filter(x -> x % 2 == 0)

      .forEach(System.out::print);

A. 1234
B. 24
C. 13
D. Exception

Q3. What is the output?

Optional<String> op =

        Optional.of(“Java”);

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

A. Optional[Java]
B. Java
C. null
D. Exception

Q4. What happens here?

Optional.empty().get();

A. null
B. NoSuchElementException
C. NullPointerException
D. Compilation Error

Q5. 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. Car
B. Vehicle
C. Vehicle Car
D. Compilation Error

Q6. What is the output?

class Parent {

    static void show() {

        System.out.println(“Parent”);

    }

}

class Child extends Parent {

    static void show() {

        System.out.println(“Child”);

    }

}

public class Test {

    public static void main(String[] args) {

        Parent p = new Child();

        p.show();

    }

}

A. Parent
B. Child
C. Exception
D. Compilation Error

Q7. Insert the missing code.

List<String> names =

        Arrays.asList(“Java”,”Spring”);

__________

Desired output:

JAVA

SPRING

A.

names.stream()

     .map(String::toUpperCase)

     .forEach(System.out::println);

B.

names.toUpperCase();

C.

names.print();

D.

System.out.println(names);

Q8. Exception and inheritance.

class Parent {

    void read() throws Exception {

    }

}

class Child extends Parent {

    void read() throws RuntimeException {

    }

}

A. Compilation Error
B. Valid override
C. Runtime Exception
D. Exception

Q9. Time complexity of Merge Sort.

A. O(n²)
B. O(log n)
C. O(n log n)
D. O(n)

Q10. Which interface represents a function that accepts one argument and returns a result?

A. Predicate
B. Consumer
C. Function
D. Supplier

Answers – SET 4

QAnswerExplanation
1BMethod reference prints without spaces.
2BOnly even numbers pass filter.
3BOptional contains value.
4BNo value present.
5BDefault method inherited.
6AStatic methods are hidden, not overridden.
7Amap() converts to uppercase.
8BUnchecked exception allowed.
9CMerge sort complexity.
10CFunction<T,R>.

SET 5 – OCP Challenge Round

Q1. What is the output?

StringBuilder sb =

        new StringBuilder(“Java”);

sb.append(“17”);

System.out.println(sb);

A. Java
B. 17
C. Java17
D. Exception

Q2. What is the output?

int x = 10;

System.out.println(++x + x++);

A. 20
B. 21
C. 22
D. 23

Q3. What is the output?

String s =

        “Java”;

System.out.println(

        s.substring(1,3));

A. av
B. ava
C. va
D. Jav

Q4. What is the output?

Map<String,Integer> map =

        new HashMap<>();

map.put(“A”,1);

map.put(“A”,2);

System.out.println(

        map.get(“A”));

A. 1
B. 2
C. Exception
D. null

Q5. What is the output?

enum Status {

    OPEN,CLOSED

}

public class Test {

    public static void main(String[] args) {

        System.out.println(

            Status.OPEN.ordinal());

    }

}

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

Q6. What is the output?

System.out.println(

        true && false || true);

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

Q7. Insert missing code.

Predicate<Integer> p =

        _________;

System.out.println(

        p.test(20));

Desired output:

true

A.

x -> x > 10

B.

x > 10

C.

return x > 10

D.

() -> true

Q8. Exception prediction.

try {

    throw new IOException();

}

catch(RuntimeException e) {

    System.out.println(“Runtime”);

}

catch(Exception e) {

    System.out.println(“Exception”);

}

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

Q9. Which algorithm uses Queue internally?

A. DFS
B. BFS
C. Binary Search
D. Merge Sort

Q10. Which collection is thread-safe?

A. ArrayList
B. HashMap
C. ConcurrentHashMap
D. HashSet

Answers – SET 5

QAnswerExplanation
1CStringBuilder is mutable.
2C11 + 11 = 22.
3AIndex 1 to 2.
4BSecond put replaces value.
5AFirst enum constant index.
6A&& evaluated before
7AValid lambda expression.
8BIOException caught by Exception.
9BBreadth-first search uses queue.
10CThread-safe map implementation.

Final Evaluation Matrix

ScoreAssessment
45–50Outstanding Java Developer
40–44Strong Core Java Knowledge
35–39Good Fundamentals
30–34Average
20–29Needs Improvement
Below 20Requires Core Java Training

Topics Covered

✅ OOP and Inheritance
✅ Runtime Polymorphism
✅ Method Overriding
✅ Exception Handling
✅ Exception Inheritance Rules
✅ Strings and StringBuilder
✅ Enums
✅ Operators and Evaluation Order
✅ Collections Framework
✅ Java 8 Lambdas
✅ Streams and Optional
✅ Algorithms and Complexity
✅ Missing Code Questions
✅ Output Prediction Questions

Interviewer Recommendations

  • Give 30 minutes per set.
  • Ask candidates to explain their answers.
  • Award bonus points for correct reasoning.
  • Ask follow-up questions on incorrect answers.
  • Focus on code-reading ability rather than memorization.

Candidates scoring above 40/50 generally demonstrate strong Core Java fundamentals and are typically successful in enterprise Java development roles.

Leave a Reply

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