learn2code

28 Java Technical Interview Programming Questions – Important!

Coding Courses and 28 Java Technical Interview Programming Questions - Important!. Advantages of Inheritance in Java and As a Java developer preparing for your next big interview, you need to be ready for Java Scenario Based Interview Questions that test your problem-solving skills and practical knowledge. In this blog post, we'll dive deep into 20 essential Java scenario-based interview questions, complete with detailed answers and explanations. Let's get started. Java Scenario Based Interview Questions: Singleton Class Question: Explain how you'd implement a thread-safe singleton class in Java. Answer: Here's an example of a thread-safe singleton implementation using the double-checked locking pattern: public class Singleton { private static volatile Singleton instance; private Singleton() {} public static Singleton getInstance() { if (instance == null) { synchronized (Singleton.class) { if (instance == null) { instance = new Singleton(); } } } return instance; } } Explanation: We use the volatile keyword to ensure that changes to the instance variable are immediately visible to other threads. The double-checked locking pattern minimizes the use of synchronization, improving performance. The private constructor prevents direct instantiation. The getInstance() method first checks if an instance exists before entering the synchronized block, reducing overhead. Handling Exceptions in File Reading Question: How would you handle exceptions in a method that reads from a file? Answer: Here's an example of how to handle exceptions when reading from a file: public static String readFile(String fileName) throws IOException { StringBuilder content = new StringBuilder(); try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) { String line; while ((line = reader.readLine()) != null) { content.append(line).append("\n"); } } catch (FileNotFoundException e) { System.err.println("File not found: " + fileName); throw e; } catch (IOException e) { System.err.println("Error reading file: " + fileName); throw e; } return content.toString(); } Explanation: We use a try-with-resources statement to ensure the BufferedReader is closed automatically. We catch specific exceptions (FileNotFoundException and IOException) to provide more detailed error messages. We re-throw the exceptions to allow the calling method to handle them if necessary. This approach follows the principle of "fail fast" by not suppressing exceptions. Thread-Safe Counter Question: You need to implement a thread-safe counter. How would you do it? Answer: Here's an implementation of a thread-safe counter using AtomicInteger: import java.util.concurrent.atomic.AtomicInteger; public class ThreadSafeCounter { private AtomicInteger count = new AtomicInteger(0); public void increment() { count.incrementAndGet(); } public void decrement() { count.decrementAndGet(); } public int getValue() { return count.get(); } } Explanation: We use AtomicInteger from the java.util.concurrent.atomic package, which provides atomic operations for integers. The incrementAndGet() and decrementAndGet() methods perform atomic increment and decrement operations, ensuring thread safety. This implementation is lock-free, offering better performance than using synchronized methods. Observer Pattern Question: Explain how you'd implement the Observer pattern in a weather monitoring application. Answer: Here's a basic implementation of the Observer pattern for a weather monitoring application: import java.util.ArrayList; import java.util.List; interface Observer { void update(float temperature, float humidity, float pressure); } class WeatherStation { private List observers = new ArrayList(); private float temperature; private float humidity; private float pressure; public void registerObserver(Observer o) { observers.add(o); } public void removeObserver(Observer o) { observers.remove(o); } public void notifyObservers() { for (Observer observer : observers) { observer.update(temperature, humidity, pressure); } } public void setMeasurements(float temperature, float humidity, float pressure) { this.temperature = temperature; this.humidity = humidity; this.pressure = pressure; notifyObservers(); } } class DisplayDevice implements Observer { @Override public void update(float temperature, float humidity, float pressure) { System.out.println("Temperature: " + temperature + "°C"); System.out.println("Humidity: " + humidity + "%"); System.out.println("Pressure: " + pressure + " hPa"); } } Explanation: We define an Observer interface with an update method that receives weather data. The WeatherStation class maintains a list of observers and notifies them when weather data changes. The DisplayDevice class implements the Observer interface and updates its display when notified. This pattern allows for loose coupling between the weather station and display devices. Custom Exception Question: How would you implement a custom exception class? Answer: Here's an example of implementing a custom exception class: public class InsufficientFundsException extends Exception { private double amount; public InsufficientFundsException(double amount) { super("Insufficient funds: Attempted to withdraw " + amount); this.amount = amount; } public double getAmount() { return amount; } } Explanation: We extend the Exception class to create our custom exception. We include a constructor that takes the withdrawal amount as a parameter and passes a descriptive message to the superclass constructor. We provide a getter method for the amount, allowing the caller to access this information if needed. This custom exception can be used in a banking application to handle insufficient funds scenarios. Producer-Consumer Pattern Question: Explain how you'd implement a producer-consumer pattern using BlockingQueue. Answer: Here's an implementation of the producer-consumer pattern using BlockingQueue: import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; class Producer implements Runnable { private BlockingQueue queue; public Producer(BlockingQueue queue) { this.queue = queue; } @Override public void run() { try { for (int i = 0; i < 10; i++) { queue.put(i); System.out.println("Produced: " + i); Thread.sleep(100); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } class Consumer implements Runnable { private BlockingQueue queue; public Consumer(BlockingQueue queue) { this.queue = queue; } @Override public void run() { try { while (true) { Integer item = queue.take(); System.out.println("Consumed: " + item); Thread.sleep(200); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } public class ProducerConsumerExample { public static void main(String[] args) { BlockingQueue queue = new LinkedBlockingQueue(5); Thread producerThread = new Thread(new Producer(queue)); Thread consumerThread = new Thread(new Consumer(queue)); producerThread.start(); consumerThread.start(); } } Explanation: We use a BlockingQueue (specifically, LinkedBlockingQueue) to safely pass items between the producer and consumer. The producer adds items to the queue using the put() method, which blocks if the queue is full. The consumer removes items from the queue using the take() method, which blocks if the queue is empty. This implementation ensures thread-safety and proper coordination between the producer and consumer. Optimizing Database Queries Question: Explain how you'd optimize database queries in a Java application. Answer: Here are several strategies to optimize database queries in a Java application: Use prepared statements: String sql = "SELECT * FROM users WHERE id = ?"; try (PreparedStatement pstmt = connection.prepareStatement(sql)) { pstmt.setInt(1, userId); ResultSet rs = pstmt.executeQuery(); // Process results } Implement connection pooling: ComboPooledDataSource cpds = new ComboPooledDataSource(); cpds.setDriverClass("com.mysql.jdbc.Driver"); cpds.setJdbcUrl("jdbc:mysql://localhost/mydb"); cpds.setUser("username"); cpds.setPassword("password"); cpds.setMinPoolSize(5); cpds.setAcquireIncrement(5); cpds.setMaxPoolSize(20); Use batch processing for multiple inserts: String sql = "INSERT INTO users (name, email) VALUES (?, ?)"; try (PreparedStatement pstmt = connection.prepareStatement(sql)) { for (User user : users) { pstmt.setString(1, user.getName()); pstmt.setString(2, user.getEmail()); pstmt.addBatch(); } pstmt.executeBatch(); } Implement proper indexing in the database: CREATE INDEX idx_user_email ON users (email); Explanation: Prepared statements improve performance by allowing the database to reuse the query plan. Connection pooling reduces the overhead of creating new database connections for each query. Batch processing reduces the number of round trips to the database for multiple inserts. Proper indexing in the database can significantly improve query performance, especially for large tables. Implementing a Custom Thread Pool Question: You're tasked with creating a thread pool for handling incoming network connections. How would you do this? Answer: Here's an implementation of a custom thread pool for handling network connections: import java.util.concurrent.*; public class NetworkConnectionPool { private final ExecutorService executorService; public NetworkConnectionPool(int nThreads) { this.executorService = new ThreadPoolExecutor( nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(), new ThreadFactory() { @Override public Thread newThread(Runnable r) { Thread t = new Thread(r); t.setDaemon(true); return t; } } ); } public void processConnection(Runnable task) { executorService.execute(task); } public void shutdown() { executorService.shutdown(); try { if (!executorService.awaitTermination(60, TimeUnit.SECONDS)) { executorService.shutdownNow(); } } catch (InterruptedException ex) { executorService.shutdownNow(); Thread.currentThread().interrupt(); } } } Usage: NetworkConnectionPool pool = new NetworkConnectionPool(10); // Process a connection pool.processConnection(() -> { // Handle network connection }); // Shutdown the pool when done pool.shutdown(); Explanation: We use ThreadPoolExecutor to create a fixed-size thread pool. The custom ThreadFactory creates daemon threads, which allows the JVM to exit if the main thread completes. The processConnection method submits tasks to the thread pool. The shutdown method ensures a graceful shutdown of the thread pool. Caching Mechanism Question: Describe how you'd implement a caching mechanism using the Proxy pattern. Answer: Here's an implementation of a caching mechanism using the Proxy pattern: interface Image { void display(); } class RealImage implements Image { private String fileName; public RealImage(String fileName) { this.fileName = fileName; loadFromDisk(); } private void loadFromDisk() { System.out.println("Loading " + fileName); } @Override public void display() { System.out.println("Displaying " + fileName); } } class ProxyImage implements Image { private RealImage realImage; private String fileName; public ProxyImage(String fileName) { this.fileName = fileName; } @Override public void display() { if (realImage == null) { realImage = new RealImage(fileName); } realImage.display(); } } public class ImageViewer { public static void main(String[] args) { Image image1 = new ProxyImage("image1.jpg"); Image image2 = new ProxyImage("image2.jpg"); image1.display(); // Loading and displaying image1 image1.display(); // Only displaying image1 (already loaded) image2.display(); // Loading and displaying image2 } } Explanation: The Image interface defines the common interface for RealImage and ProxyImage. RealImage represents the actual image object, which is expensive to create. ProxyImage acts as a surrogate for RealImage, implementing the same interface. ProxyImage creates the RealImage object only when it's first requested, implementing lazy loading. Subsequent calls to display() on the same ProxyImage object reuse the cached RealImage object. Custom Lock with Timeout Question: You need to implement a custom lock with timeout capabilities. How would you approach this? Answer: Here's an implementation of a custom lock with timeout capabilities: import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.AbstractQueuedSynchronizer; public class TimeoutLock { private static class Sync extends AbstractQueuedSynchronizer { @Override protected boolean tryAcquire(int arg) { return compareAndSetState(0, 1); } @Override protected boolean tryRelease(int arg) { setState(0); return true; } @Override protected boolean isHeldExclusively() { return getState() == 1; } } private final Sync sync = new Sync(); public boolean lock(long timeout, TimeUnit unit) throws InterruptedException { return sync.tryAcquireNanos(1, unit.toNanos(timeout)); } public void unlock() { sync.release(1); } } Usage: TimeoutLock lock = new TimeoutLock(); if (lock.lock(5, TimeUnit.SECONDS)) { try { // Critical section } finally { lock.unlock(); } } else { System.out.println("Failed to acquire lock within timeout"); } Explanation: We extend AbstractQueuedSynchronizer to implement the core locking mechanism. The tryAcquire method attempts to set the state from 0 to 1 atomically, indicating lock acquisition. The tryRelease method resets the state to 0, releasing the lock. The lock method uses tryAcquireNanos to attempt lock acquisition with a timeout. This implementation provides a reusable, efficient custom lock with timeout capabilities. Custom Annotation Question: Explain how you'd implement a custom annotation and where you might use it. Answer: Here's an example of implementing a custom annotation for method execution timing: import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface TimeExecution { } And here's how you might use it with an aspect: import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; @Aspect public class TimingAspect { @Around("@annotation(TimeExecution)") public Object timeMethod(ProceedingJoinPoint joinPoint) throws Throwable { long start = System.currentTimeMillis(); Object result = joinPoint.proceed(); long end = System.currentTimeMillis(); System.out.println(joinPoint.getSignature() + " took " + (end - start) + " ms"); return result; } } Usage: public class MyService { @TimeExecution public void doSomething() { // Method implementation } } Explanation: We define a custom annotation TimeExecution with runtime retention and method target. We implement an aspect that intercepts methods annotated with @TimeExecution. The aspect measures the execution time of the method and logs it. This annotation can be used to easily add performance logging to specific methods without modifying their code. Factory Method Pattern Question: How would you implement a factory method pattern in Java? Answer: Here's an implementation of the factory method pattern for creating different types of vehicles: interface Vehicle { void drive(); } class Car implements Vehicle { @Override public void drive() { System.out.println("Driving a car"); } } class Motorcycle implements Vehicle { @Override public void drive() { System.out.println("Riding a motorcycle"); } } abstract class VehicleFactory { abstract Vehicle createVehicle(); public void deliverVehicle() { Vehicle vehicle = createVehicle(); vehicle.drive(); } } class CarFactory extends VehicleFactory { @Override Vehicle createVehicle() { return new Car(); } } class MotorcycleFactory extends VehicleFactory { @Override Vehicle createVehicle() { return new Motorcycle(); } } Usage: VehicleFactory carFactory = new CarFactory(); carFactory.deliverVehicle(); // Output: Driving a car VehicleFactory motorcycleFactory = new MotorcycleFactory(); motorcycleFactory.deliverVehicle(); // Output: Riding a motorcycle Explanation: We define a Vehicle interface and concrete implementations (Car and Motorcycle). The abstract VehicleFactory class declares the factory method createVehicle(). Concrete factory classes (CarFactory and MotorcycleFactory) implement the factory method. The deliverVehicle() method in VehicleFactory uses the factory method to create and use a vehicle. This pattern allows for easy extension of vehicle types without modifying existing code. Custom Thread-Safe Data Structure Question: You need to implement a custom thread-safe data structure. What considerations would you keep in mind? Answer: Here's an example of a thread-safe bounded buffer implementation: import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class BoundedBuffer { private final E[] items; private int putIndex, takeIndex, count; private final Lock lock = new ReentrantLock(); private final Condition notFull = lock.newCondition(); private final Condition notEmpty = lock.newCondition(); @SuppressWarnings("unchecked") public BoundedBuffer(int capacity) { items = (E[]) new Object[capacity]; } public void put(E item) throws InterruptedException { lock.lock(); try { while (count == items.length) { notFull.await(); } items[putIndex] = item; if (++putIndex == items.length) putIndex = 0; ++count; notEmpty.signal(); } finally { lock.unlock(); } } public E take() throws InterruptedException { lock.lock(); try { while (count == 0) { notEmpty.await(); } E item = items[takeIndex]; items[takeIndex] = null; if (++takeIndex == items.length) takeIndex = 0; --count; notFull.signal(); return item; } finally { lock.unlock(); } } } Explanation: We use a ReentrantLock to ensure thread-safety. Conditions (notFull and notEmpty) are used to coordinate between producers and consumers. The put() method blocks when the buffer is full, and the take() method blocks when the buffer is empty. We use a circular buffer to efficiently use the array space. The lock.lock() and lock.unlock() calls are placed in a try-finally block to ensure the lock is always released. Key considerations for thread-safe data structures: Synchronization: Use locks, atomic operations, or other synchronization mechanisms. Consistency: Ensure that the data structure remains in a valid state even under concurrent access. Performance: Balance thread-safety with performance, using techniques like lock-free algorithms where appropriate. Deadlock prevention: Be careful about the order of acquiring multiple locks. Fairness: Consider whether operations should be fair (e.g., first-come-first-served) or not. Implementing Secure Password Hashing Question: Explain how you'd implement proper password hashing in a Java application. Answer: Here's an example of implementing secure password hashing using bcrypt: import org.mindrot.jbcrypt.BCrypt; public class PasswordHasher { private static final int LOG_ROUNDS = 12; public static String hashPassword(String plainTextPassword) { return BCrypt.hashpw(plainTextPassword, BCrypt.gensalt(LOG_ROUNDS)); } public static boolean checkPassword(String plainTextPassword, String hashedPassword) { return BCrypt.checkpw(plainTextPassword, hashedPassword); } } Usage: String password = "mySecurePassword123"; String hashedPassword = PasswordHasher.hashPassword(password); // Store hashedPassword in the database // Later, when verifying: boolean isValid = PasswordHasher.checkPassword("mySecurePassword123", hashedPassword); Explanation: We use the BCrypt algorithm, which is designed for password hashing and includes salt automatically. The LOG_ROUNDS parameter determines the computational cost of the hashing (higher is more secure but slower). hashPassword() generates a salt and hashes the password. checkPassword() verifies a plain text password against a hashed password. This approach protects against rainbow table attacks and makes brute-force attacks computationally expensive. Circuit Breaker Pattern Question: You're tasked with implementing a circuit breaker pattern for fault tolerance. How would you approach this? Answer: Here's a basic implementation of the Circuit Breaker pattern: import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; public class CircuitBreaker { private final long timeout; private final int failureThreshold; private final long resetTimeout; private AtomicInteger failureCount; private AtomicLong lastFailureTime; private State state; private enum State { CLOSED, OPEN, HALF_OPEN } public CircuitBreaker(long timeout, int failureThreshold, long resetTimeout) { this.timeout = timeout; this.failureThreshold = failureThreshold; this.resetTimeout = resetTimeout; this.failureCount = new AtomicInteger(0); this.lastFailureTime = new AtomicLong(0); this.state = State.CLOSED; } public boolean allowRequest() { if (state == State.OPEN) { if (System.currentTimeMillis() - lastFailureTime.get() > resetTimeout) { synchronized (this) { if (state == State.OPEN) { state = State.HALF_OPEN; } } } else { return false; } } return true; } public void recordSuccess() { failureCount.set(0); state = State.CLOSED; } public void recordFailure() { failureCount.incrementAndGet(); lastFailureTime.set(System.currentTimeMillis()); if (failureCount.get() >= failureThreshold) { state = State.OPEN; } } } Usage: CircuitBreaker breaker = new CircuitBreaker(1000, 5, 60000); public void performOperation() { if (breaker.allowRequest()) { try { // Perform the operation breaker.recordSuccess(); } catch (Exception e) { breaker.recordFailure(); // Handle the exception } } else { // Handle circuit open (e.g., return cached data, default response, or error) } } Explanation: The Circuit Breaker has three states: CLOSED (normal operation), OPEN (failing, rejecting requests), and HALF_OPEN (testing if the system has recovered). allowRequest() checks if a request should be allowed based on the current state. recordSuccess() and recordFailure() update the circuit breaker's state based on the operation's outcome. This pattern helps prevent cascading failures in distributed systems by failing fast and allowing time for recovery. Custom Collection Question: You need to implement a custom collection that maintains elements in insertion order and allows for efficient removal of the oldest element. How would you approach this? Answer: Here's an implementation of a custom collection called AgeOrderedSet that maintains elements in insertion order and allows for efficient removal of the oldest element: import java.util.*; public class AgeOrderedSet implements Set { private final Map map; private Node head; private Node tail; private static class Node { E element; Node prev; Node next; Node(E element) { this.element = element; } } public AgeOrderedSet() { this.map = new HashMap(); } @Override public boolean add(E element) { if (map.containsKey(element)) { return false; } Node newNode = new Node(element); map.put(element, newNode); if (tail == null) { head = tail = newNode; } else { newNode.prev = tail; tail.next = newNode; tail = newNode; } return true; } @Override public boolean remove(Object o) { Node node = map.remove(o); if (node == null) { return false; } removeNode(node); return true; } public E removeOldest() { if (head == null) { return null; } E oldest = head.element; removeNode(head); map.remove(oldest); return oldest; } private void removeNode(Node node) { if (node.prev != null) { node.prev.next = node.next; } else { head = node.next; } if (node.next != null) { node.next.prev = node.prev; } else { tail = node.prev; } } @Override public int size() { return map.size(); } @Override public boolean isEmpty() { return map.isEmpty(); } @Override public boolean contains(Object o) { return map.containsKey(o); } // Other Set methods would be implemented here... @Override public Iterator iterator() { return new Iterator() { private Node current = head; @Override public boolean hasNext() { return current != null; } @Override public E next() { if (!hasNext()) { throw new NoSuchElementException(); } E element = current.element; current = current.next; return element; } }; } } Explanation: We use a combination of a HashMap and a doubly-linked list to achieve the desired functionality. The HashMap allows for O(1) lookups and removals by element. The doubly-linked list maintains the insertion order and allows for efficient removal of the oldest element. add() inserts elements at the tail of the list. removeOldest() removes the head of the list in O(1) time. The iterator() method returns an iterator that traverses elements in insertion order. This implementation provides O(1) time complexity for add, remove, and removeOldest operations. Rate Limiter Question: You need to implement a rate limiter to restrict the number of requests a user can make within a given time window. How would you approach this? Answer: Here's an implementation of a simple rate limiter using the token bucket algorithm: import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; public class RateLimiter { private final ConcurrentHashMap userBuckets; private final int capacity; private final int refillRate; private final long refillPeriodMillis; public RateLimiter(int capacity, int refillRate, long refillPeriodMillis) { this.userBuckets = new ConcurrentHashMap(); this.capacity = capacity; this.refillRate = refillRate; this.refillPeriodMillis = refillPeriodMillis; } public boolean allowRequest(String userId) { TokenBucket bucket = userBuckets.computeIfAbsent(userId, k -> new TokenBucket()); return bucket.consumeToken(); } private class TokenBucket { private final AtomicInteger tokens; private long lastRefillTimestamp; TokenBucket() { this.tokens = new AtomicInteger(capacity); this.lastRefillTimestamp = System.currentTimeMillis(); } synchronized boolean consumeToken() { refill(); if (tokens.get() > 0) { tokens.decrementAndGet(); return true; } return false; } private void refill() { long now = System.currentTimeMillis(); long timeElapsed = now - lastRefillTimestamp; int tokensToAdd = (int) (timeElapsed / refillPeriodMillis * refillRate); if (tokensToAdd > 0) { tokens.updateAndGet(currentTokens -> Math.min(capacity, currentTokens + tokensToAdd)); lastRefillTimestamp = now; } } } } Usage: RateLimiter limiter = new RateLimiter(10, 1, 1000); // 10 tokens, refill 1 token per second String userId = "user123"; for (int i = 0; i < 15; i++) { if (limiter.allowRequest(userId)) { System.out.println("Request " + i + " allowed"); } else { System.out.println("Request " + i + " denied"); } Thread.sleep(200); // Simulate some delay between requests } Explanation: We implement a token bucket algorithm, where each user has a bucket of tokens. The bucket has a maximum capacity and refills at a specified rate. allowRequest() checks if a token is available and consumes it if so. The refill() method adds tokens based on the time elapsed since the last refill. This implementation is thread-safe and can handle concurrent requests for multiple users. The rate limiter allows for bursts of traffic up to the bucket capacity, while still maintaining a long-term rate limit. Simple Dependency Injection Container Question: Describe how you'd implement a simple dependency injection container. Answer: Here's a basic implementation of a simple dependency injection container: import java.lang.reflect.Constructor; import java.util.HashMap; import java.util.Map; public class DIContainer { private Map

This Java Technical Interview Programming Questions were Asked in Tech Mahindra, TCS, Wipro, Infosys, Accenture, Capgemini and Many Other Service and Product Based Companies.


Scroll Down To Download Technical Interview Programming Questions as PDF

1. Swap Two Numbers

Problem Statement

Write a program to swap two numbers without using a third variable.

Input

a = 10
b = 20

Output

a = 20
b = 10

Explanation

To swap two numbers without using a third variable, you can use arithmetic operations to interchange the values.

Program

public class learn2code {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;

        // Swapping without a third variable
        a = a + b; // a = 30
        b = a - b; // b = 10
        a = a - b; // a = 20

        System.out.println("a = " + a);
        System.out.println("b = " + b);
    }
}


