Core Java Interview Questions and Answers

Jul 8, 2024

Core Java Interview Questions and Answers

Main Topics Covered

  • Execution of try, catch, and finally blocks
  • Usage of Java Runtime Environment (JRE) and Java Development Kit (JDK)
  • Garbage Collection in JVM
  • Memory Leaks
  • Object-Oriented Programming (OOP) Concepts
  • Usage of main method (public static void main)
  • Method Overloading and Overriding
  • Abstract Classes and Interfaces
  • Design Patterns

Detailed Notes

Execution of try, catch, and finally

  • Return Statement in try/catch: Even if a return statement is executed, the finally block still runs.
  • Using try with finally without catch: It's possible to handle cleanup without a catch block to propagate exceptions.
  • Performance Impact: Using try, catch, and finally can slightly impact performance due to exception handling overhead but is generally minimal.
  • Exceptions:
    • finally block won’t execute if JVM exits via System.exit() during try or catch execution.
    • Multiple finally blocks are not allowed within a single try-catch-finally structure.
  • Handling Multiple Exceptions: Use a single catch block separated by a pipe | symbol to handle multiple exceptions with the same logic.

Java Runtime Environment (JRE) and Java Development Kit (JDK)

  • Running Java Applications: Cannot run without JRE as it contains essential tools and libraries.
    • Alternative tool: JLink in newer Java versions (from Java 9) allows bundling Java applications with their own JRE.
    • GraalVM: Can build a native image that doesn't need JRE to run.
  • JDK without JRE: Not possible, as JDK includes JRE containing essential components for running and developing Java applications.
  • Different Garbage Collection Algorithms: Mark-Sweep, Mark-Compact, Generational Copying are used depending on the collector chosen.
  • Memory Leaks: Can occur even with automatic garbage collection when objects are no longer needed but still referenced.

OOP Concepts

  • Java is Partially Object-Oriented: Uses primitive types like int, char which are not objects.
  • Advantages of Being Partially Object-Oriented:
    • Simple non-object types (like int) help Java run faster and use less memory.
    • Allows Java to work well with technologies and systems not fully object-oriented.
  • Use in Enterprise Projects: Object-oriented programming (OOP) helps organize code better, making it easier to update and scale, saving time and effort.
  • Access Modifiers: public static void main(String[] args) is the entry point of Java applications.
    • public: Accessible from anywhere.
    • static: Doesn’t need an object instance to be called.
    • void: Doesn’t return any value.
    • main: Method name.
  • Primitive Data Types: Cannot be null. Default values are provided (e.g., 0 for int, false for boolean).
  • Wrapper Classes: Enable storing primitives in collections by treating them as objects.

Method Overloading and Overriding

  • Overloading: Using the same method name with different inputs in the same class.
  • Overriding: Occurs when a subclass has a method with the same name, return type, and parameters as in its parent class.
  • Dynamic Method Dispatch: Determines which method to use at runtime when methods are overridden.

Abstract Classes and Interfaces

  • Abstract Classes: Cannot create objects directly; must be extended by other classes.
  • Interfaces: List methods a class should have without detailing how they work.
    • Can include static and default methods but no state.
    • Used for achieving polymorphism.

Design Patterns

  • Singleton Pattern: Commonly used to manage database connections ensuring shared instance reuse efficiently.
  • Thread Safety in Singleton: Ensures only one thread can access shared data structure at a time.
  • Concurrent Programming: Immutable objects are useful because they can be shared without needing synchronization.

Other Key Points

  • Encapsulation: Keeps important data hidden and safe, ensuring security and integrity.
  • Static Block: Can throw an exception but must handle it within the block.
  • Functional Interface: Allows only a single abstract method but can have inherited static and default methods.
  • Java Collections: Uses different algorithms for sorting (dual-pivot quicksort for primitives, timsort for objects).
    • HashMap in Java 8: Handles collisions using a balanced tree for efficient searching.
  • Java Server Creation: Possible without frameworks using Java SE API (ServerSocket for TCP, HttpServer for HTTP).

Conclusion

  • Recap of important interview topics.
  • Encourage exploring Java interview preparation kits for comprehensive guidance.