Java के Advanced Interview सवाल
In this page:
Checked vs Unchecked Exceptions
Checked exceptions compiler द्वारा enforce किए जाते हैं: एक method जो एक फेंक सकता है उसे इसे declare या catch करना चाहिए, callers को failure को explicitly handle करने के लिए मजबूर करते हुए। Unchecked exceptions (RuntimeException और इसकी subclasses) उस compile-time enforcement को पूरी तरह छोड़ देते हैं, यही वजह है कि ये आमतौर पर expected failure conditions के बजाय programming errors के लिए रखे जाते हैं।
उदाहरण: Checked vs Unchecked Exceptions
public class Main {
static void readFile() throws java.io.IOException { // checked -- must be declared or caught
throw new java.io.IOException("file missing");
}
static void process() { // no throws clause needed for unchecked
throw new RuntimeException("unchecked -- skips compile-time enforcement");
}
public static void main(String[] args) {
try { readFile(); } catch (java.io.IOException e) { System.out.println("Checked: " + e.getMessage()); }
try { process(); } catch (RuntimeException e) { System.out.println("Unchecked: " + e.getMessage()); }
}
}
Login to try C/C++/Java/PHP code in the editor
HashMap Collisions and Re-indexing
HashMap collisions तब होते हैं जब दो अलग keys एक ही bucket में hash होती हैं; Java इसे collide हुई entries को उस bucket के अंदर एक linked list में chain करके handle करता है, और Java 8+ में worst-case lookup time को O(n) तक घटने से बचाने के लिए पर्याप्त रूप से dense bucket की list को एक छोटे red-black tree में convert कर देता है।
उदाहरण: HashMap Collisions and Re-indexing
import java.util.*;
public class Main {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(1, "a"); map.put(17, "b"); // both may land in the same bucket depending on capacity
System.out.println(map.get(1) + " " + map.get(17)); // chained within the bucket, still found correctly
}
}
Login to try C/C++/Java/PHP code in the editor
Synchronized Methods vs Blocks
पूरे method को synchronize करना पूरे object (या class, अगर static हो) पर lock करता है, जो सरल है लेकिन अनावश्यक contention बना सकता है। Synchronized blocks आपको सिर्फ उन specific lines को lock करने देते हैं जो असल में shared state को छूती हैं, उसी method में असंबंधित काम के लिए दूसरे threads के इंतज़ार को घटाते हुए।
उदाहरण: Synchronized Methods vs Blocks
public class Main {
static int counter = 0;
static final Object lock = new Object();
static synchronized void incrementWholeMethod() { counter++; } // locks the whole object
static void incrementBlockOnly() {
synchronized (lock) { counter++; } // locks only these lines
}
public static void main(String[] args) {
incrementWholeMethod();
incrementBlockOnly();
System.out.println(counter);
}
}
Login to try C/C++/Java/PHP code in the editor
Comparable vs Comparator
Comparable किसी class के single, natural sort order को compareTo() के ज़रिए define करता है, जो सीधे class में ही बना होता है। Comparator class के बाहर रहता है और आपको original class को बिल्कुल बिना बदले एक या ज़्यादा वैकल्पिक, situational orderings define करने देता है।
उदाहरण: Comparable vs Comparator
import java.util.*;
public class Main {
static class Person implements Comparable<Person> {
String name; int age;
Person(String name, int age) { this.name = name; this.age = age; }
public int compareTo(Person o) { return Integer.compare(age, o.age); } // natural order, baked in
}
public static void main(String[] args) {
List<Person> people = new ArrayList<>(List.of(new Person("Bo", 30), new Person("Al", 20)));
Collections.sort(people); // uses Comparable
people.sort(Comparator.comparing(p -> p.name)); // Comparator, external alternative order
System.out.println(people.get(0).name);
}
}
Login to try C/C++/Java/PHP code in the editor
Shallow Copy vs Deep Copy
एक shallow copy सिर्फ top-level object और उसके direct field references को दोहराता है, इसलिए nested objects अब भी original और copy के बीच share होते हैं। एक deep copy पूरी तरह से नए nested objects भी recursively बनाता है, इसलिए एक copy की inner state में बदलाव कभी दूसरे में लीक नहीं होते।
उदाहरण: Shallow Copy vs Deep Copy
public class Main {
static class Address { String city; Address(String city) { this.city = city; } }
static class Person {
Address address;
Person(Address address) { this.address = address; }
Person shallowCopy() { return new Person(this.address); } // shares the same Address
Person deepCopy() { return new Person(new Address(this.address.city)); } // new nested object
}
public static void main(String[] args) {
Person original = new Person(new Address("Delhi"));
Person shallow = original.shallowCopy();
Person deep = original.deepCopy();
System.out.println(original.address == shallow.address);
System.out.println(original.address == deep.address);
}
}
Login to try C/C++/Java/PHP code in the editor
hashCode()के बिनाequals()को override करना, जिससे बराबर objects अलगHashMapbuckets में पड़ते हैं और lookups fail होते हैं।- यह मान लेना कि एक shallow copy स्वतंत्र है, जबकि nested objects अब भी share होते हैं, इसलिए copy में उन्हें बदलने से original भी बदल जाता है।
- पूरे method को synchronize करना जबकि सिर्फ एक छोटे हिस्से को lock चाहिए, जो कम threads को parallel में चलने देता है।
Chapter Quiz — Complete all 19 topics to unlock
0/19 topics done
Complete these topics first:
- Java Reflection API
- Java Annotations Advanced
- Java Garbage Collection
- Java Memory Management
- Java Performance Optimization
- Java Advanced Interview Questions
- Java CompletableFuture
- Java Atomic Classes
- Java Locks & Semaphores
- Java Concurrent Collections
- Java Cryptography Basics
- Java Hashing (MD5, SHA)
- Java SSL & HTTPS
- Java Logging (Log4j/SLF4J)
- Java Serialization Advanced
- Java Interview Questions Advanced
- Java Connection Pooling
- Java Test Driven Development
- Java Integration Testing