2. Reverse a Number

Problem Statement

Write a program to reverse a number.

Input

1234

Output

4321

Explanation

To reverse a number, repeatedly extract the last digit and build the reversed number.

Program

public class learn2code {
    public static void main(String[] args) {
        int num = 1234;
        int reversed = 0;

        while (num != 0) {
            int digit = num % 10;
            reversed = reversed * 10 + digit;
            num /= 10;
        }

        System.out.println("Reversed Number: " + reversed);
    }
}


3. Reverse a String

Problem Statement

Write a program to reverse a string.

Input

"ABCD"

Output

"DCBA"

Explanation

To reverse a string, convert it to a character array and swap characters from the ends towards the center.

Program

public class learn2code {
    public static void main(String[] args) {
        String str = "ABCD";
        char[] chars = str.toCharArray();
        int left = 0, right = chars.length - 1;

        while (left < right) {
            char temp = chars[left];
            chars[left] = chars[right];
            chars[right] = temp;
            left++;
            right--;
        }

        String reversedStr = new String(chars);
        System.out.println("Reversed String: " + reversedStr);
    }
}


4. Fibonacci Series

Problem Statement

Write a program to print the Fibonacci series up to 10 terms.

Output

0, 1, 1, 2, 3, 5, 8, 13, 21, 34

