DEV Community

Full Stack from Full-Stack
Full Stack from Full-Stack

Posted on

Top 10 Java interview questions

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

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

String s1 = new String(hello);
String s2 = new String(hello);
System.out.println(s1 == s2); // false
System.out.println(s1.equals(s2)); // true
Enter fullscreen mode Exit fullscreen mode

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.

public final class ImmutableClass {
    private final int value;

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

    public int getValue() {
        return value;
    }
}
Enter fullscreen mode Exit fullscreen mode

3. Describe the difference between ArrayList and LinkedList.

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

ArrayList<Integer> arrList = new ArrayList<>();
LinkedList<Integer> linkList = new LinkedList<>();
Enter fullscreen mode Exit fullscreen mode

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

Use the final keyword.

public class MyClass {
    public final void myMethod() {
        // …
    }
}
Enter fullscreen mode Exit fullscreen mode

5. How do you create a thread in Java?

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

class MyThread extends Thread {
    public void run() {
        // …
    }
}
class MyRunnable implements Runnable {
    public void run() {
        // …
    }
}
Enter fullscreen mode Exit fullscreen mode

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.

public void myMethod() throws MyException {
    if (condition) {
        throw new MyException("Error occurred");
    }
}
Enter fullscreen mode Exit fullscreen mode

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

Use the finally block.

try {
    // risky code
} catch (Exception e) {
    // handle exception
} finally {
    // code to run regardless
}
Enter fullscreen mode Exit fullscreen mode

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

Use the filter() and map() methods.

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());
Enter fullscreen mode Exit fullscreen mode

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

Use synchronized or java.util.concurrent utilities.

private int counter = 0;
public synchronized void increment() {
    counter++;
}
Enter fullscreen mode Exit fullscreen mode

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.

Optional<String> opt = Optional.ofNullable(getNullableString());
String result = opt.orElse("default");
Enter fullscreen mode Exit fullscreen mode

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!

Top comments (0)