IBM Java Developer Interview Questions & Answers
This guide covers real-world technical interview questions asked during an IBM Full Stack Java Developer interview. The candidate was within the 3 to 7 years of experience bracket. The process included a 45-question MCQ round (Java, Spring Boot, AWS), followed by a 50-minute technical interview, a managerial round, and an HR round.
Below are the technical questions asked during the 50-minute technical round.
1. Core Java & Java 8 Features
Q: What are the new features introduced in Java 8?
A: Java 8 introduced several major features to enable functional programming and efficient processing:
- Lambda Expressions: For concise and flexible coding.
- Stream API: For efficient, declarative data processing on collections.
- New Date/Time API (
java.time): Thread-safe and better designed for date/time management. - Optional Class: To handle potential null values gracefully.
- Default and Static methods in Interfaces.
- Functional Interfaces.
Q: What is the difference between a Lambda Expression and an Anonymous Class?
A: * Lambda Expression: A highly concise way to implement a Functional Interface (an interface with exactly one abstract method). It focuses purely on passing behavior.
- Anonymous Class: Used to instantiate a class or interface on the fly without naming it. Unlike lambdas, anonymous classes can implement interfaces with multiple methods, maintain their own internal state (instance variables), and use the
thiskeyword to refer to themselves.
Q: What is the difference between map and flatMap in the Stream API?
A: * map: Transforms each item in a stream into exactly one other item (a 1-to-1 mapping). It produces a stream of the exact same size as the original.
flatMap: Transforms each item into a stream of items and then "flattens" those nested streams into a single, continuous, unified stream (a 1-to-Many mapping). Useful for flattening structures like aList<List<String>>into aList<String>.
Q: Provide an example where you would use the reduce operation in Streams.
A: The reduce operation is used when you want to combine or aggregate all items in a stream into a single, final result.
Example: Finding the total sum of a List<Integer>. You would stream the list and use reduce(0, (a, b) -> a + b) to accumulate all the numbers into a single total.
Q: What is the purpose of the Optional class and what problem does it solve?
A: The Optional class is a container object used to represent the presence or absence of a value. It was introduced to solve the infamous NullPointerException. By returning an Optional, you force the calling code to explicitly acknowledge and handle the fact that a value might be missing, without relying on dangerous if (obj != null) checks.
Q: Describe Method Overloading. Can you overload a method by changing only the return type?
A: Method overloading occurs when you have multiple methods in the same class that share the exact same name but have different parameter lists (different type, number, or order of parameters).
- Can you change just the return type? No. Java identifies methods strictly by their signature (name + parameters). Changing only the return type will result in a compile-time error because the compiler won't know which method to call.
2. Memory, Strings & Collections
Q: Explain the internal working of HashMap and how it handles collisions.
A: A HashMap stores key-value pairs in an internal array of "buckets." When you call put(key, value), it uses a hashing function on the key to calculate the exact bucket index.
- Collisions: A collision happens if two different keys generate the exact same hash code and map to the same bucket.
HashMaphandles this by storing the colliding entries in a Linked List at that specific bucket. When searching, it traverses this list and uses theequals()method to find the correct key.
Q: How did Java 8 improve the collision resolution mechanism in HashMap?
A: In Java 8, if the Linked List at a specific bucket grows too long (specifically, crossing a threshold of 8 elements), the HashMap automatically converts that Linked List into a Balanced Tree (Red-Black Tree). This drastically speeds up search times in heavily collided buckets from $O(n)$ to $O(\log n)$.
Q: Explain the difference between == and .equals() with examples.
A: * == Operator: Checks if two object references point to the exact same memory location in the heap.
.equals()Method: Checks if the logical content or value of the two objects is identical.- Example:
new String("IBM") == new String("IBM")evaluates tofalse(different memory addresses). However,new String("IBM").equals(new String("IBM"))evaluates totrue(same content).
Q: What is String Interning and how does it influence comparisons?
A: String interning is a memory optimization technique where Java stores only one unique copy of each distinct String literal in a special memory area called the String Pool. Because interned strings with the same value point to the exact same memory reference, you can use the much faster == operator to compare them directly, improving performance.
Q: What is the difference between String and StringBuffer regarding thread safety and memory?
A: * String: Immutable (cannot be changed). Modifying it creates a brand-new object in memory, leaving the old one for garbage collection. Because it cannot change state, it is inherently thread-safe.
StringBuffer: Mutable (can be modified directly without creating new objects). It is highly memory-efficient for frequent string manipulations. It is also thread-safe because all of its public methods are synchronized, allowing only one thread to access it at a time.
3. Spring Framework & Spring Boot
Q: What is Dependency Injection (DI) in Spring?
A: Dependency Injection is a core design pattern in Spring where the IoC (Inversion of Control) container automatically creates, manages, and connects (injects) object dependencies into a class. This means developers do not have to manually instantiate objects using the new keyword, making the application loosely coupled and significantly easier to test and manage.
Q: What is the difference between Constructor Injection and Setter Injection?
A: * Constructor Injection: Dependencies are passed into the object through its constructor at the exact moment of creation. This is best for mandatory dependencies and allows the fields to be marked as final (promoting immutability).
- Setter Injection: Dependencies are passed into the object via setter methods after the object has been constructed. This is best for optional dependencies that can be swapped or changed later.
Q: How do @Resource, @Autowired, and @Inject differ?
A: All three are used for dependency injection, but they resolve dependencies in different orders:
@Resource(Java EE standard): Searches first by Name, then by Type.@Autowired(Spring specific) and@Inject(CDI standard): Search first by Type, then by Qualifier, and finally by Name.
Q: What is the purpose of the @Qualifier annotation?
A: When you have multiple beans of the exact same type available in the Spring context, @Autowired will get confused and throw an exception. The @Qualifier("specificBeanName") annotation is used alongside @Autowired to tell Spring exactly which bean instance should be injected.
Q: How do you handle exceptions globally in a Spring Boot application?
A: By using the @ControllerAdvice (or @RestControllerAdvice) annotation on a centralized global class. Inside this class, you define methods annotated with @ExceptionHandler(SpecificException.class). These methods catch specific exceptions thrown from anywhere in the application and return a customized, consistent JSON error response to the client.
Q: How do you implement a logging mechanism in Spring Boot?
A: Spring Boot provides Logback as the default logging framework out of the box. You instantiate a logger in your class using LoggerFactory.getLogger(YourClass.class). You can then use methods like logger.info(), logger.debug(), or logger.error() to record events. Advanced configurations (like log levels or rolling file appenders) can be set in application.properties or a custom logback-spring.xml file.
4. RESTful APIs
Q: What is the difference between REST and SOAP? When would you prefer one over the other?
A: * REST (Representational State Transfer): A lightweight architectural style that uses standard HTTP methods and typically exchanges JSON data. It is fast, easy to implement, and highly scalable. Preferred for modern web, mobile, and microservice applications.
- SOAP (Simple Object Access Protocol): A highly structured, rigid protocol that relies entirely on XML. It has strict standards for security (WS-Security) and ACID transactions. Preferred for highly complex, secure, enterprise-level integrations (like legacy banking or payment gateways).
Q: What role do HTTP methods play in a RESTful API?
A: HTTP methods define the specific CRUD (Create, Read, Update, Delete) operation you want to perform on a resource:
- GET: Retrieves data.
- POST: Creates new resources.
- PUT: Completely updates an existing resource.
- DELETE: Removes a resource.
Q: What does "statelessness" mean in REST?
A: Statelessness means the server does not store any client context or session data in memory between HTTP requests. Every single request from the client to the server must contain all the necessary information (such as authentication tokens) for the server to understand and process it independently.
Q: How can we secure a REST API?
A: 1. Always use HTTPS/TLS to encrypt data in transit. 2. Implement robust Authentication and Authorization frameworks, such as OAuth 2.0 or JWT (JSON Web Tokens). 3. Validate and sanitize all incoming input to prevent injection attacks. 4. Implement Rate Limiting to prevent DDoS and brute-force attacks.
Q: What is HATEOAS in REST?
A: HATEOAS stands for Hypermedia as the Engine of Application State. It is a REST constraint where the API embeds hypermedia links inside its JSON responses. These links dynamically guide the client on what specific actions or operations they can perform next on that resource, making the API highly self-descriptive and discoverable without hardcoding URLs on the client side.
5. AWS Basics & SQL
Q: Explain EC2, S3, and IAM in AWS.
A: * EC2 (Elastic Compute Cloud): Provides scalable, virtual servers in the cloud where you can deploy and run your applications.
- S3 (Simple Storage Service): A highly scalable object storage service used for saving files, images, backups, and static web content.
- IAM (Identity and Access Management): A security service that manages users, groups, roles, and strict permissions to securely control who can access specific AWS resources.
Q: What are JOINs in SQL?
A: JOINs are SQL clauses used to logically combine rows from two or more tables based on a related, common column between them (typically primary and foreign keys). They allow you to retrieve comprehensive, relational data seamlessly across multiple tables in a single query.
Q: Coding Question: Stream API Sorting
A: The candidate was asked to write a quick Java 8 snippet to sort a list of Employee objects based on their age using the Stream API.
List<Employee> sortedEmployees = employees.stream()
.sorted(Comparator.comparingInt(Employee::getAge))
.collect(Collectors.toList());