Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Core Java OOPs Concepts Tutorial

1: What is multithreading in Java?

Answer: Multithreading in Java is the concurrent execution of two or more threads. It allows multiple tasks to be executed in parallel, improving the overall performance of the application.

2: How can you create a thread in Java?

Answer: You can create a thread in Java by extending the Thread class or implementing the Runnable interface and then overriding the run() method.


class MyThread extends Thread {
    public void run() {
        // Thread logic goes here
    }
}

// Creating and starting the thread
MyThread myThread = new MyThread();
myThread.start();
        

3: What is the Runnable interface?

Answer: The Runnable interface in Java is used for creating a thread by implementing the run() method. It provides a way to separate the thread's behavior from the actual thread implementation.


class MyRunnable implements Runnable {
    public void run() {
        // Runnable logic goes here
    }
}

// Creating and starting the thread using Runnable
Thread myThread = new Thread(new MyRunnable());
myThread.start();
        

4: Explain the difference between Thread class and Runnable interface.

Answer: The Thread class in Java is a class hierarchy for creating and manipulating threads, while the Runnable interface provides a way to implement threads without extending the Thread class. Using Runnable is often preferred for better design and flexibility.

5: What is the difference between process and thread?

Answer: A process is an independent program that runs in its own memory space, while a thread is a lightweight unit of a process that shares the same memory space with other threads in the same process.

6: How do you synchronize threads in Java?

Answer: You can synchronize threads in Java using the synchronized keyword. It can be applied to methods or code blocks to ensure that only one thread can access the synchronized code at a time, preventing data inconsistencies.


class SynchronizedExample {
    private synchronized void synchronizedMethod() {
        // Synchronized method logic
    }
}
        

7: Explain the concept of deadlock in multithreading.

Answer: Deadlock occurs when two or more threads are blocked forever, each waiting for the other to release a resource. It can happen when multiple locks are acquired in different orders by different threads.

8: What is the purpose of the wait(), notify(), and notifyAll() methods?

Answer: These methods are used for inter-thread communication. wait() causes the current thread to wait until another thread notifies it. notify() wakes up one of the threads that are waiting. notifyAll() wakes up all the threads that are currently waiting.


// Example using wait() and notify()
synchronized (sharedObject) {
    while (condition) {
        sharedObject.wait(); // Releases the lock and waits
    }
    
    // Perform actions when condition is met
    
    sharedObject.notify(); // Notifies one waiting thread
}
        

9: What is the purpose of the join() method?

Answer: The join() method in Java is used to make sure that a thread finishes its execution before the calling thread continues. It waits for the specified thread to terminate.


Thread anotherThread = new Thread(() -> {
    // Thread logic
});

anotherThread.start();
anotherThread.join(); // Waits for anotherThread to finish
        

10: What is the purpose of the yield() method in Java?

Answer: The yield() method is used to temporarily pause the execution of the currently executing thread, allowing other threads of the same priority to run.


Thread.yield(); // Pause the current thread
        

11: Explain the concept of a race condition in multithreading.

Answer: A race condition occurs when two or more threads access shared data concurrently, and the final outcome depends on the timing or order of their execution. It can lead to unpredictable and undesirable behavior.

12: What is the purpose of the volatile keyword in Java?

Answer: The volatile keyword in Java is used to indicate that a variable's value may be changed by multiple threads simultaneously. It ensures that changes made by one thread are immediately visible to other threads.


class SharedResource {
    private volatile int counter;
}
        

13: How can you handle exceptions in a multithreaded environment?

Answer: Exceptions in a multithreaded environment can be handled by using try-catch blocks within the run() method or by setting an UncaughtExceptionHandler for the thread.


Thread myThread = new Thread(() -> {
    try {
        // Thread logic with potential exceptions
    } catch (Exception e) {
        // Exception handling
    }
});
        

14: What is the ThreadPoolExecutor in Java?

Answer: ThreadPoolExecutor is a class in Java that provides a flexible and efficient mechanism for executing tasks concurrently using a pool of worker threads. It helps manage and control the execution of tasks in a multithreaded environment.


ExecutorService executor = Executors.newFixedThreadPool(5); // Creates a fixed-size thread pool with 5 threads

executor.execute(() -> {
    // Task logic
});

executor.shutdown(); // Shutdown the executor when done
        

15: What is the significance of the sleep() method in Java?

Answer: The sleep() method is used to pause the execution of the current thread for a specified amount of time. It allows other threads to run during this time, providing a way to introduce delays in the program.


try {
    Thread.sleep(1000); // Pause for 1 second
} catch (InterruptedException e) {
    // Exception handling
}
        

16: What is a daemon thread in Java?

Answer: A daemon thread in Java is a background thread that runs without preventing the program from exiting. These threads are typically used for tasks that do not require the application to wait for their completion.

17: How can you prevent thread interference and consistency issues?

Answer: Thread interference and consistency issues can be prevented by using synchronization, locking mechanisms, and ensuring proper visibility of shared data through the use of volatile or synchronized keywords.

18: Explain the concept of the ThreadLocal class in Java.

Answer: The ThreadLocal class in Java provides a way to create variables that are local to each thread. Each thread that accesses the variable has its own, independently initialized copy of the variable.


ThreadLocal<String> threadLocalVariable = new ThreadLocal<>();
threadLocalVariable.set("Value specific to this thread");

String value = threadLocalVariable.get(); // Retrieve value for the current thread
        

19: What is the difference between the start() and run() methods in Java threads?

Answer: The start() method is used to begin the execution of a thread, while the run() method contains the code that constitutes the new thread. Calling run() directly does not create a new thread; it simply executes the code in the current thread.

20: Explain the concept of a reentrant lock in Java.

Answer: A reentrant lock in Java is a synchronization primitive that allows a thread to lock an object multiple times. It is called "reentrant" because a thread that holds the lock can reacquire it without blocking.


ReentrantLock lock = new ReentrantLock();

lock.lock(); // Acquire the lock
try {
    // Critical section
} finally {
    lock.unlock(); // Release the lock
}
        

21: What is the purpose of the AtomicInteger class?

Answer: AtomicInteger is a class in Java that provides atomic operations for integer variables. It ensures that operations such as increment and decrement are executed atomically, without interference from other threads.


AtomicInteger counter = new AtomicInteger();

counter.incrementAndGet(); // Atomically increment the counter
        

22: How can you create a thread-safe singleton class in Java?

Answer: A thread-safe singleton class in Java can be implemented by using double-checked locking or by utilizing the Enum type. Both approaches ensure that only one instance of the singleton is created and that it is thread-safe.

23: What is the ForkJoinPool in Java?

Answer: ForkJoinPool is a special-purpose ExecutorService introduced in Java for parallelizing divide-and-conquer algorithms. It is designed to efficiently run tasks that can be broken into smaller tasks and executed concurrently.


ForkJoinPool forkJoinPool = new ForkJoinPool();

forkJoinPool.invoke(new MyRecursiveTask()); // Invoke a recursive task in the pool
        

24: Explain the concept of the CyclicBarrier in Java.

Answer: CyclicBarrier is a synchronization aid in Java that allows a set of threads to wait for each other to reach a common barrier point. Once all threads have arrived, the barrier is released, and the threads can proceed.


CyclicBarrier barrier = new CyclicBarrier(3); // A barrier for 3 threads

// Threads 1, 2, and 3 reach the barrier point
barrier.await(); // Wait for other threads to reach the barrier
        
©2024 WithoutBook