Explanation

The Fibonacci series is a sequence where each number is the sum of the two preceding ones.

Program

public class learn2code {
    public static void main(String[] args) {
        int n = 10;
        int a = 0, b = 1;

        System.out.print(a + ", " + b);

        for (int i = 2; i < n; i++) {
            int next = a + b;
            System.out.print(", " + next);
            a = b;
            b = next;
        }
    }
}


5. Check if a Number is a Palindrome

Problem Statement

Write a program to check if a number is a palindrome.

Input

1221

Output

1221 is a Palindrome

Explanation

A palindrome is a number that reads the same backward as forward. Reverse the number and compare it to the original.

Program

public class learn2code {
    public static void main(String[] args) {
        int num = 1221;
        int original = num;
        int reversed = 0;

        while (num != 0) {
            int digit = num % 10;
            reversed = reversed * 10 + digit;
            num /= 10;
        }

        if (original == reversed) {
            System.out.println(original + " is a Palindrome");
        } else {
            System.out.println(original + " is not a Palindrome");
        }
    }
}


6. Check if a String is a Palindrome

Problem Statement

Write a program to check if a string is a palindrome.

Input

"DAD"

Output

DAD is a Palindrome

Explanation

A palindrome string reads the same backward as forward. Reverse the string and compare it to the original.

