Top 10 Java interview questions
Tags: java, interview, questions, answers
Published: 2023-10-09 17:39:24

### 1. Explain the difference between == and equals() in Java.

\== checks for reference equality, while equals() checks for content equality.

```java
String s1 = new String(“hello”);
String s2 = new String(“hello”);
System.out.println(s1 == s2); // false
System.out.println(s1.equals(s2)); // true
```

### 2. How can you make a class immutable in Java?

Make its fields private and final, provide no setter methods, and ensure deep copies in constructors and getters if necessary.

```java
public final class ImmutableClass {
    private final int value;

    public ImmutableClass(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }
}
```

### 3. Describe the difference between ArrayList and LinkedList.

ArrayList is backed by an array, while LinkedList is a doubly-linked list.

```java
ArrayList<Integer> arrList = new ArrayList<>();
LinkedList<Integer> linkList = new LinkedList<>();
```

### 4. How can you prevent a method from being overridden?

Use the final keyword.

```java
public class MyClass {
    public final void myMethod() {
        // …
    }
}
```

### 5. How do you create a thread in Java?

Either by extending the Thread class or implementing the Runnable interface.

```java
class MyThread extends Thread {
    public void run() {
        // …
    }
}
class MyRunnable implements Runnable {
    public void run() {
        // …
    }
}
```

### 6. What is the difference between throw and throws in Java?

throw is used to explicitly throw an exception, while throws declares exceptions a method might throw.

```java
public void myMethod() throws MyException {
    if (condition) {
        throw new MyException("Error occurred");
    }
}
```

### 7. How can you execute a block of code regardless of whether an exception is thrown?

Use the finally block.

```java
try {
    // risky code
} catch (Exception e) {
    // handle exception
} finally {
    // code to run regardless
}
```

### 8. How do you use Java Streams to filter and transform a list?

Use the filter() and map() methods.

```java
List<String> list = Arrays.asList("a", "ab", "abc");
List<String> result = list.stream()
                            .filter(s -> s.length() > 1)
                            .map(s -> s.toUpperCase())
                            .collect(Collectors.toList());
```

### 9. How can you ensure thread safety when updating a counter?

Use synchronized or java.util.concurrent utilities.

```java
private int counter = 0;
public synchronized void increment() {
    counter++;
}
```

### 10. How can you use Java 8’s Optional to handle potential null values?

Use the Optional class to wrap potential null values and provide alternatives.

```java
Optional<String> opt = Optional.ofNullable(getNullableString());
String result = opt.orElse("default");
```

_In closing, the intricacies of Java backend engineering are both challenging and rewarding. These foundational questions and insights serve as stepping stones for both budding and seasoned developers. As the tech landscape evolves, so should our understanding and adaptability. Here’s to continuous learning and coding excellence._

🗨️💡 **Join the Conversation! Share Your Thoughts Below.**

🗣️ Your opinion matters! We're eager to hear your take on this topic. What are your thoughts, experiences, or questions related to what you've just read? Don't hold back—let's dive into a lively discussion!

**Enjoyed this Article?**

💖 React: Click the heart icon to show your appreciation and help others discover this content too!

🔔 Follow: Stay updated with the latest insights, tips, and trends by subscribing to our profile. Don't miss out on future articles!

🚀 Share: Spread the knowledge! Share this article with your network and help us reach a wider audience.

Your engagement is what keeps our community thriving. Thank you for being a part of it!