🎤 Top 82 Interview Questions

Yeh sabse common Core Java interview questions hain — tap karke answer kholo.

JVM Java bytecode ko run karta hai (ek virtual computer). JRE = JVM + libraries, jo program run karne ke liye chahiye. JDK = JRE + compiler aur tools, jo code likhne/compile karne ke liye chahiye.
Java code ek baar compile hoke bytecode (.class file) banta hai, jo kisi bhi OS par JVM ke through chal sakta hai — bina dobara likhe.
Bytecode universal hai, sirf JVM OS-specific hai. Har OS apni JVM install karta hai jo same bytecode ko samajh leti hai — is wajah se code portable rehta hai.
Primitive types (int, char, boolean, etc.) directly value store karte hain aur fixed size ke hote hain. Reference types (String, Array, Object) memory address store karte hain jo actual object ki taraf point karta hai.
int ek primitive type hai. Integer ek wrapper class hai jo int ko object ki tarah use karne deti hai (jaise Collections mein) — is process ko autoboxing kehte hain.
int=0, double=0.0, boolean=false, char=(khaali), aur object references (String etc) = null. Local variables ki koi default value nahi hoti, unhe initialize karna zaroori hai.
'==' reference (memory address) compare karta hai — do objects same jagah hain ya nahi. '.equals()' content/value compare karta hai — jaise do Strings ka text same hai ya nahi.
User se keyboard input lene ke liye — jaise Scanner sc = new Scanner(System.in); sc.nextInt();
Class ek blueprint/template hai jo batati hai ki object ke paas kaunse fields (data) aur methods (behavior) honge.
Object class ka real instance hai, jo apni khud ki values rakhta hai aur memory mein banta hai jab 'new' keyword use karte hain.
Encapsulation, Abstraction, Inheritance, aur Polymorphism.
Data (fields) ko private rakh kar getter/setter se access dena. Isse data safe rehta hai aur galti se bahar se change nahi hota.
Car chalate waqt sirf steering/pedal use karte ho, engine ke internal working ki chinta nahi karni padti — complex cheez chhupi hoti hai, sirf zaroori interface dikhta hai.
Code reusability — common properties/behavior ek parent class mein likh kar, child classes usse reuse kar sakti hain bina dobara likhe.
Nahi, ek class sirf ek class ko extend kar sakti hai (diamond problem se bachne ke liye). Lekin ek class multiple interfaces implement kar sakti hai.
Same class mein same method naam, par different parameters (number/type) — jaise add(int,int) aur add(int,int,int). Ye compile-time polymorphism hai.
Child class parent class ke method ko same signature ke saath apne tareeke se dobara define karti hai. Ye run-time polymorphism hai.
Overloading same class mein hota hai (parameters alag), compile-time par decide hota hai. Overriding parent-child mein hota hai (signature same), run-time par decide hota hai.
Ek special method jo class ke naam jaisa hota hai aur object banate waqt (new se) automatically call hoti hai, taaki initial values set ho sakein.
Same class mein multiple constructors, different parameters ke saath — jisse object ko alag-alag tareeke se banaya ja sake.
Agar tum khud koi constructor nahi likhte, to Java automatically ek khaali (no-argument) constructor de deta hai.
Current object ko refer karne ke liye — jaise jab constructor parameter aur field ka naam same ho, this.name = name likhte hain.
Parent class ke constructor, method ya field ko refer/call karne ke liye use hota hai child class ke andar se.
static member class ka hota hai, kisi single object ka nahi — sab objects usse share karte hain. Object banaye bina bhi ClassName.member se access kar sakte ho.
Kyunki static method kisi object se bandha nahi hota — wo class-level hota hai, isliye 'this' (jo current object refer karta hai) uska koi matlab nahi rehta.
final variable ki value change nahi ho sakti (constant). final method ko override nahi kiya ja sakta. final class ko extend nahi kiya ja sakta.
Ek class jise directly 'new' se object nahi bana sakte. Isme kuch methods complete aur kuch abstract (bina body ke) ho sakte hain — child class ko abstract methods complete karne padte hain.
Ek pura contract jisme (traditionally) sirf method signatures hote hain, koi implementation nahi. Class 'implements' keyword se interface follow karti hai.
Agar completely unrelated classes ke beech common behavior chahiye (contract) to Interface. Agar related classes ke beech kuch common code share karna hai to Abstract class.
default aur static methods add ho gaye, jinki body ho sakti hai — pehle interface mein sirf abstract methods hi allowed the.
Program run hote waqt aane wali ek unexpected problem/error, jaise divide by zero ya invalid array index — jo agar handle na ho to program crash kar deti hai.
Checked exceptions compile-time par check hoti hain aur handle karna zaroori hai (jaise IOException). Unchecked exceptions run-time par aati hain aur handle karna optional hai (jaise NullPointerException).
try mein risky code likhte hain. Agar error aaye, matching catch block use handle karta hai. finally block hamesha chalta hai, error aaye ya na aaye.
throw ek exception ko manually 'phenkne' (raise karne) ke liye use hota hai method ke andar. throws method signature mein batata hai ki ye method kaunsi exceptions de sakta hai.
Jab tum kisi null reference (jisme koi object nahi hai) par method call karne ya field access karne ki koshish karte ho.
Bahut rare cases mein — jaise agar JVM khud crash ho jaaye (System.exit() call ho jaaye try block mein), tabhi finally skip hota hai.
Exception class (ya RuntimeException) ko extend karke apni class banate hain — jaise class InsufficientBalanceException extends Exception.
Ek resizable (dynamic size) list jo List interface implement karti hai — order maintain karti hai, duplicates allow karti hai, index se access hota hai.
Array ka size fixed hota hai aur primitive types bhi store kar sakta hai. ArrayList ka size dynamically badh/ghat sakta hai, lekin sirf objects store karta hai.
Set interface implement karta hai, sirf unique values store karta hai (duplicates automatically ignore ho jaate hain), order guarantee nahi hota.
Key-value pairs store karta hai. Har key ka hashcode calculate karke wo decide karta hai value kahan store hogi, isliye key se value dhoondna bahut fast hota hai.
List ordered hai, duplicates allow karta hai. Set sirf unique values rakhta hai. Map key-value pairs mein data store karta hai.
ArrayList array-based hai, random access (index se) fast hai. LinkedList nodes ki chain hai, beech mein insert/delete fast hai lekin random access slow hai.
Ek object jo Collection (List, Set, etc) ke elements ko ek-ek karke traverse karne deta hai, hasNext() aur next() methods ke saath.
Program ke execution ka ek independent flow/path — Java program kam se kam ek thread (main thread) mein chalta hai, aur extra threads bana kar parallel kaam kiya ja sakta hai.
Thread class ko extend karke, ya Runnable interface ko implement karke — dono mein run() method likhte hain aur start() se thread chalate hain.
start() ek naya thread banata hai aur usme run() ko execute karta hai (parallel). Directly run() call karne se koi naya thread nahi banta, wo normal method call jaisa hi chalta hai (same thread mein).
Jab multiple threads same data ko ek saath modify karne ki koshish karein, to synchronized ek waqt mein sirf ek thread ko us code block/method mein aane deta hai — data corrupt hone se bachata hai.
Security, thread-safety aur performance ke liye — String Pool mein same value ke Strings share ho sakte hain sirf tabhi jab wo change na ho sakein. Isse memory bhi bachta hai.
String immutable hai. StringBuilder mutable hai but thread-safe nahi (fast, single-thread ke liye best). StringBuffer bhi mutable hai aur thread-safe hai (synchronized, thoda slow).
Widening chhote type ko bade mein automatically convert karta hai (jaise int->double), koi loss nahi. Narrowing bade ko chhote mein manually cast karna padta hai (jaise double->int), data (decimal) kho sakta hai.
Primitive type (jaise int) ko automatically uske wrapper class object (Integer) mein convert karna, jaise Collections mein primitive store karne ke liye.
Method ko variable ki value ki copy milti hai, original variable change nahi hota. Objects ke case mein reference ki copy milti hai, isliye object ke andar ke changes dikhte hain, par reference khud reassign nahi hota.
Recursion mein method khud ko call karta hai. Base case wo condition hai jaha recursion rukta hai — agar na ho to StackOverflowError aa jaayega (infinite calls).
Agar case ke baad break na lagayein, to control agle case mein bhi chala jaata hai, chahe wo match na kare — isko fall-through kehte hain.
int[][] grid = new int[3][4]; se 3 rows aur 4 columns ka grid banta hai, grid[row][col] se access hota hai.
HashMap/HashSet correctly kaam karne ke liye — agar do objects equals() se equal hain, to unka hashCode() bhi same hona chahiye, warna Collections mein unexpected behavior aa sakta hai.
ClassName@hashcode jaisa (jaise Point@1b6d3586) — isliye usually override karke readable format diya jaata hai.
Static nested class outer object ke bina bhi bana sakte ho (independent). Inner (non-static) class ko outer class ke ek object ki zaroorat hoti hai, aur wo outer ke instance members access kar sakti hai.
Jab ek class ki sirf ek baar, turant zaroorat ho (jaise event listener) — naam diye bina, inline define kar dete hain.
Type-safety milti hai — sirf predefined constants hi allowed hote hain, galat/random values compile-time par hi reject ho jaate hain.
Type-safety aur code reusability — ek hi class/method kayi types ke saath kaam kar sakta hai, aur galat type dalne par compiler turant error deta hai.
Agar koi modifier na likho, to member sirf usi package ke andar accessible hota hai, bahar ke package se nahi.
Related classes ka group/folder — code organize karta hai aur naming conflicts se bachata hai (jaise java.util, java.io).
Comparable (compareTo method) class ke andar define hota hai aur uski "natural ordering" batata hai. Comparator (compare method) class ke bahar likha jaata hai aur custom/multiple orderings dene deta hai.
First-In-First-Out — jo element sabse pehle add hua wahi sabse pehle remove hota hai, jaise ek ticket line.
Ek chhota, naam-less function jo ek functional interface (sirf ek abstract method wala interface) ko turant implement karta hai — jaise (a, b) -> a + b.
Collections par filter, map, sort jaise operations ko chain karke, bina manual loops likhe, chhote aur readable code mein likh sakte hain.
Ek interface jisme sirf ek hi abstract method ho — jaise Runnable, Comparator. Ye lambda expressions ke target type hote hain.
Resource (jaise file, connection) ko automatically close kar deta hai jab try block khatam hota hai, chahe error aaye ya na aaye — manually close() call karne ki zaroorat nahi.
File ko line-by-line, fast tareeke se padhne ke liye — ye internal buffering karta hai jo baar-baar disk read karne se better performance deta hai.
Ek hi catch block mein '|' operator se multiple exception types pakadna, jaise catch (IOException | SQLException e) { } — code duplication kam karta hai.
Bade projects mein specific error types ko organize karne ke liye — jaise ek base AppException se PaymentException, LoginException jaisi specific exceptions banti hain, jinhe alag-alag ya ek saath catch kiya ja sakta hai.
New, Runnable, Running, Waiting/Blocked, aur Terminated.
Jab do (ya zyada) threads ek-doosre ke paas jo resource hai uska intezaar karte reh jaate hain, aur koi bhi aage nahi badh paata — program hamesha ke liye atak jaata hai.
Ek thread pool manager jo threads ko manually banane/manage karne ke bajaye, ready threads ko reuse karta hai — better resource management deta hai.
Stack method calls aur local variables store karti hai (fast, auto-clean). Heap objects store karti hai (jo 'new' se bante hain), aur ye tab tak rehte hain jab tak koi reference use na kare.
Jab us object ki koi reference (kisi bhi variable se) bachi na ho — tab wo "unreachable" ho jaata hai aur GC eligible ban jaata hai.
Class pehli baar load hote hi sirf ek baar chalta hai — object banane se pehle bhi, static variables initialize karne ke liye use hota hai.
Object ko bytes ke stream mein convert karna (jaise file mein save karne ya network par bhejne ke liye) — class ko Serializable interface implement karna padta hai. Wapas object banane ko deserialization kehte hain.
int, char, String, aur enum jaise types par — Java 7+ mein String bhi switch mein use ho sakti hai.
Jaise ek chessboard ya spreadsheet — rows aur columns dono chahiye, jaise seating chart ya tic-tac-toe board.