Program

public class learn2code {
    public static void main(String[] args) {
        String str = "DAD";
        String reversed = new StringBuilder(str).reverse().toString();

        if (str.equals(reversed)) {
            System.out.println(str + " is a Palindrome");
        } else {
            System.out.println(str + " is not a Palindrome");
        }
    }
}


7. Find Prime Numbers in a Given Range

Problem Statement

Write a program to find prime numbers between 2 and 10.

Input

2, 10

Output

Prime numbers between 2 and 10 are: 2, 3, 5, 7

Explanation

A prime number is a number greater than 1 with no divisors other than 1 and itself.

Program

public class learn2code {
    public static void main(String[] args) {
        int start = 2, end = 10;

        System.out.print("Prime numbers between " + start + " and " + end + " are: ");

        for (int i = start; i <= end; i++) {
            if (isPrime(i)) {
                System.out.print(i + " ");
            }
        }
    }

    static boolean isPrime(int n) {
        if (n <= 1) {
            return false;
        }
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }
}


8. Check if a Number is Prime

Problem Statement

Write a program to check if a given number is a prime number.

Input

2

Output

2 is a Prime Number

Explanation

Check if a number has no divisors other than 1 and itself.

Program

public class learn2code {
    public static void main(String[] args) {
        int num = 2;

        if (isPrime(num)) {
            System.out.println(num + " is a Prime Number");
        } else {
            System.out.println(num + " is not a Prime Number");
        }
    }

