Sri Raghavendra Educational Institutions Society

Sri Krishna Institute of Technology

(Approved by AICTE, Affiliated to VTU, Karnataka)

Rahul Kumar

Assistant Professor

Your Profile Picture

Company-Wise Interview Preparation

A curated list of frequently asked Java and technical questions for top tech companies. Click a category to see the questions.


These companies typically focus on strong fundamentals in Core Java, OOPs, and basic concepts across the Java ecosystem.

Q: What is Java? What are its main features?

Answer: Java is a high-level, object-oriented, platform-independent programming language. Main features include: Simple, Secure, Portable, Robust, Multithreaded, and Architecture-Neutral.

Q: Why is Java platform independent?

Answer: Java code is compiled into bytecode, which can run on any machine with a Java Virtual Machine (JVM), regardless of the underlying hardware or OS.

Q: Is Java a pure object-oriented language? Why/Why not?

Answer: No, because it supports primitive data types (like `int`, `float`, etc.), which are not objects.

Q: Explain OOPs concepts in Java.

Answer:
Encapsulation: Wrapping data and code together.
Inheritance: Acquiring properties from another class.
Polymorphism: Many forms (method overloading/overriding).
Abstraction: Hiding details, showing only essentials.

Q: Difference between `==` and `equals()`?

Answer: `==` checks for reference (memory address) equality, while `equals()` checks for value/content equality (if overridden).

Q: List all primitive data types in Java.

Answer: `byte`, `short`, `int`, `long`, `float`, `double`, `char`, and `boolean`.

Q: What is the default value of int, float, boolean, and object reference?

Answer: For instance variables, the defaults are: `int`: 0, `float`: 0.0f, `boolean`: false, object reference: `null`.

Q: What is an array? How do you declare and initialize one?

Answer: An array is a collection of similar data types with a fixed size.
Declaration: `int[] arr = new int[5];`
Initialization: `int[] arr2 = {1, 2, 3, 4, 5};`

Q: What are the different control statements in Java?

Answer: Selection (`if`, `switch`), Iteration (`for`, `while`, `do-while`), and Jump (`break`, `continue`, `return`).

Q: Write a program to print numbers from 1 to 10 using a for loop.
for (int i = 1; i <= 10; i++) {
    System.out.println(i);
}
Q: What is exception handling and why is it needed?

Answer: Exception handling is a mechanism to manage runtime errors, maintaining the normal flow of the program. It uses `try`, `catch`, `finally`, `throw`, and `throws` keywords.

Q: Difference between checked and unchecked exceptions?

Answer: Checked exceptions are checked at compile time (e.g., `IOException`) and must be handled. Unchecked exceptions are checked at runtime (e.g., `NullPointerException`) and handling is optional.

Q: Difference between ArrayList and LinkedList?

Answer: `ArrayList` is faster for random access (get/set) but slower for insertions/deletions. `LinkedList` is faster for insertions/deletions but slower for random access.

Q: What is a HashMap? How does it work?

Answer: `HashMap` stores key-value pairs. It uses the key's `hashCode()` to find a bucket and stores the entry. Collisions are handled by storing entries in a linked list or, in Java 8+, a balanced tree.

Q: What is multithreading? How is it achieved in Java?

Answer: Multithreading is the concurrent execution of two or more threads. It is achieved by either extending the `Thread` class or implementing the `Runnable` interface.

Q: Difference between a process and a thread?

Answer: A process is an independent program with its own memory space. A thread is a lightweight sub-process that shares memory with other threads in the same process.

Q: What is synchronization?

Answer: Synchronization is a mechanism that controls the access of multiple threads to any shared resource, preventing data inconsistency and thread-interference.

Q: What is serialization?

Answer: Serialization is the process of converting an object's state into a byte stream, which can then be saved to a file or sent over a network. The `Serializable` interface must be implemented.

Q: What is the use of the `transient` keyword?

Answer: The `transient` keyword is used to mark a member variable to not be serialized when it is persisted to streams of bytes.

Q: What is the difference between final, finally, and finalize?

