PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Vector Class and Stack Class in Java Collections (वेक्टर (Vector - लीगेसी सिंक्रोनाइज़्ड) और स्टैक क्लास) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: Vector and its subclass Stack are synchronized legacy classes introduced in JDK 1.0 and later retrofitted into the Java Collections Framework under the List interface. Because their method-level synchronization and flawed inheritance design hurt performance and encapsulation, modern Java favors ArrayList, CopyOnWriteArrayList, and ArrayDeque.
अध्याय सार: Vector और उसकी सब-क्लास Stack, JDK 1.0 की सिंक्रोनाइज़्ड लीगेसी क्लासेस हैं जिन्हें बाद में जावा कलेक्शंस फ्रेमवर्क में List इंटरफ़ेस के अंतर्गत जोड़ा गया। मेथड-स्तरीय सिंक्रोनाइज़ेशन ओवरहेड और दोषपूर्ण इनहेरिटेंस के कारण आधुनिक जावा में ArrayList और ArrayDeque को प्राथमिकता दी जाती है।
Vector Class and Stack Class in Java Collections: Detailed Notes & Architecture (वेक्टर (Vector - लीगेसी सिंक्रोनाइज़्ड) और स्टैक क्लास)
1. Vector Architecture, Legacy Methods, and Enumeration (Vector आर्किटेक्चर, लीगेसी मेथड और Enumeration इंटरफ़ेस)
[EN] java.util.Vector<E> is a legacy thread-safe dynamic array from JDK 1.0 that was retrofitted in JDK 1.2 to implement List<E>, RandomAccess, Cloneable, and Serializable. Alongside standard List methods (add, get, remove), Vector retains legacy methods such as addElement(), elementAt(), firstElement(), lastElement(), removeAllElements(), and capacity(). Calling vector.elements() returns a legacy java.util.Enumeration<E> (hasMoreElements(), nextElement()), which is read-only (no remove method) and is NOT fail-fast, unlike Vector.iterator() which IS fail-fast.
[HI] java.util.Vector<E>, JDK 1.0 की एक लीगेसी थ्रेड-सेफ डायनामिक एरे क्लास है जिसे JDK 1.2 में List<E> और RandomAccess से जोड़ा गया। मानक List मेथड्स के अतिरिक्त Vector में addElement(), elementAt(), firstElement(), removeAllElements() और capacity() जैसे पुराने मेथड्स भी मौजूद हैं। vector.elements() मेथड एक लीगेसी Enumeration<E> लौटाता है जो केवल रीड-ओनली है और fail-fast नहीं होता, जबकि vector.iterator() fail-fast व्यवहार दिखाता है।
2. Vector Capacity Growth (2x Doubling) and Synchronization Cost (Vector कैपेसिटी ग्रोथ (2x दोगुना होना) और सिंक्रोनाइज़ेशन लागत)
[EN] Like ArrayList, a default-constructed Vector starts with an initial capacity of 10, but differs sharply in how it resizes when full. If constructed with new Vector<>() or capacityIncrement <= 0, Vector doubles its capacity (2x growth: 10 -> 20 -> 40), whereas specifying new Vector<>(initCap, capIncrement) expands linearly by adding capacityIncrement each time. Every public method on Vector (including get() and size()) is marked synchronized on the Vector instance lock, ensuring single-operation thread safety at the cost of lock contention in single-threaded code. Importantly, compound actions such as checking !vec.isEmpty() followed by vec.get(0) are still NOT atomic unless externally synchronized by the caller.
[HI] ArrayList की तरह डिफ़ॉल्ट Vector भी 10 कैपेसिटी से शुरू होता है, परंतु भरने पर इसके बढ़ने का नियम अलग है। यदि capacityIncrement शून्य या नेगेटिव हो, तो Vector अपनी क्षमता को दोगुना (2x: 10 -> 20 -> 40) करता है, जबकि new Vector<>(init, inc) देने पर यह हर बार inc जोड़कर बढ़ता है। Vector के लगभग सभी पब्लिक मेथड synchronized हैं, जिससे सिंगल-थ्रेडेड कोड में भी लॉक ओवरहेड पड़ता है। यद्यपि, !vec.isEmpty() जाँचकर vec.get(0) कॉल करने जैसे संयुक्त (compound) ऑपरेशंस बिना बाहरी synchronized ब्लॉक के अभी भी थ्रेड-सेफ नहीं होते।
3. Stack Class: LIFO Operations and Inheritance from Vector (Stack क्लास: LIFO ऑपरेशंस और Vector से इनहेरिटेंस)
[EN] java.util.Stack<E> models a Last-In, First-Out (LIFO) stack by directly extending Vector<E> ('public class Stack<E> extends Vector<E>'). It defines five core stack methods on top of Vector: push(E item), pop() (removes and returns top), peek() (returns top without removing), empty() (boolean check), and search(Object o). If pop() or peek() is called on an empty Stack, it throws java.util.EmptyStackException (a RuntimeException). Pay special exam attention to Stack.search(Object o): it returns the 1-based distance from the top of the stack (top element has position 1), or -1 if the object is not found!
[HI] java.util.Stack<E>, Vector<E> को सीधे एक्सटेंड करके Last-In, First-Out (LIFO) स्टैक मॉडल प्रदान करता है। यह पाँच मुख्य मेथड जोड़ता है: push(E), pop() (शीर्ष तत्व हटाकर लौटाना), peek() (बिना हटाए देखना), empty() और search(Object)। खाली Stack पर pop() या peek() कॉल करने पर EmptyStackException उत्पन्न होता है। परीक्षा की दृष्टि से महत्वपूर्ण: Stack.search(Object o) शीर्ष से 1-आधारित (1-based) स्थिति लौटाता है (सबसे ऊपर वाले तत्व का मान 1 होता है), और न मिलने पर -1 लौटाता है।
4. Design Flaws and Modern Java Alternatives (ArrayDeque) (डिज़ाइन की कमियाँ और आधुनिक जावा विकल्प (ArrayDeque))
[EN] Extending Vector instead of encapsulating it via composition is widely recognized as a classic JDK 1.0 object-oriented design flaw in java.util.Stack. Because Stack inherits every Vector method, external code can bypass LIFO discipline by calling stack.add(0, x) or stack.remove(1) at arbitrary indices, corrupting stack semantics. Furthermore, inheriting Vector's method-level synchronization slows down single-threaded stack algorithms unnecessarily. For this reason, official Java documentation recommends using the Deque interface backed by ArrayDeque (Deque<Integer> s = new ArrayDeque<>()) for stacks, or ConcurrentLinkedDeque / BlockingDeque for concurrent stacks.
[HI] कम्पोजिशन के बजाय Vector को एक्सटेंड करना java.util.Stack की एक प्रसिद्ध JDK 1.0 डिज़ाइन कमी मानी जाती है। Vector के सभी मेथड्स इनहेरिट होने के कारण यूजर stack.add(0, x) या stack.remove(1) कॉल करके स्टैक के LIFO नियम को तोड़ सकता है। साथ ही, सिंक्रोनाइज़ेशन के कारण सिंगल-थ्रेडेड प्रोग्राम में प्रदर्शन घटता है। इसलिए जावा का आधिकारिक दस्तावेज़ स्टैक के लिए Deque<E> इंटरफ़ेस के साथ ArrayDeque (new ArrayDeque<>()) उपयोग करने की अनुशंसा करता है।
Syntax, Formula & Technical Example (Vector Class and Stack Class in Java Collections)
import java.util.*;
public class VectorStackDemo {
public static void main(String[] args) {
Vector<String> v = new Vector<>(2, 5);
v.add("A"); v.add("B"); v.add("C");
Stack<Integer> st = new Stack<>();
st.push(10); st.push(20); st.push(30);
System.out.println("Cap=" + v.capacity() + ", pos(10)=" + st.search(10));
}
}
Vector Class and Stack Class in Java Collections Quick Reference & Comparison Table
| Class / Interface | Core Characteristic | Key Exam Fact |
|---|---|---|
| Vector<E> | Synchronized dynamic array (2x growth) | Legacy JDK 1.0 List; default capacity 10 |
| Stack<E> | Extends Vector<E> for LIFO access | search() returns 1-based top offset; -1 if absent |
| Enumeration<E> | Legacy cursor via vector.elements() | Read-only and NOT fail-fast |
| ArrayDeque<E> | Modern non-synchronized Deque stack | Preferred replacement for Stack (O(1) push/pop) |
Vector Class and Stack Class in Java Collections Top 20 Bilingual MCQs with Answers
Q1. Which class does java.util.Stack extend in the Java standard library?
(जावा स्टैंडर्ड लाइब्रेरी में java.util.Stack किस क्लास को एक्सटेंड करता है?)
(A) java.util.Vector
(B) java.util.ArrayDeque
(C) java.util.LinkedList
(D) java.util.AbstractList
Correct Answer: (A)
[EN] java.util.Stack directly subclasses java.util.Vector ('class Stack<E> extends Vector<E>').
[HI] java.util.Stack सीधे java.util.Vector की सब-क्लास है।
Q2. What happens to the capacity of a default 'new Vector<>()' (capacity 10) when an 11th element is added?
(डिफ़ॉल्ट 'new Vector<>()' (कैपेसिटी 10) में 11वाँ एलिमेंट जोड़ने पर उसकी कैपेसिटी कितनी हो जाती है?)
(A) 20 (doubles by 100%)
(B) 30
(C) 15 (grows by 1.5x)
(D) 11
Correct Answer: (A)
[EN] When capacityIncrement is 0 or unspecified, Vector doubles its existing capacity (10 -> 20).
[HI] जब capacityIncrement न दिया हो या 0 हो, तो Vector भरने पर अपनी कैपेसिटी दोगुनी (10 से 20) कर लेता है।
Q3. If a Vector is constructed with 'new Vector<>(4, 3)' and 5 elements are added, what will v.capacity() return?
(यदि 'new Vector<>(4, 3)' से Vector बनाया जाए और 5 एलिमेंट्स जोड़े जाएँ, तो v.capacity() क्या लौटाएगा?)
(A) 10
(B) 7
(C) 6
(D) 8
Correct Answer: (B)
[EN] Because capacityIncrement is positive (3), resizing adds 3 to the old capacity 4, giving 7.
[HI] पॉज़िटिव capacityIncrement (3) देने पर रीसाइज़िंग में पुरानी कैपेसिटी 4 में 3 जुड़कर नई कैपेसिटी 7 हो जाती है।
Q4. Suppose you push 10, then 20, then 30 onto a Stack. What does 'stack.search(10)' return?
(मान लें आप Stack में क्रमशः 10, फिर 20, फिर 30 push करते हैं। 'stack.search(10)' क्या लौटाएगा?)
(A) 2
(B) 1
(C) 0
(D) 3
Correct Answer: (D)
[EN] Stack.search() returns the 1-based position from the top of the stack: 30 is at 1, 20 is at 2, and 10 is at 3.
[HI] Stack.search() शीर्ष से 1-आधारित स्थिति लौटाता है: 30 (1), 20 (2), और 10 की स्थिति 3 होगी।
Q5. What does Stack.search(obj) return if the object is NOT present in the stack?
(यदि खोजा गया ऑब्जेक्ट Stack में मौजूद न हो, तो Stack.search(obj) क्या लौटाता है?)
(A) 0
(B) -1
(C) null
(D) Throws EmptyStackException
Correct Answer: (B)
[EN] Stack.search() returns -1 when no matching element is found via equals().
[HI] तत्व न मिलने पर Stack.search() मेथड -1 लौटाता है।
Q6. Which exception is thrown if pop() or peek() is invoked on an empty java.util.Stack?
(खाली java.util.Stack पर pop() या peek() कॉल करने पर कौन-सा एक्सेप्शन उत्पन्न होता है?)
(A) IndexOutOfBoundsException
(B) EmptyStackException
(C) IllegalStateException
(D) NoSuchElementException
Correct Answer: (B)
[EN] java.util.Stack throws EmptyStackException on empty pop() or peek() (whereas ArrayDeque throws NoSuchElementException on pop()).
[HI] खाली Stack पर pop() या peek() करने पर EmptyStackException आता है (जबकि ArrayDeque.pop() पर NoSuchElementException आता है)।
Q7. What is the return value of stack.peek() when elements [A, B, C] (C on top) exist in the Stack?
(यदि Stack में [A, B, C] (C शीर्ष पर) हैं, तो stack.peek() क्या करता है?)
(A) Returns A (the bottom element)
(B) Removes and returns C
(C) Returns the integer size 3
(D) Returns C without removing it from the stack
Correct Answer: (D)
[EN] peek() inspects and returns the top element without modifying the stack.
[HI] peek() स्टैक से शीर्ष तत्व C को बिना हटाए केवल लौटाता है।
Q8. Which method is recommended instead of Stack for implementing a LIFO stack in modern single-threaded Java?
(आधुनिक सिंगल-थ्रेडेड जावा में LIFO स्टैक बनाने के लिए Stack के स्थान पर कौन-सा विकल्प अनुशंसित है?)
(A) Queue<E> stack = new PriorityQueue<>()
(B) Set<E> stack = new TreeSet<>()
(C) Deque<E> stack = new ArrayDeque<>()
(D) Vector<E> stack = new Vector<>()
Correct Answer: (C)
[EN] Java Javadoc explicitly states that Deque (implemented by ArrayDeque) provides a more complete and faster LIFO stack than legacy Stack.
[HI] जावा डॉक्यूमेंटेशन के अनुसार ArrayDeque द्वारा कार्यान्वित Deque, लीगेसी Stack से अधिक तेज़ और बेहतर LIFO विकल्प है।
Q9. Which cursor returned by vector.elements() is NOT fail-fast and lacks a remove() method?
(vector.elements() द्वारा लौटाया गया कौन-सा कर्सर fail-fast नहीं होता और जिसमें remove() मेथड भी नहीं होता?)
(A) Enumeration
(B) Iterator
(C) ListIterator
(D) Spliterator
Correct Answer: (A)
[EN] Enumeration only defines hasMoreElements() and nextElement(); it is read-only and non-fail-fast.
[HI] Enumeration में केवल hasMoreElements() और nextElement() होते हैं; यह रीड-ओनली और non-fail-fast होता है।
Q10. Why is Vector slower than ArrayList in single-threaded programs?
(सिंगल-थ्रेडेड प्रोग्राम्स में Vector, ArrayList की तुलना में धीमा क्यों होता है?)
(A) Vector prohibits RandomAccess indexing
(B) Vector sorts elements automatically on every add()
(C) All Vector methods acquire and release synchronization monitor locks on every call
(D) Vector uses a linked list instead of an array
Correct Answer: (C)
[EN] Method-level synchronized locks add monitor overhead on every operation even when only one thread accesses the Vector.
[HI] Vector के प्रत्येक मेथड पर synchronized लॉक होने से सिंगल-थ्रेडेड उपयोग में भी अनावश्यक लॉक ओवरहेड लगता है।
Q11. Which legacy method in Vector inserts or appends an element at the end, equivalent to List.add(E)?
(Vector में List.add(E) के समतुल्य लीगेसी मेथड कौन-सा है जो अंत में एलिमेंट जोड़ता है?)
(A) appendItem(E obj)
(B) addElement(E obj)
(C) insertFirst(E obj)
(D) putElement(E obj)
Correct Answer: (B)
[EN] Vector.addElement(E obj) is the synchronized legacy equivalent of appending to the end of the Vector.
[HI] addElement(E obj), Vector का लीगेसी मेथड है जो अंत में एलिमेंट जोड़ता है।
Q12. Why does Stack break strict LIFO encapsulation in Java?
(जावा में Stack कड़े LIFO एनकैप्सुलेशन को क्यों तोड़ता है?)
(A) Because Stack implements Map internally
(B) Because Stack.push() inserts at a random index
(C) Because extending Vector exposes indexed insert/delete methods like add(0, e) and remove(index)
(D) Because Stack allows only one element at a time
Correct Answer: (C)
[EN] Inheriting Vector's List methods permits random insertion and removal at any index, violating pure stack discipline.
[HI] Vector को एक्सटेंड करने के कारण Stack पर add(0, e) या remove(index) जैसे इंडेक्स मेथड्स भी चलते हैं, जो शुद्ध LIFO सिद्धांत के विरुद्ध है।
Q13. What does stack.empty() return compared to vector.isEmpty()?
(vector.isEmpty() की तुलना में stack.empty() क्या लौटाता है?)
(A) empty() returns an int count, while isEmpty() returns boolean
(B) Both return true when size() == 0
(C) empty() clears the stack, while isEmpty() only checks size
(D) empty() throws EmptyStackException if the stack has elements
Correct Answer: (B)
[EN] Stack.empty() is a legacy Stack method returning size() == 0, functionally identical to inherited Vector.isEmpty().
[HI] Stack.empty() स्टैक के खाली होने पर true लौटाता है, जो इनहेरिटेड Vector.isEmpty() के ही समान है।
Q14. Which of the following is NOT true about Vector and Stack in Java?
(जावा में Vector और Stack के बारे में निम्नलिखित में से कौन-सा कथन सत्य नहीं है?)
(A) Vector allows null elements and duplicate values
(B) Vector implements RandomAccess, Cloneable, and Serializable
(C) Stack extends AbstractList directly rather than Vector
(D) Vector was introduced in JDK 1.0 before the Collections Framework
Correct Answer: (C)
[EN] Stack extends Vector (not AbstractList directly); Vector itself extends AbstractList.
[HI] Stack सीधे AbstractList को नहीं बल्कि Vector को एक्सटेंड करता है (Vector स्वयं AbstractList को एक्सटेंड करता है)।
Q15. What happens if two threads concurrently iterate over a Vector using iterator() while a third thread calls vector.add()?
(यदि दो थ्रेड iterator() से Vector पर इटरेशन कर रहे हों और तीसरा थ्रेड vector.add() कॉल करे, तो क्या होगा?)
(A) Vector automatically freezes the third thread until iteration ends
(B) Only Enumeration throws ConcurrentModificationException
(C) Vector's iterator() is fail-safe and ignores the addition
(D) The iterators throw ConcurrentModificationException because Vector.iterator() is fail-fast
Correct Answer: (D)
[EN] Although individual Vector methods are synchronized, Vector's Iterator (unlike Enumeration) is fail-fast and detects structural modifications via modCount.
[HI] भले ही Vector के मेथड synchronized हैं, लेकिन इसका Iterator fail-fast है और इटरेशन के दौरान बदलाव होने पर ConcurrentModificationException फेंकता है।
Q16. If 'Vector<Integer> v = new Vector<>(5);' is created and 3 elements are added, what are v.size() and v.capacity()?
(यदि 'Vector<Integer> v = new Vector<>(5);' बनाकर 3 एलिमेंट्स जोड़े जाएँ, तो v.size() और v.capacity() क्या होंगे?)
(A) size() = 3 and capacity() = 10
(B) size() = 5 and capacity() = 10
(C) size() = 3 and capacity() = 5
(D) size() = 5 and capacity() = 5
Correct Answer: (C)
[EN] size() counts actual stored elements (3), while capacity() reports the backing array length (5).
[HI] size() वास्तविक संग्रहीत एलिमेंट्स (3) बताता है, जबकि capacity() आंतरिक एरे का आकार (5) बताती है।
Q17. Within Vector Class and Stack Class in Java Collections, which class / interface directly maps to: "Synchronized dynamic array (2x growth)"?
(Vector Class and Stack Class in Java Collections में कौन सा Class / Interface सीधे "Synchronized dynamic array (2x growth)" से संबंधित है?)
(A) ArrayDeque<E>
(B) Enumeration<E>
(C) Stack<E>
(D) Vector<E>
Correct Answer: (D)
[EN] In Vector Class and Stack Class in Java Collections, 'Vector<E>' handles 'Synchronized dynamic array (2x growth)' (Legacy JDK 1.0 List; default capacity 10).
[HI] Vector Class and Stack Class in Java Collections में 'Vector<E>' का कार्य/विवरण 'Synchronized dynamic array (2x growth)' (Legacy JDK 1.0 List; default capacity 10) है।
Q18. Which option accurately describes 'Stack<E>' in Vector Class and Stack Class in Java Collections?
(Vector Class and Stack Class in Java Collections के संदर्भ में 'Stack<E>' की सही विशेषता कौन सी है?)
(A) Extends Vector<E> for LIFO access
(B) Synchronized dynamic array (2x growth)
(C) Modern non-synchronized Deque stack
(D) Legacy cursor via vector.elements()
Correct Answer: (A)
[EN] 'Stack<E>' provides 'Extends Vector<E> for LIFO access' — key point: search() returns 1-based top offset; -1 if absent.
[HI] 'Stack<E>' का मुख्य विवरण 'Extends Vector<E> for LIFO access' है (search() returns 1-based top offset; -1 if absent)।
Q19. In Vector Class and Stack Class in Java Collections, identify the correct parameter for the specification: "Legacy cursor via vector.elements()".
(Vector Class and Stack Class in Java Collections में "Legacy cursor via vector.elements()" विवरण किस विकल्प के लिए सही है?)
(A) ArrayDeque<E>
(B) Enumeration<E>
(C) Vector<E>
(D) Stack<E>
Correct Answer: (B)
[EN] 'Enumeration<E>' corresponds to 'Legacy cursor via vector.elements()' (Read-only and NOT fail-fast).
[HI] 'Enumeration<E>' का संबंध 'Legacy cursor via vector.elements()' (Read-only and NOT fail-fast) से है।
Q20. Considering Vector Class and Stack Class in Java Collections, what key behavior defines 'ArrayDeque<E>'?
(Vector Class and Stack Class in Java Collections में 'ArrayDeque<E>' की मुख्य भूमिका या मान क्या है?)
(A) Synchronized dynamic array (2x growth)
(B) Extends Vector<E> for LIFO access
(C) Legacy cursor via vector.elements()
(D) Modern non-synchronized Deque stack
Correct Answer: (D)
[EN] 'ArrayDeque<E>' represents 'Modern non-synchronized Deque stack' (Preferred replacement for Stack (O(1) push/pop)).
[HI] 'ArrayDeque<E>' का अर्थ/मान 'Modern non-synchronized Deque stack' है (Preferred replacement for Stack (O(1) push/pop))।
Vector Class and Stack Class in Java Collections — Exam FAQs
Q1: Is a compound operation like 'if (!vector.contains(x)) vector.add(x);' automatically thread-safe on Vector?
क्या Vector पर 'if (!vector.contains(x)) vector.add(x);' जैसा संयुक्त ऑपरेशन स्वतः थ्रेड-सेफ होता है?
[EN] No; individual calls to contains() and add() acquire and release the lock separately, so another thread can interleave between them unless wrapped in synchronized(vector).
[HI] नहीं; contains() और add() अलग-अलग लॉक लेते और छोड़ते हैं, इसलिए बीच में दूसरा थ्रेड डेटा बदल सकता है; इसके लिए बाहरी synchronized(vector) ब्लॉक आवश्यक है।
Q2: Why did Java keep Vector and Stack instead of deleting them?
जावा ने पुरानी Vector और Stack क्लासेस को हटाने के बजाय क्यों बनाए रखा?
[EN] To maintain strict backward compatibility with legacy JDK 1.0/1.1 applications and libraries while retrofitting them onto the List interface.
[HI] पुराने JDK 1.0/1.1 एप्लिकेशंस के साथ बैकवर्ड कम्पैटिबिलिटी बनाए रखने के लिए इन्हें हटाने के बजाय List इंटरफ़ेस से जोड़कर रखा गया।
Q3: Does Vector.elements() Enumeration fail fast when modified during traversal?
क्या ट्रैवर्सल के दौरान बदलाव होने पर Vector.elements() का Enumeration fail-fast होता है?
[EN] No; Enumeration does not track modCount, so it does not throw ConcurrentModificationException (though skipped/stale elements may occur without external locking).
[HI] नहीं; Enumeration आंतरिक modCount की जाँच नहीं करता, इसलिए वह ConcurrentModificationException नहीं फेंकता।
Post a Comment
Post a Comment