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
| Q | Answer | Explanation |
| 1 | B | Runtime polymorphism. |
| 2 | B | String is immutable. |
| 3 | B | StringBuilder is mutable. |
| 4 | C | 5 + 7 = 12. |
| 5 | B | First enum constant. |
| 6 | B | ArithmeticException caught. |
| 7 | A | Stream transformation. |
| 8 | A | Narrower checked exception allowed. |
| 9 | B | Binary search halves data. |
| 10 | C | Set 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
| Q | Answer | Explanation |
| 1 | B | Index starts at 0. |
| 2 | B | Duplicate removed. |
| 3 | B | Second value replaces first. |
| 4 | A | && evaluated first. |
| 5 | B | Only 2 and 3 pass filter. |
| 6 | B | Optional contains value. |
| 7 | A | Valid lambda. |
| 8 | C | Modification during iteration. |
| 9 | A | Average constant time lookup. |
| 10 | B | Maintains 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
| Q | Answer | Explanation |
| 1 | A | Static methods are hidden. |
| 2 | B | Different objects. |
| 3 | A | Integer cache (-128 to 127). |
| 4 | B | Outside cache range. |
| 5 | B | String concatenation. |
| 6 | A | Queue is FIFO. |
| 7 | A | Descending order comparator. |
| 8 | B | IOException caught by Exception. |
| 9 | B | Merge sort complexity. |
| 10 | C | Custom sorting interface. |
Final Interview Recommendations
| Score | Rating |
| 27–30 | Outstanding |
| 24–26 | Strong Java Developer |
| 20–23 | Good Fundamentals |
| 15–19 | Average |
| Below 15 | Needs 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.