    static boolean isPrime(int n) {
        if (n <= 1) {
            return false;
        }
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }
}


9. Find Factorial of a Number

Problem Statement

Write a program to find the factorial of a number.

Input

5

Output

120

Explanation

The factorial of a number is the product of all positive integers less than or equal to that number.

Program

public class learn2code {
    public static void main(String[] args) {
        int num = 5;
        int factorial = 1;

        for (int i = 1; i <= num; i++) {
            factorial *= i;
        }

        System.out.println("Factorial of " + num + " is " + factorial);
    }
}


10. Check if a Number is an Armstrong Number

Problem Statement

Write a program to check if a number is an Armstrong number.

Input

153

Output

153 is an Armstrong Number

Explanation

An Armstrong number is a number that is equal to the sum of its own digits raised to the power of the number of digits.

Program

public class learn2code {
    public static void main(String[] args) {
        int num = 153;
        int original = num;
        int sum = 0;

        while (num != 0) {
            int digit = num % 10;
            sum += Math.pow(digit, 3);
            num /= 10;
        }

        if (sum == original) {
            System.out.println(original + " is an Armstrong Number");
        } else {
            System.out.println(original + " is not an Armstrong Number");
        }
    }
}


11. Count Number of Digits in a Number

Problem Statement

Write a program to count the number of digits in a number.

