Java Collections Framework Quiz
30 Questions
1 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is the output of the given code snippet that adds two types of numeric wrapper classes to a HashSet?

  • Compilation Failure
  • Test - 10 Test - 10 (correct)
  • Test - 10
  • Runtime Exception
  • Which of these exceptions occurs when two threads modify a TreeSet at the same time?

  • IteratorModificationException is thrown
  • Both threads can perform action successfully
  • No exceptions are thrown but data integrity is compromised
  • ConcurrentModificationException is thrown (correct)
  • What method would you use to confirm the number of bits required to hold a BitSet object?

  • length() (correct)
  • capacity()
  • numberOfBits()
  • size()
  • What will be the output of the Hashtable's toString method in the given code?

    <p>{A=3, C=8, B=2}</p> Signup and view all the answers

    Which of the following concepts is not related to the implementation of the List interface?

    <p>RoleUnresolvedList</p> Signup and view all the answers

    In terms of complexity, how many times is a key hashed when saving a key-value pair in a hash table?

    <p>Twice</p> Signup and view all the answers

    Which interface is considered the fundamental interface that inherits to all other interfaces in the Java Collections Framework?

    <p>Collection</p> Signup and view all the answers

    What will be the output of the given ArrayList manipulation program?

    <p>[A, D, B, C]</p> Signup and view all the answers

    Which of these classes offers a structure similar to an array but is part of the Java Collections Framework?

    <p>BitSet</p> Signup and view all the answers

    Which of the following provides a way to store elements ensuring both uniqueness and insertion order?

    <p>LinkedHashSet</p> Signup and view all the answers

    What does the method obj1.equals(obj2) return in the first Java program?

    <p>true</p> Signup and view all the answers

    What is the output when printing obj in the second Java program?

    <p>{A=1, B=2, C=3}</p> Signup and view all the answers

    Which class object is specifically used to store key-value pairs?

    <p>All of the mentioned</p> Signup and view all the answers

    Which of the following can form a dynamic array in Java?

    <p>ArrayList &amp; Vector</p> Signup and view all the answers

    What output does the Collections.reverse(list) produce when printed using the iterator in the Collection_Algos class?

    <p>1 5 8 2</p> Signup and view all the answers

    Which method retrieves an element at a specified index in the Vector class example?

    <p>elementAt()</p> Signup and view all the answers

    In the context of HashMap, which option describes its retrieval mechanism?

    <p>Retrieves by key through hashing.</p> Signup and view all the answers

    What data structure does the LinkedList class implement in the Collection_Algos program?

    <p>Deque</p> Signup and view all the answers

    Which statement best describes the difference between ArrayList and Vector?

    <p>Vector is synchronized while ArrayList is not.</p> Signup and view all the answers

    In the vector class example, what number is printed at index 1?

    <p>2</p> Signup and view all the answers

    What will be the output of the following Java program involving the BitSet class? import java.util.*; class Bitset { public static void main(String args[]) { BitSet obj1 = new BitSet(5); BitSet obj2 = new BitSet(10); for (int i = 0; i < 5; ++i) obj1.set(i); for (int i = 3; i < 13; ++i) obj2.set(i); obj1.and(obj2); System.out.print(obj1); } }

    <p>{3, 4}</p> Signup and view all the answers

    What are the correct roles of front and rear pointers in a CircularQueue implementation?

    <p>Front pointer points to first element; rear pointer points to the last element</p> Signup and view all the answers

    Which of the following is not a subinterface of Queue?

    <p>BlockingEnque</p> Signup and view all the answers

    Which method is used to reduce the capacity of an ArrayList object?

    <p>trimToSize()</p> Signup and view all the answers

    Which of these methods is used to sort the elements of an ArrayList?

    <p>Collections.sort(listObj);</p> Signup and view all the answers

    In which package can all collection classes be found?

    <p>java.util</p> Signup and view all the answers

    Which method can be used to obtain a set of all keys in a Map?

    <p>keySet()</p> Signup and view all the answers

    Which of these methods is a member of the Remote class?

    <p>None of the mentioned</p> Signup and view all the answers

    Which method of the Map class is used to obtain an element in the map using a specified key?

    <p>get()</p> Signup and view all the answers

    What is the name of the data member of the Vector class used to store the number of elements in the vector?

    <p>elementCount</p> Signup and view all the answers

    Study Notes

    Java Collections Framework Questions and Answers

    • BitSet and Intersection: Given BitSet obj1 with elements 0-4 set and BitSet obj2 with elements 3-12 set, obj1.and(obj2) operations results in BitSet containing elements {3, 4}.

    • CircularQueue Pointers: In a CircularQueue implementation, the front pointer points to the first element, and the rear pointer points to the last element.

    • Queue Subinterfaces: BlockingQueue and TransferQueue are subinterfaces of Queue. BlockingEnque is not a subinterface of Queue.

    • ArrayList Capacity Reduction: Use trimToSize() to reduce the capacity of an ArrayList object.

    • ArrayList Sorting: Use Collections.sort(listObj) to sort elements in an ArrayList.

    • Collection Package: The java.util package contains all the collection classes.

    • Map KeySet: Use keySet() method to obtain a set of all keys in a map.

    • Remote Class Methods: The Remote class doesn't have any methods listed in the given options.

    • Map Element Retrieval: Use the get() method to obtain an element in a Map given its key.

    • Vector Element Count: The elementCount data member of the Vector class stores the number of elements in the vector.

    • HashMap KeySet Output: HashMap.keySet() returns a Set containing the keys as an output. The output is [A, B, C] (in a Set format).

    • Date Object Output: A Date object returns the current time and date.

    • ArrayList Element Access Complexity: Accessing an element in ArrayList has a worst-case time complexity of O(1).

    • Dynamic Array Implementation: ArrayList implements a dynamic array.

    • Queue Insertion/Deletion: Use enqueue and dequeue methods to insert and delete items from the queue.

    • List Interface Implementations: Standard collection classes that implement all the standard functions on list data structures include ArrayList and LinkedList.

    • TreeSet Thread Safety: Attempting to modify a TreeSet from multiple threads concurrently will throw a ConcurrentModificationException. This exception is thrown if an iterator is used to modify the underlying collection during iteration by another thread.

    • HashSet with Different Types: Adding Long and Integer values to a HashSet will result in the distinct values being accommodated. The HashSet can hold unique elements of any type, even if they're of different types. Here, the object will convert to its primitive type and be stored (in this scenario 10).

    • Legacy Interface: The Enumeration interface is a legacy interface in the Java Collections Framework.

    • Hashtable Contains Method: The contains() method of Hashtable returns false if a given object is not present as a value in the Hashtable.

    • List Implementation: RoleUnresolvedList, Stack, and AttibuteList are valid implementations of List interface ArrayList and LinkedList. SessionList is not mentioned as a valid interface.

    • Hashing in HashMap: The key is hashed twice (hashcode() and hashfunction()) to locate the appropriate bucket for storing the key-value pair in a HashMap.

    • Basic Collection Interface: The Collection interface is the fundamental interface for other collection interfaces (like List, Set, and Queue).

    • ListIterator Index: Use .previousIndex() to get the index of the previous element in a ListIterator.

    • ArrayList Modification: Adding an element at a specific index in ArrayList, will shift existing elements to create space, output [A, D, B, C]

    • TreeMap Output: The entrySet() method in a TreeMap returns a view of the map's entries stored in a Set format and the output will be [A=1, B=2, C=3] (in set format).

    • BitSet Object Size: Use .length() to get the number of bits required to hold the BitSet object.

    • Remote Method Exception: The RemoteException is thrown by remote methods.

    • Array-like Architecture: BitSet has an architecture similar to an array.

    • Array Sorting: Use Arrays.sort(array) to sort an array.

    • Hashtable Output: The output of obj.toString() for a Hashtable object will be formatted {key=value, key=value} .

    • Legacy Map Interface: The Dictionary interface (and its implementation Hashtable were legacy interfaces that Map has since superceded (replaced).

    • Array Initialization and Sorting Output: The code correctly initializes and sorts an array, resulting in the output 1 2 3 4 5.

    • LinkedHashSet Feature: LinkedHashSet maintains the order of insertion and ensures uniqueness of elements..

    • ArrayList Comparison-Equality: Comparing two ArrayList objects for equality (using equals()) checks if they are the same size and have the same elements in the same order.

    • HashMap Output: The toString() method for HashMap object is {key=value, key=value}.

    • Key-Value Storage: The classes that use keys to store values are Map, Hashtable, and Dictionary.

    • Dynamic Array Classes: ArrayList and Vector are classes that represent dynamic arrays.

    • LinkedList Iteration and Reversal: Iterating over a reversed LinkedList using an iterator results in the output in reverse order (1 5 8 2).

    • Vector Element Retrieval: The elementAt(index) method of Vector is used to retrieve the element at a specific index. Output in this case is 2.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Description

    Test your knowledge of the Java Collections Framework with this quiz. It covers topics such as BitSets, CircularQueues, and ArrayLists, as well as important methods and subinterfaces. Perfect for Java enthusiasts looking to solidify their understanding of collections.

    More Like This

    Use Quizgecko on...
    Browser
    Browser