OOP and Java 8 Interview Questions
Part A: Object-Oriented Programming (OOP) Interview Questions​
Beginner Level OOP Questions (1-15)​
Fundamental OOP Concepts​
-
What is Object-Oriented Programming?
- Programming paradigm based on objects containing data and methods
-
What are the four pillars of OOP?
- Encapsulation, Inheritance, Polymorphism, Abstraction
-
What is a class and object?
- Class: blueprint/template, Object: instance of a class
-
What is encapsulation? Provide an example.
- Wrapping data and methods together, hiding internal implementation
class Person {
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
} -
What is inheritance? What are its types?
- Mechanism to acquire properties of another class
- Types: Single, Multilevel, Hierarchical, Multiple (through interfaces), Hybrid
-
What is polymorphism? Explain its types.
- One interface, multiple implementations
- Types: Compile-time (method overloading) and Runtime (method overriding)
-
What is abstraction?
- Hiding implementation details and showing only essential features
-
What is the difference between abstraction and encapsulation?
- Abstraction: hiding complexity, Encapsulation: hiding data
-
What is method overloading?
- Same method name with different parameters in same class
-
What is method overriding?
- Child class provides specific implementation of parent class method
-
What is the difference between overloading and overriding?
- Overloading: compile-time, same class; Overriding: runtime, different classes
-
What is a constructor? What are its types?
- Special method to initialize objects
- Types: Default, Parameterized, Copy constructor
-
What is constructor overloading?
- Multiple constructors with different parameters
-
What is the difference between constructor and method?
- Constructor initializes object, method performs operations
-
What is
this
keyword?- Reference to current object instance
Intermediate Level OOP Questions (16-25)​
-
What is
super
keyword?- Reference to parent class members
-
What is the difference between
this()
andsuper()
?- this(): calls current class constructor, super(): calls parent constructor
-
What is an abstract class?
- Class that cannot be instantiated, can have abstract and concrete methods
-
What is an interface?
- Contract defining method signatures, pure abstraction (before Java 8)
-
What is the difference between abstract class and interface?
- Abstract class: partial abstraction, Interface: complete abstraction
- Abstract class: can have constructors, Interface: cannot
-
Can we have multiple inheritance in Java? How?
- No through classes, Yes through interfaces
-
What is composition?
- "Has-a" relationship, object contains other objects
-
What is aggregation?
- Special form of association, "has-a" relationship with independent lifecycle
-
What is the difference between composition and inheritance?
- Composition: "has-a", Inheritance: "is-a"
-
What is association, aggregation, and composition?
- Association: relationship between classes
- Aggregation: weak "has-a"
- Composition: strong "has-a"
Advanced Level OOP Questions (26-30)​
-
What is dynamic method dispatch?
- Runtime method resolution based on object type
-
What is the diamond problem?
- Ambiguity in multiple inheritance, solved by interfaces in Java
-
What is SOLID principles?
- Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation, Dependency Inversion
-
What are design patterns? Name few important ones.
- Reusable solutions to common problems
- Singleton, Factory, Observer, Strategy, Decorator
-
What is tight coupling and loose coupling?
- Tight: high dependency, Loose: low dependency between classes
Part B: Java 8 Interview Questions​
Beginner Level Java 8 Questions (1-15)​
Lambda Expressions​
-
What are the new features introduced in Java 8?
- Lambda expressions, Stream API, Functional interfaces, Optional, Method references, Default methods
-
What is a lambda expression?
- Anonymous function that can be treated as a value
(parameters) -> expression
(parameters) -> { statements; } -
What is a functional interface?
- Interface with exactly one abstract method (SAM - Single Abstract Method)
-
What is @FunctionalInterface annotation?
- Ensures interface has only one abstract method
-
What are the built-in functional interfaces in Java 8?
- Predicate, Function, Consumer, Supplier, UnaryOperator, BinaryOperator
-
What is Predicate functional interface?
- Takes one argument, returns boolean
Predicate<String> isEmpty = s -> s.isEmpty();
-
What is Function functional interface?
- Takes one argument, returns a result
Function<String, Integer> length = s -> s.length();
-
What is Consumer functional interface?
- Takes one argument, returns nothing (void)
Consumer<String> print = s -> System.out.println(s);
-
What is Supplier functional interface?
- Takes no argument, returns a result
Supplier<String> supplier = () -> "Hello World";
-
What are method references?
- Shorthand notation for lambda expressions
- Types: Static, Instance, Constructor, Instance of arbitrary object
-
What are the types of method references?
- Static: Class::staticMethod
- Instance: object::instanceMethod
- Constructor: Class::new
- Instance of arbitrary object: Class::instanceMethod
-
What is the difference between lambda expression and method reference?
- Method reference is shorthand for lambda when calling existing method
-
Can we use lambda expressions with any interface?
- No, only with functional interfaces
-
What is the difference between lambda and anonymous inner class?
- Lambda: functional interfaces only, Anonymous class: any interface/abstract class
-
What are default methods in interfaces?
- Methods with implementation in interfaces (Java 8+)
Intermediate Level Java 8 Questions (16-30)​
Stream API​
-
What is Stream API?
- Functional-style operations on collections of objects
-
What are the characteristics of Stream?
- No storage, Functional, Lazy evaluation, Possibly unbounded, Consumable
-
What is the difference between Collection and Stream?
- Collection: data structure, Stream: abstraction for processing data
-
What are intermediate and terminal operations in Stream?
- Intermediate: return Stream (filter, map, sorted)
- Terminal: return non-Stream result (collect, forEach, reduce)
-
What is the difference between map() and flatMap()?
- map(): one-to-one mapping
- flatMap(): one-to-many mapping, flattens nested structures
-
What is filter() method in Stream?
- Filters elements based on predicate
list.stream().filter(x -> x > 5).collect(toList());
-
What is collect() method in Stream?
- Terminal operation to transform stream elements into different form
-
What are Collectors?
- Utility class with predefined collectors (toList, toSet, groupingBy, etc.)
-
What is reduce() method?
- Combines stream elements into single result
Optional<Integer> sum = numbers.stream().reduce(Integer::sum);
-
What is the difference between findFirst() and findAny()?
- findFirst(): returns first element, findAny(): returns any element
-
What is parallel stream?
- Stream that can be processed in parallel across multiple threads
list.parallelStream().filter(predicate);
-
When should you use parallel streams?
- Large datasets, CPU-intensive operations, independent operations
-
What is Optional class?
- Container object to handle null values gracefully
-
What are the methods of Optional class?
- of(), ofNullable(), empty(), isPresent(), ifPresent(), orElse(), orElseGet()
-
What is the difference between orElse() and orElseGet()?
- orElse(): always executes, orElseGet(): executes only if empty
Advanced Level Java 8 Questions (31-40)​
Advanced Features​
-
What are static methods in interfaces?
- Methods that belong to interface, not implementing class
-
What is the diamond problem with default methods?
- Multiple interfaces with same default method, resolved by implementing class
-
How do you resolve conflicts with multiple default methods?
- Override in implementing class or use Interface.super.method()
-
What is CompletableFuture?
- Enhanced Future for asynchronous programming
-
What are the differences between Future and CompletableFuture?
- CompletableFuture: composable, chainable, better exception handling
-
What is Date and Time API in Java 8?
- New immutable date-time classes: LocalDate, LocalTime, LocalDateTime, ZonedDateTime
-
What are the advantages of new Date-Time API?
- Immutable, Thread-safe, Clear API, Better parsing/formatting
-
What is forEach() method?
- Internal iteration method for collections and streams
-
What is the difference between external and internal iteration?
- External: for loop (imperative), Internal: forEach (functional)
-
What are StringJoiner and String.join()?
- Utilities for joining strings with delimiters
Common Coding Examples​
OOP Example:​
// Encapsulation + Inheritance + Polymorphism
abstract class Animal {
protected String name;
public Animal(String name) { this.name = name; }
public abstract void makeSound();
public void sleep() { System.out.println(name + " is sleeping"); }
}
class Dog extends Animal {
public Dog(String name) { super(name); }
@Override
public void makeSound() { System.out.println(name + " barks"); }
}
Java 8 Example:​
// Lambda + Stream + Optional
List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David");
Optional<String> result = names.stream()
.filter(name -> name.startsWith("C"))
.map(String::toUpperCase)
.findFirst();
result.ifPresent(System.out::println); // CHARLIE
Interview Preparation Tips:​
For OOP Questions:​
- Practice implementing all four pillars with code examples
- Understand real-world scenarios where each concept applies
- Be ready to draw UML diagrams
- Know design patterns and SOLID principles
For Java 8 Questions:​
- Practice writing lambda expressions and method references
- Master Stream API operations with examples
- Understand functional programming concepts
- Practice converting imperative code to functional style
Common Follow-up Areas:​
- Performance implications
- Best practices and when to use what
- Real-world application scenarios
- Code optimization techniques