Top 15 Java Interview Questions for 1–2 Years Experience – Real Answers

🟡 Top 15 Java Interview Questions for 1–2 Years Experience




If you're a Java developer with 1–2 years of experience, companies like TCS, Infosys, Capgemini, and Cognizant expect you to know Core Java, OOPs, Collections, and basic project-level concepts. Below are 15 most frequently asked questions with easy-to-remember answers.


Q1. What is the difference between JDK, JRE, and JVM?

 JVM (Java Virtual Machine)

  1. Definition: JVM is a virtual runtime environment that executes Java bytecode.

  2. Function: It converts bytecode (.class files) into machine-specific code, making Java platform-independent ("write once, run anywhere").

  3. Key Point: JVM doesn't understand .java files; it runs compiled .class files.

Example:
When you run a Java program using java MyClass, the JVM is what executes MyClass.class.


 JRE (Java Runtime Environment)

  1. Definition: JRE is a software package that contains JVM + core libraries + other components required to run Java applications.

  2. Function: It provides the environment to run already compiled Java programs.

  3. Key Point: It does NOT include development tools like the compiler (javac).

Use case:
If you're an end user just running a Java app (not developing), you only need JRE.


 JDK (Java Development Kit)

  1. Definition: JDK is a full-fledged development kit that includes everything in JRE plus development tools like javac, javadoc, javap, etc.

  2. Function: It is used to develop, compile, and debug Java applications.

  3. Key Point: JDK = JRE + Compiler + Debugger + Tools

Use case:
As a developer writing Java code, you need the JDK to compile .java files and produce .class files.

Q2. What is the difference between == and equals()?

Interview Answer: Difference Between == and equals() in Java

When you're asked in an interview about the difference between == and equals() in Java, here's a structured way to explain it confidently:


== Operator:

  • == is a reference comparison operator.

  • It checks whether two object references point to the same memory location.

  • For example:

    String s1 = new String("Java");
    String s2 = new String("Java");
    System.out.println(s1 == s2); // false
    

    Even though s1 and s2 have the same content, they are different objects in memory, so == returns false.


equals() Method:

  • equals() is a method defined in the Object class and is overridden by many classes like String, List, etc.

  • It is used to compare the actual content or value of two objects.

  • For example:

    System.out.println(s1.equals(s2)); // true
    

    This returns true because equals() checks the logical equality based on content.


📌 Key Differences:

Criteria == (Operator) equals() (Method)
Type Operator Method
Comparison Basis Reference (memory address) Content (logical equality)
Applicable On Primitives and Object References Objects (must override for custom classes)
Result true if references are same true if contents are same
Common Mistake Used accidentally for string comparison Recommended for string/value comparison


Q3. What are wrapper classes in Java?

Wrapper classes convert primitive types to objects. E.g., int → Integer, char → Character

Q4. What is method overloading?

When multiple methods in the same class have the same name but different parameters.

Q5. What is method overriding?

When a subclass provides a specific implementation of a method already defined in its superclass.

Q6. Difference between Abstract Class and Interface?

Interfaces support multiple inheritance and can’t have constructors. Abstract classes can have both abstract and non-abstract methods.

Q7. Difference between ArrayList and LinkedList?

ArrayList: Faster for accessing elements
LinkedList: Faster for inserting/deleting elements

Q8. What does the final keyword do?

It prevents change: final variable (constant), final method (can’t override), final class (can’t extend)

Q9. What is exception handling in Java?

Mechanism to handle runtime errors using try, catch, finally, and throws.

Q10. Checked vs Unchecked Exceptions?

Checked: Compile-time (e.g., IOException)
Unchecked: Runtime (e.g., NullPointerException)

Q11. What is String immutability?

Strings in Java are immutable. Once created, their values cannot be changed.

Q12. What are static methods and variables?

Belong to the class, not instance. Can be accessed without creating an object.

Q13. What is constructor overloading?

Creating multiple constructors in a class with different parameter lists.

Q14. What is garbage collection?

Automatic memory cleanup. Unused objects are removed by the Garbage Collector.

Q15. How to remove duplicates from an ArrayList?

Use LinkedHashSet to preserve order and remove duplicates.

Example logic: new ArrayList<>(new LinkedHashSet<>(list));


✅ Tips for Candidates:

  • Revise OOPs, Collections, Exception Handling
  • Be ready to explain your current project
  • Know how to write basic SQL queries

📅 Last Updated: 15 May 2025
📎 #JavaInterview #1YearExperience #JavaQnA

Post a Comment

0 Comments