Answer:
`final`: A keyword used to make variables constant, methods non-overridable, and classes non-inheritable.
`finally`: A block that always executes after a `try-catch` block, used for resource cleanup.
`finalize`: A method called by the garbage collector just before an object is destroyed (deprecated).

Q: What are lambda expressions?

Answer: Lambda expressions are anonymous functions that can be used to implement a method defined in a functional interface, leading to more concise code.

Q: What is the Stream API?

Answer: The Stream API is used to process collections of objects in a functional style. It allows for operations like `filter`, `map`, and `reduce` to be chained together.

Q: What is the Optional class?

Answer: `Optional` is a container object which may or may not contain a non-null value. It's used to avoid `NullPointerException` and promote better API design.

Q: What are the main components of J2EE?

Answer: Key components include Servlets, JavaServer Pages (JSP), Enterprise JavaBeans (EJB), Java Database Connectivity (JDBC), Java Message Service (JMS), etc.

Q: What is Hibernate?

Answer: Hibernate is an Object-Relational Mapping (ORM) framework that simplifies database interactions by mapping Java objects to database tables.

Q: What is a servlet?

Answer: A servlet is a Java class that extends the capabilities of a server. It is used to handle HTTP requests and generate dynamic responses in a web application.

Q: What is connection pooling?

Answer: Connection pooling is a technique of creating and managing a pool of database connections that can be reused, which significantly improves performance and resource management.

These companies focus heavily on Data Structures, Algorithms, system design, and deep conceptual understanding of how things work internally.

Q: Implement a thread-safe singleton class in Java.

Answer: The double-checked locking pattern is a common and efficient way to achieve this.

public class Singleton {
    private static volatile Singleton instance;

    private Singleton() {}

    public static Singleton getInstance() {
        if (instance == null) { // First check
            synchronized (Singleton.class) {
                if (instance == null) { // Second check
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}
Q: How do you design a thread-safe LRU Cache?

Answer: A common approach is to use a `LinkedHashMap` configured in access-order mode and override its `removeEldestEntry` method. To make it thread-safe, you can wrap it using `Collections.synchronizedMap()` or handle synchronization manually.

Q: Explain the producer-consumer problem in Java.

Answer: This is a classic concurrency problem where one or more "producer" threads generate data and one or more "consumer" threads consume it, using a shared buffer. It can be solved using `wait()` and `notify()`/`notifyAll()` for synchronization, or more easily and robustly using a `BlockingQueue` from the `java.util.concurrent` package.

Q: How does garbage collection work in Java?

Answer: The JVM's Garbage Collector (GC) automatically reclaims memory by destroying objects that are no longer reachable from any live threads. Common GC algorithms include Mark-and-Sweep. The heap is often divided into generations (Young and Old) to optimize collection, an approach known as Generational GC.

Q: How do you detect and prevent memory leaks in Java?

Answer: Memory leaks occur when objects are no longer needed but are still referenced, preventing the GC from reclaiming them. They can be detected using profiling tools like JProfiler or VisualVM. Prevention involves nullifying object references when they're no longer needed, closing resources in `finally` blocks, and using `WeakReference` for cache implementations.

Q: What is the use of the `volatile` keyword?

Answer: The `volatile` keyword ensures that changes to a variable made by one thread are immediately visible to other threads. It prevents issues related to CPU caching of variable values.

Q: What is the difference between `sleep()` and `wait()`?

Answer: `Thread.sleep()` is a static method that pauses the current thread for a specified time but **does not** release any locks it holds. `object.wait()` is an instance method that makes the current thread wait and **does** release the lock on the object it was called on, allowing other threads to acquire the lock.

Be prepared to write code on a whiteboard or in a shared editor for these types of problems.

  • Reverse a string without using built-in reverse functions.
  • Find the first non-repeating character in a string. (Hint: Use a HashMap or a character count array).
  • Detect if a cycle exists in a singly linked list. (Hint: Use the slow and fast pointer approach).
  • Implement a thread-safe singleton pattern. (See example above).
  • Implement the producer-consumer problem using `wait()` and `notify()`.
  • Write a program that can result in a deadlock and explain how to avoid it. (Hint: Involves two or more threads acquiring locks on two or more resources in a different order).