Input

1234

Output

Number of digits are 4

Explanation

To count the number of digits, repeatedly divide the number by 10 until it becomes 0, counting the iterations.

Program

public class learn2code {
    public static void main(String[] args) {
        int num = 1234;
        int count = 0;

        while (num != 0) {
            num /= 10;
            count++;
        }

        System.out.println("Number of digits are " + count);
    }
}


12. Count Even and Odd Digits in a Number

Problem Statement

Write a program to count the number of even and odd digits in a number.

Input

1234

Output

Even Numbers: 2
Odd Numbers: 2

Explanation

To count even and odd digits, extract each digit and check if it’s divisible by 2.

Program

public class learn2code {
    public static void main(String[] args) {
        int num = 1234;
        int evenCount = 0;
        int oddCount = 0;

        while (num != 0) {
            int digit = num % 10;
            if (digit % 2 == 0) {
                evenCount++;
            } else {
                oddCount++;
            }
            num /= 10;
        }

        System.out.println("Even Numbers: " + evenCount);
        System.out.println("Odd Numbers: " + oddCount);
    }
}


13. Check if a Number is Even or Odd

Problem Statement

Write a program to check if a number is even or odd.

Input

2

Output

Even Number

Explanation

A number is even if it is divisible by 2, otherwise it is odd.

