Logo

dev-resources.site

for different kinds of informations.

Top 10 Java interview questions

Published at
10/9/2023
Categories
java
interview
questions
answers
Author
fromfullstack
Categories
4 categories in total
java
open
interview
open
questions
open
answers
open
Author
13 person written this
fromfullstack
open
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!

questions Article's
30 articles in total
Favicon
10 Must-Know Software Testing Interview Questions
Favicon
How does Optional.ifPresent() differ from Optional.orElse()?
Favicon
10 questions developers should ask our self
Favicon
DSA: Array - questions
Favicon
DSA: LinkedList questions
Favicon
DSA: Stack - Questions
Favicon
How to Build an Effective Study Space for IT Certification Preparation: A BCS Business Analysis (BAPv5) Approach
Favicon
Why do I get so many followers on this platform?
Favicon
The Ultimate 2024 Adobe AD0-E555 Exam Question Set for Confident Preparation
Favicon
How to change the redirect URL when changing a user's email in Supabase?
Favicon
Figure out gfg IDLE
Favicon
When "The Best" isn't good enough
Favicon
20 Best Questions to Ask Yourself for Self Growth
Favicon
How to ask better questions?
Favicon
Join Our Organisation
Favicon
How do animation libraries affect SEO in the Next JS?
Favicon
Choosing right tech stack
Favicon
Frontend Fundamentals: Top 10 Interview Questions and In-Depth Answers for Every Developer
Favicon
Top 11 Microprocessor Interview Questions and Answers
Favicon
What should I do?
Favicon
Master Your Success Journey with Pass4success SAP C_S43_2022 Exam Prep
Favicon
Can anyone recommend a modern CSS course?
Favicon
Creating Smart Questions with NestJS and OpenAI
Favicon
111+ Interview Questions - Flutter
Favicon
How to Structure your Resume After your First Job
Favicon
Top 25 Kafka interview questions
Favicon
Top 10 Java interview questions
Favicon
Getting an issue with the (!) mark after the domain name
Favicon
101 questions for python developer
Favicon
google chrome pdf extension

Featured ones: