Top Interview Questions For Java in TCS
Here are some commonly asked Java interview questions and answers for TCS (Tata Consultancy Services):
Core Java Questions:
1. What are the main features of Java?
Answer:
- Object-Oriented – Uses OOP principles like inheritance and polymorphism.
- Platform-Independent – Runs on any OS with a JVM.
- Automatic Memory Management – Uses Garbage Collection.
- Multi-threaded – Supports concurrent execution.
- Secure & Robust – Prevents memory leaks and provides exception handling.
2. What is the difference between JDK, JRE, and JVM?
Answer:
- JDK (Java Development Kit) – Includes JRE + tools for development (compiler, debugger).
- JRE (Java Runtime Environment) – Contains JVM + libraries for running Java applications.
- JVM (Java Virtual Machine) – Converts Java bytecode into machine code for execution.
3. What is the difference between == and .equals()?
Answer:
- == checks reference equality (whether two objects point to the same memory location).
- .equals() checks content equality (whether two objects have the same value).
Example:
String s1 = new String("TCS");
String s2 = new String("TCS");
System.out.println(s1 == s2); // false (different objects)
System.out.println(s1.equals(s2)); // true (same content)
4. Explain Java Memory Management.
Answer:
- Heap Memory – Stores objects and class instances.
- Stack Memory – Stores method calls and local variables.
- Garbage Collection (GC) – Automatically removes unused objects from the heap.
5. What are Wrapper classes in Java?
Answer:
Wrapper classes convert primitive data types into objects (Integer, Double, Character, etc.).
Example:
int x = 10;
Integer obj = Integer.valueOf(x); // Boxing
int y = obj.intValue(); // Unboxing
OOPs (Object-Oriented Programming) Questions:
6. What are the 4 pillars of OOPs?
Answer:
- Encapsulation – Hiding data using private variables and public methods.
- Abstraction – Hiding implementation details using abstract classes and interfaces.
- Inheritance – Acquiring properties of a parent class in a child class.
- Polymorphism – Method Overloading (compile-time) and Method Overriding (runtime).
7. What is the difference between Abstract Class and Interface?
Feature | Abstract Class | Interface |
---|---|---|
Methods | Can have abstract and concrete methods | Only abstract methods (before Java 8) |
Variables | Can have final/non-final variables | Only public, static, final variables |
Inheritance | Single inheritance | Multiple inheritance |
Usage | When classes have a common base | When different classes have common behavior |
Example:
Abstract Class:
abstract class Animal {
abstract void sound();
void eat() { System.out.println("Eating..."); }
}
Interface:
interface Animal {
void sound();
}
8. What is Method Overloading and Method Overriding?
Answer:
- Method Overloading – Same method name, different parameters (Compile-time Polymorphism).
- Method Overriding – Same method in parent and child class with the same signature (Runtime Polymorphism).
Example:
Overloading:
class MathOperations {
int add(int a, int b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }
}
Overriding:
class Parent {
void show() { System.out.println("Parent Class"); }
}
class Child extends Parent {
@Override
void show() { System.out.println("Child Class"); }
}
Exception Handling & Multi-threading Questions:
9. What is Exception Handling in Java?
Answer:
Exception handling prevents program crashes using:
try
– Code that may cause an exception.catch
– Handles exceptions.finally
– Executes always, even if an exception occurs.throw
– Used to throw an exception explicitly.throws
– Declares exceptions that a method may throw.
Example:
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero.");
} finally {
System.out.println("Finally block executed.");
}
10. What are Checked and Unchecked Exceptions?
Answer:
- Checked Exceptions – Checked at compile-time (IOException, SQLException).
- Unchecked Exceptions – Occur at runtime (NullPointerException, ArithmeticException).
11. Explain the life cycle of a thread in Java.
Answer:
- New – Thread created but not started.
- Runnable – Thread ready to run but waiting for CPU.
- Running – Thread executing.
- Blocked/Waiting – Thread waiting for resources.
- Terminated – Thread execution completed.
Example:
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running...");
}
}
public class Test {
public static void main(String[] args) {
MyThread t = new MyThread();
t.start();
}
}
Spring & Hibernate Questions:
12. What is Spring Framework?
Answer:
Spring is a lightweight Java framework used for enterprise applications.
- Core features: Dependency Injection, AOP, MVC, Security, etc.
13. What is Dependency Injection in Spring?
Answer:
Dependency Injection (DI) is a design pattern where dependencies are injected by the Spring container instead of being created manually.
Example:
class Car {
Engine engine;
Car(Engine engine) { this.engine = engine; }
}
14. What is Hibernate and why is it used?
Answer:
Hibernate is an ORM (Object Relational Mapping) tool that simplifies database operations.
- Eliminates JDBC boilerplate code.
- Uses HQL (Hibernate Query Language).
- Supports caching for performance improvement.
Example:
@Entity
class Employee {
@Id @GeneratedValue
private int id;
private String name;
}
Miscellaneous Java Questions:
15. What is the difference between HashMap and HashTable?
Feature | HashMap | HashTable |
---|---|---|
Synchronization | Not synchronized | Synchronized |
Performance | Faster | Slower |
Null Values | Allows one null key and multiple null values | Doesn’t allow null keys or values |
Example:
HashMap<Integer, String> map = new HashMap<>();
map.put(1, "TCS");
System.out.println(map.get(1));
16. What is the difference between Collections and Streams?
Feature | Collections | Streams |
---|---|---|
Storage | Stores data | Doesn’t store data |
Iteration | External iteration | Internal iteration |
Modification | Can be modified | Immutable |
Example:
List<String> list = Arrays.asList("A", "B", "C");
list.stream().forEach(System.out::println);
These are some of the most commonly asked Java questions in TCS interviews. Let me know if you need help with mock interviews or coding challenges! 🚀