← Back to Advanced Java Course | Chapter 11: Advanced & Security | Lesson 6 of 19

Java के Advanced Interview सवाल

ये Java job interviews में लोगों से पूछे जाने वाले कठिन सवाल हैं, जैसे किसी बड़े match के लिए practice। इनमें collections, threads और copying जैसी चीज़ें शामिल हैं।

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

java
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()); }
	}
}

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

java
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
	}
}

Synchronized Methods vs Blocks

पूरे method को synchronize करना पूरे object (या class, अगर static हो) पर lock करता है, जो सरल है लेकिन अनावश्यक contention बना सकता है। Synchronized blocks आपको सिर्फ उन specific lines को lock करने देते हैं जो असल में shared state को छूती हैं, उसी method में असंबंधित काम के लिए दूसरे threads के इंतज़ार को घटाते हुए।

उदाहरण: Synchronized Methods vs Blocks

java
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);
	}
}

Comparable vs Comparator

Comparable किसी class के single, natural sort order को compareTo() के ज़रिए define करता है, जो सीधे class में ही बना होता है। Comparator class के बाहर रहता है और आपको original class को बिल्कुल बिना बदले एक या ज़्यादा वैकल्पिक, situational orderings define करने देता है।

उदाहरण: Comparable vs Comparator

java
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);
	}
}

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

java
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);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. hashCode() के बिना equals() को override करना, जिससे बराबर objects अलग HashMap buckets में पड़ते हैं और lookups fail होते हैं।
  2. यह मान लेना कि एक shallow copy स्वतंत्र है, जबकि nested objects अब भी share होते हैं, इसलिए copy में उन्हें बदलने से original भी बदल जाता है।
  3. पूरे method को synchronize करना जबकि सिर्फ एक छोटे हिस्से को lock चाहिए, जो कम threads को parallel में चलने देता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.