Program

public class learn2code {
    public static void main(String[] args) {
        int num = 2;

        if (num % 2 == 0) {
            System.out.println(num + " is an Even Number");
        } else {
            System.out.println(num + " is an Odd Number");
        }
    }
}


14. Find Sum of Digits in a Number

Problem Statement

Write a program to find the sum of digits in a number.

Input

1234

Output

10

Explanation

To find the sum of digits, extract each digit and add them together.

Program

public class learn2code {
    public static void main(String[] args) {
        int num = 1234;
        int sum = 0;

        while (num != 0) {
            int digit = num % 10;
            sum += digit;
            num /= 10;
        }

        System.out.println("Sum of digits: " + sum);
    }
}


15. Find the Largest Number

Problem Statement

Write a program to find the largest number among three numbers.

Input

a = 10, b = 20, c = 30

Output

Largest Number is 30

Explanation

To find the largest number, compare each number with the others and determine the maximum.

Program

public class learn2code {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        int c = 30;

        int largest = (a > b) ? (a > c ? a : c) : (b > c ? b : c);

        System.out.println("Largest Number is " + largest);
    }
}


16. Generate Random Number and String

Problem Statement

Write a program to generate a random number and a random string.

Explanation

Use Math.random() for generating random numbers and a loop for random strings.

Program

import java.util.Random;

public class learn2code {
    public static void main(String[] args) {
        // Generate random number
        int randomNumber = (int) (Math.random() * 100);
        System.out.println("Random Number: " + randomNumber);

        // Generate random string
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        StringBuilder randomString = new StringBuilder();
        Random rand = new Random();
        int length = 10;

        for (int i = 0; i < length; i++) {
            randomString.append(characters.charAt(rand.nextInt(characters.length())));
        }

        System.out.println("Random String: " + randomString.toString());
    }
}


Find More Java Questions in Following Link:10 Important Java Question Paper with Model Answer

17. Find Sum of Elements in an Array

Problem Statement

Write a program to find the sum of elements in an array.

Input

a[] = {1, 2, 3, 4}

Output

10

Explanation

To find the sum of elements, iterate through the array and add each element.

Program

public class learn2code {
    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4};
        int sum = 0;

        for (int i = 0; i < a.length; i++) {
            sum += a[i];
        }

        System.out.println("Sum of elements: " + sum);
    }
}


18. Print Even and Odd Numbers in an Array

Problem Statement

Write a program to print even and odd numbers in an array.

Input

a[] = {1, 2, 3, 4}

Output

Even Numbers: 2, 4
Odd Numbers: 1, 3

Explanation

To print even and odd numbers, iterate through the array and check each number’s divisibility by 2.

Program

public class learn2code {
    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4};

        System.out.print("Even Numbers: ");
        for (int i = 0; i < a.length; i++) {
            if (a[i] % 2 == 0) {
                System.out.print(a[i] + " ");
            }
        }

        System.out.print("\\\\nOdd Numbers: ");
        for (int i = 0; i < a.length; i++) {
            if (a[i] % 2 != 0) {
                System.out.print(a[i] + " ");
            }
        }
    }
}


19. Check if Two Arrays are Equal

Problem Statement

Write a program to check if two arrays are equal.

Input

a[] = {1, 2, 3, 4}
b[] = {1, 2, 3, 4}

Output

a and b are equal

Explanation

Two arrays are equal if they have the same length and corresponding elements are equal.

Program

import java.util.Arrays;

public class learn2code {
    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4};
        int[] b = {1, 2, 3, 4};

        if (Arrays.equals(a, b)) {
            System.out.println("a and b are equal");
        } else {
            System.out.println("a and b are not equal");
        }
    }
}


20. Find Missing Number in an Array

Problem Statement

Write a program to find the missing number in an array.

Input

a[] = {1, 2, 3, 5}

Output

Missing number is 4

Explanation

To find the missing number, calculate the expected sum and subtract the actual sum from it.

Program

public class learn2code {
    public static void main(String[] args) {
        int[] a = {1, 2, 3, 5};
        int n = a.length + 1;
        int totalSum = n * (n + 1) / 2;
        int actualSum = 0;

        for (int i = 0; i < a.length; i++) {
            actualSum += a[i];
        }

        int missingNumber = totalSum - actualSum;
        System.out.println("Missing number is " + missingNumber);
    }
}


21. Find the Max and Min Element in an Array

Problem Statement

Write a program to find the maximum and minimum elements in an array.

Input

a[] = {3, 4, 5, 6}

Output

Max is 6
Min is 3

Explanation

To find the maximum and minimum elements, iterate through the array and keep track of the largest and smallest values.

Program

