Capgemini Java & Spring Boot Interview Questions for 3 years of experience
If you're preparing for a Java & Spring Boot interview at Capgemini. Below are the key Java and Spring Boot interview questions that were asked on March 7th, along with detailed answers.
1. Features of Java 8
Java 8 introduced several new features, including:
- Lambda Expressions – Allows functional programming.
- Functional Interfaces – Interfaces with only one abstract method.
- Stream API – Provides operations for processing collections.
- Optional Class – Avoids
NullPointerException
. - Default & Static Methods in Interfaces – Allows method implementation inside interfaces.
- New Date & Time API –
LocalDate
,LocalTime
,LocalDateTime
.
Example of Lambda Expression:
@FunctionalInterface interface Greeting { void sayHello(); } public class LambdaExample { public static void main(String[] args) { Greeting greet = () -> System.out.println("Hello, Java 8!"); greet.sayHello(); } }
2. Why is String Immutable in Java?
Reasons why String
is immutable:
✅ Security – Used in class loading, networking, etc.
✅ Thread Safety – Multiple threads can safely share a String
.
✅ Performance – String pooling optimizes memory usage.
✅ Caching in HashMap – Hashcode remains unchanged.
3. Can We Override a Private Method in Java?
No, private methods cannot be overridden because they are not inherited.
Example:
class Parent { private void display() { System.out.println("Parent class"); } } class Child extends Parent { private void display() { // Not overriding, just a new method in ChildSystem.out.println("Child class"); } }
4. How to Create an Immutable Class?
To make a class immutable:
- Declare the class as
final
. - Mark fields as
private final
. - Do not provide setters.
- Use a constructor to set values.
Example:
final class ImmutablePerson { private final String name; public ImmutablePerson(String name) { this.name = name; } public String getName() { return name; } }
5. Singleton Class in Java
A Singleton ensures only one instance exists.
Example: Thread-Safe Singleton Using synchronized
class Singleton { private static Singleton instance; private Singleton() {} public static synchronized Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } }
6. Can We Return from a void
Method?
Yes, we can use return
in a void
method but without a value.
Example:
void display() { System.out.println("Hello"); return; // Valid but optional }
7. Hierarchy of Map
in
Map (Interface) ├── HashMap │ ├── LinkedHashMap ├── TreeMap (Sorted) ├── Hashtable (Thread-Safe)
8. Overriding equals()
Method in Java
When overriding equals()
, ensure:
- Check instance type
- Compare field values
Example:
class Person { private String name; public Person(String name) { this.name = name; } @Overridepublic boolean equals(Object obj) { if (this == obj) return true; if (!(obj instanceof Person)) return false; Person p = (Person) obj; return name.equals(p.name); } }
9. Java Program to Rearrange an Array (First, Last, Alternate)
Example:
Input: [1, 2, 3, 4, 5]
Output: [5, 1, 4, 2, 3]
Code:
import java.util.Arrays; public class RearrangeArray { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; int[] result = new int[arr.length]; int left = 0, right = arr.length - 1, index = 0; while (left <= right) { if (index % 2 == 0) { result[index] = arr[right--]; } else { result[index] = arr[left++]; } index++; } System.out.println(Arrays.toString(result)); // Output: [5, 1, 4, 2, 3] } }
11. Spring Bean Scopes
Scope Description
singleton
Default, one instance per container.
prototype
Creates a new instance per request.
request
New bean per HTTP request (Web).
session
New bean per session (Web).
application
One instance per servlet context.
websocket
One instance per WebSocket session.
Example of Prototype Scope:
@Component @Scope("prototype") public class PrototypeBean { public PrototypeBean() { System.out.println("Prototype Bean Created"); } }
Conclusion
This blog covered key Java and Spring Boot interview questions asked in the Capgemini interview on March 7th. It included Java 8 features, String immutability, Singleton pattern, Bean scopes, Collection vs. Map, and practical coding questions.
Would you like additional Capgemini-specific questions or system design concepts for your next interview?