Java OCP Programming Challenge – Sets 9 to 11

30 Program-Based Questions for Java Developers

Most interview failures happen not because candidates don’t know concepts, but because they cannot read and understand code quickly.

These assessments focus entirely on:

✅ OOP and Inheritance
✅ Runtime Polymorphism
✅ Exception Handling
✅ String Manipulation
✅ Enums
✅ Collections
✅ Java 8 Streams
✅ Lambdas
✅ Operators
✅ Complexity Analysis
✅ Missing Code Questions
✅ Output Prediction

Each set contains:

  • 6 Output Questions
  • 1 Missing Code Question
  • 1 Exception/OOP Question
  • 1 Algorithm Question
  • 1 Collection/Java 8 Question

Time: 30 minutes per set.

SET 9 – OOP, Strings and Exceptions

Q1. What is the output?

class Parent {

    void show() {

        System.out.print(“Parent “);

    }

}

class Child extends Parent {

    void show() {

        System.out.print(“Child “);

    }

}

public class Test {

    public static void main(String[] args) {

        Parent p = new Child();

        p.show();

    }

}

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

Q2. What is the output?

String s = “Java”;

s.concat(“17”);

System.out.println(s);

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

Q3. What is the output?

StringBuilder sb =

    new StringBuilder(“Java”);

sb.append(“17”);

System.out.println(sb);

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

Q4. What is the output?

int x = 5;

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

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

Q5. What is the output?

enum Status {

    OPEN,CLOSED

}

System.out.println(

    Status.OPEN.ordinal());

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

Q6. What is the output?

try {

    System.out.println(10/0);

}

catch(Exception e) {

    System.out.println(“Error”);

}

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

Q7. Insert missing code.

List<String> list =

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

__________

Desired Output:

JAVA

SPRING

A.

list.stream()

    .map(String::toUpperCase)

    .forEach(System.out::println);

B.

list.upper();

C.

list.print();

D.

System.out.println(list);

Q8. What happens?

class Parent {

    void read() throws Exception {

    }

}

class Child extends Parent {

    void read() throws IOException {

    }

}

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

Q9. Complexity of Binary Search?

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

Q10. Which collection removes duplicates?

A. List
B. Queue
C. Set
D. ArrayList

Answers – SET 9

QAnswerExplanation
1BRuntime polymorphism.
2BString is immutable.
3BStringBuilder is mutable.
4C5 + 7 = 12.
5BFirst enum constant.
6BArithmeticException caught.
7AStream transformation.
8ANarrower checked exception allowed.
9BBinary search halves data.
10CSet removes duplicates.

SET 10 – Collections, Java 8 and Operators

Q1. What is the output?

List<Integer> list =

    Arrays.asList(10,20,30);

System.out.println(list.get(1));

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

Q2. What is the output?

Set<Integer> set =

    new HashSet<>();

set.add(10);

set.add(10);

System.out.println(set.size());

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

Q3. 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. null
D. Exception

Q4. What is the output?

System.out.println(

    true && false || true);

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

Q5. What is the output?

Stream.of(1,2,3)

      .filter(x -> x > 1)

      .forEach(System.out::print);

A. 123
B. 23
C. 12
D. Exception

Q6. What is the output?

Optional<String> op =

    Optional.of(“Java”);

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

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

Q7. Insert missing code.

Predicate<Integer> p =

    _________;

System.out.println(

    p.test(15));

A.

x -> x > 10

B.

x > 10

C.

return x > 10

D.

() -> true

Q8. What happens?

Iterator<Integer> itr =

    list.iterator();

while(itr.hasNext()) {

    list.add(100);

}

A. Works
B. Infinite loop
C. ConcurrentModificationException
D. Compilation Error

Q9. Complexity of HashMap lookup?

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

Q10. Which collection maintains insertion order?

A. HashSet
B. LinkedHashSet
C. TreeSet
D. Hashtable

Answers – SET 10

QAnswerExplanation
1BIndex starts at 0.
2BDuplicate removed.
3BSecond value replaces first.
4A&& evaluated first.
5BOnly 2 and 3 pass filter.
6BOptional contains value.
7AValid lambda.
8CModification during iteration.
9AAverage constant time lookup.
10BMaintains insertion order.

SET 11 – Advanced OCP and Code Reading

Q1. What is the output?

class A {

    static void show() {

        System.out.println(“A”);

    }

}

class B extends A {

    static void show() {

        System.out.println(“B”);

    }

}

public class Test {

    public static void main(String[] args) {

        A a = new B();

        a.show();

    }

}

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

Q2. What is the output?

String s1 = “Java”;

String s2 = new String(“Java”);

System.out.println(s1 == s2);

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

Q3. What is the output?

Integer a = 100;

Integer b = 100;

System.out.println(a == b);

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

Q4. What is the output?

Integer a = 200;

Integer b = 200;

System.out.println(a == b);

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

Q5. What is the output?

System.out.println(

    “A” + 10 + 20);

A. A30
B. A1020
C. 30A
D. Exception

Q6. What is the output?

Queue<Integer> q =

    new LinkedList<>();

q.offer(10);

q.offer(20);

System.out.println(q.poll());

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

Q7. Insert missing code.

Collections.sort(

    list,

    ___________);

Sort descending.

A.

Collections.reverseOrder()

B.

Comparator.asc()

C.

sortDesc()

D.

Comparator.reverse()

Q8. What happens?

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. Complexity of Merge Sort?

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

Q10. Which interface is used for custom sorting?

A. Runnable
B. Supplier
C. Comparator
D. Predicate

Answers – SET 11

QAnswerExplanation
1AStatic methods are hidden.
2BDifferent objects.
3AInteger cache (-128 to 127).
4BOutside cache range.
5BString concatenation.
6AQueue is FIFO.
7ADescending order comparator.
8BIOException caught by Exception.
9BMerge sort complexity.
10CCustom sorting interface.

Final Interview Recommendations

ScoreRating
27–30Outstanding
24–26Strong Java Developer
20–23Good Fundamentals
15–19Average
Below 15Needs Improvement

Evaluate candidates on:

  • Correct answer
  • Reasoning ability
  • Code reading speed
  • Knowledge of Java internals
  • OOP understanding
  • Exception handling rules
  • Collection behavior
  • Algorithm selection

A candidate who consistently scores above 80% across Sets 9–11 usually demonstrates strong Core Java and problem-solving skills expected from experienced Java developers.

Leave a Reply

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