public class learn2code {
    public static void main(String[] args) {
        int[] a = {3, 4, 5, 6};
        int max = a[0];
        int min = a[0];

        for (int i = 1; i < a.length; i++) {
            if (a[i] > max) {
                max = a[i];
            }
            if (a[i] < min

) {
                min = a[i];
            }
        }

        System.out.println("Max is " + max);
        System.out.println("Min is " + min);
    }
}


22. Separate 0s and 1s in an Array

Problem Statement

Write a program to separate 0s and 1s in an array.

Input

a[] = {0, 1, 0, 1, 0, 1}

Output

0, 0, 0, 1, 1, 1

Explanation

To separate 0s and 1s, count the number of 0s and 1s and then place them accordingly.

Program

public class learn2code {
    public static void main(String[] args) {
        int[] a = {0, 1, 0, 1, 0, 1};
        int count = 0;

        for (int i = 0; i < a.length; i++) {
            if (a[i] == 0) {
                count++;
            }
        }

        for (int i = 0; i < count; i++) {
            a[i] = 0;
        }

        for (int i = count; i < a.length; i++) {
            a[i] = 1;
        }

        System.out.print("Array after separating 0s and 1s: ");
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
    }
}


23. Find Duplicates in an Array

Problem Statement

Write a program to find duplicates in an array.

Input

a[] = {1, 2, 3, 1, 2, 3, 4}

Output

1, 2, 3

Explanation

To find duplicates, use a nested loop to compare each element with the others.

Program

import java.util.HashSet;

public class learn2code {
    public static void main(String[] args) {
        int[] a = {1, 2, 3, 1, 2, 3, 4};
        HashSet<Integer> seen = new HashSet<>();
        HashSet<Integer> duplicates = new HashSet<>();

        for (int i = 0; i < a.length; i++) {
            if (!seen.add(a[i])) {
                duplicates.add(a[i]);
            }
        }

        System.out.print("Duplicates: ");
        for (int num : duplicates) {
            System.out.print(num + " ");
        }
    }
}


24. Find the Second Largest Element in an Array

Problem Statement

Write a program to find the second largest element in an array.

Input

a[] = {1, 2, 3, 4}

Output

3

Explanation

To find the second largest element, maintain two variables for the largest and second largest values.

Program

public class learn2code {
    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4};
        int largest = Integer.MIN_VALUE;
        int secondLargest = Integer.MIN_VALUE;

        for (int i = 0; i < a.length; i++) {
            if (a[i] > largest) {
                secondLargest = largest;
                largest = a[i];
            } else if (a[i] > secondLargest && a[i] != largest) {
                secondLargest = a[i];
            }
        }

        System.out.println("Second largest element is " + secondLargest);
    }
}


25. Move all Zeros to End of Array

Problem Statement

Write a program to move all zeros to the end of an array.

Input

a[] = {0, 1, 0, 3, 12}

Output

1, 3, 12, 0, 0

Explanation

To move all zeros to the end, iterate through the array and place non-zero elements at the front, then fill the rest with zeros.

Program

public class learn2code {
    public static void main(String[] args) {
        int[] a = {0, 1, 0, 3, 12};
        int count = 0;

        for (int i = 0; i < a.length; i++) {
            if (a[i] != 0) {
                a[count++] = a[i];
            }
        }

        while (count < a.length) {
            a[count++] = 0;
        }

        System.out.print("Array after moving zeros to end: ");
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
    }
}


26. Find Intersection of Two Arrays

Problem Statement

Write a program to find the intersection of two arrays.

Input

a[] = {1, 2, 3, 4}
b[] = {3, 4, 5, 6}

Output

3, 4

Explanation

The intersection of two arrays is the set of elements that are present in both arrays.

Program

import java.util.HashSet;

public class learn2code {
    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4};
        int[] b = {3, 4, 5, 6};
        HashSet<Integer> set = new HashSet<>();

        for (int i = 0; i < a.length; i++) {
            set.add(a[i]);
        }

        System.out.print("Intersection: ");
        for (int i = 0; i < b.length; i++) {
            if (set.contains(b[i])) {
                System.out.print(b[i] + " ");
            }
        }
    }
}


27. Find Union of Two Arrays

Problem Statement

Write a program to find the union of two arrays.

Input

a[] = {1, 2, 3, 4}
b[] = {3, 4, 5, 6}

Output

1, 2, 3, 4, 5, 6

Explanation

The union of two arrays is the set of all distinct elements present in both arrays.

Program

import java.util.HashSet;

public class learn2code {
    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4};
        int[] b = {3, 4, 5, 6};
        HashSet<Integer> set = new HashSet<>();

        for (int i = 0; i < a.length; i++) {
            set.add(a[i]);
        }

        for (int i = 0; i < b.length; i++) {
            set.add(b[i]);
        }

        System.out.print("Union: ");
        for (int num : set) {
            System.out.print(num + " ");
        }
    }
}


By practicing these problems, you’ll not only prepare for interviews but also build a solid foundation in programming logic.

Download Java Interview Question PDF

For More Java Technical Interview Programming Questions

Leave a Reply

Harish

Typically replies within a hours