Java Interview Preparation Guide | 30 Most Important Questions
Core Java Interview Questions
Introduction to Core Java Interview Preparation
- The video introduces the importance of Data Structures and Algorithms (DSA) in interviews, emphasizing that interviewers also assess core Java fundamentals.
- The presenter has compiled a list of the top 30 most important core Java interview questions frequently asked in real interviews.
Compilation and Execution of Java Programs
- When compiling a Java program, source code (.java files) is converted into bytecode (.class files), which the JVM can execute.
- During execution, the JVM reads .class files and converts bytecode into machine code for CPU understanding.
Understanding JDK, JRE, and JVM
- JVM (Java Virtual Machine) runs programs by executing .class files; it is part of JRE (Java Runtime Environment).
- JRE includes core libraries needed at runtime while JDK (Java Development Kit) contains tools like compilers necessary for development.
Memory Management in Java
- The JVM allocates several memory areas when running a program: heap, stack, method area, program counter register, and native method stack.
- Stack memory stores method calls and local variables specific to each thread; heap memory stores objects shared across threads.
String Immutability and Variable Types
- Strings in Java are immutable; once created, their values cannot be changed.
- Static methods cannot access non-static variables directly due to scope limitations.
Default Values of Data Types
- Default values for various data types include zero for numeric types (byte, short, int), false for boolean, null for strings and objects.
Variable Declarations in Methods
- Local variables are declared inside methods or constructors; instance variables are declared within classes but outside any method or block. Static variables are shared among all instances of a class.
Control Flow Statements: Break vs Continue vs Return
continueskips the current iteration of a loop without exiting it;breakexits the entire loop;returnexits from the method entirely after executing its statements.
Exception Handling Basics
- The parent class of all classes in Java is
Object.
- Arrays cannot store different data types unless they are defined as an array of type Object.
Differences Between HashMap and Hashtable
- HashMap allows one null key and multiple null values while Hashtable does not allow any null keys or values. HashMap is not thread-safe compared to Hashtable's thread safety features.
Exception Keywords: Throw vs Throws
throwis used within methods to throw exceptions explicitly;throwsindicates that a method may throw exceptions during execution as part of its signature.
Try-Catch-Finally Block Rules
- A try block must have either catch or finally associated with it.
- It’s valid to have try with catch only or try with finally only.
Error vs Exception Distinction
- Exceptions can be caught and handled by programs (e.g., IOExceptions).
- Errors represent serious issues beyond control (e.g., out-of-memory errors).
Garbage Collection Process
- Garbage collection automatically removes unreferenced objects from memory to free up space when no longer needed by the program.
Final Keyword Usage
finalprevents modification when applied to fields/method/classes.
finallyexecutes regardless of exception occurrence.
finalize()cleans up before garbage collection occurs.
Main Method Signature Explained
- The main method serves as an entry point called by JVM: public means accessible outside class; static allows calling without instantiation; void indicates no return value; string args accepts command-line arguments.
Multiple Main Methods & Class Structure
- Yes, multiple main methods can exist if parameters differ but only one will be executed based on standard signature recognized by JVM.
- There’s no difference between writing main string args[] versus main string[] args—both styles are acceptable.
Initial Capacity Defaults in Collections
- Default initial capacity for HashMap is 16 while ArrayList starts at 10 elements upon creation.
Immutable Classes in Java
- Only String class is immutable among options provided—StringBuilder and StringBuffer allow modifications post-initialization.
Object-Oriented Nature of Java
- While primarily object-oriented due to its use of classes/objects, primitive data types prevent it from being fully object-oriented since they aren't treated as objects inherently despite wrapper classes existing for them.