Java Coding & Top Interview Questions (Rapid Revision Guide)

Coding Questions (Must Practice)


1. Find Duplicate Elements in Array

Logic:

  • Use HashSet
  • If element already exists → duplicate

Code:

“`java id=”c1″
import java.util.*;

public class Duplicate {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 2, 4, 1};
Set set = new HashSet<>();

    for (int num : arr) {
        if (!set.add(num)) {
            System.out.println("Duplicate: " + num);
        }
    }
}

}

---

2. Find Largest Element in Array

 Logic:

* Compare each element

 Code:

java id=”c2″
public class Largest {
public static void main(String[] args) {
int[] arr = {10, 20, 5, 40};

    int max = arr[0];

    for (int num : arr) {
        if (num > max) {
            max = num;
        }
    }

    System.out.println("Largest: " + max);
}

}

---

 3. Count Frequency of Characters

Logic:

* Use HashMap

 Code:

java id=”c3″
import java.util.*;

public class Frequency {
public static void main(String[] args) {
String str = “java”;
Map map = new HashMap<>();

    for (char c : str.toCharArray()) {
        map.put(c, map.getOrDefault(c, 0) + 1);
    }

    System.out.println(map);
}

}
“`


Top Interview Questions (Rapid Revision)


What is HashMap?

Answer:

HashMap stores key-value pairs and allows one null key and multiple null values. It is not synchronized.


HashMap vs ConcurrentHashMap

Key Difference:

  • HashMap is not thread-safe
  • ConcurrentHashMap is thread-safe

What is Exception Handling?

Answer:

Exception handling is used to handle runtime errors using try-catch blocks.


Checked vs Unchecked Exception

Answer:

  • Checked → Compile-time (IOException)
  • Unchecked → Runtime (NullPointerException)

What is Multithreading?

Answer:

Multithreading allows concurrent execution of two or more threads to maximize CPU usage.


What is Synchronization?

Answer:

Synchronization is used to control access of multiple threads to shared resources.


What is REST API?

Answer:

REST API is used for communication between client and server using HTTP methods.


What is Dependency Injection?

Answer:

Providing objects to a class instead of creating inside it.


What is Microservices?

Answer:

Architecture where application is divided into small independent services.


What is JWT?

Answer:

Token-based authentication mechanism for secure communication.


Conclusion

This covers:

  • Important coding questions
  • Frequently asked interview questions
  • Quick revision topics

Leave a Comment