Unit 8 Flashcards
What is a data structure?
A) A specific algorithm to process data.
B) A collection of data organized in some fashion, providing operations like insert, remove, sort, and find.
C) A type of database used for data analytics.
D) A low-level system operation on data.
B) A collection of data organized in some fashion, providing operations like insert, remove, sort, and find.
Which of the following is NOT a typical operation provided by data structures?
A) Compile
B) Insert
C) Sort
D) Find
A) Compile
Which of the following is an example of a data structure provided by Java?
A) String
B) ArrayList
C) int
D) println()
B) ArrayList
What is true about the Java Collections Framework?
A) It includes only List and Set interfaces.
B) It is used to handle special types of data like timestamps.
C) It provides high-performance, high-quality implementations of useful data structures and algorithms.
D) Maps extend java.util.Collection.
C) It provides high-performance, high-quality implementations of useful data structures and algorithms.
What is a List in Java?
A) A collection of key-value pairs.
B) An ordered collection of elements accessed by integer indexes.
C) A set of unique elements without duplicates.
D) A method to handle exceptions.
B) An ordered collection of elements accessed by integer indexes.
What distinguishes a Map in Java?
A) Allows duplicate keys.
B) Ordered collection of key-value pairs.
C) Does not allow the retrieval of elements.
D) A collection of key-value pairs where each key uniquely maps to a value.
D) A collection of key-value pairs where each key uniquely maps to a value.
What is the primary benefit of using generics in Java?
A) To allow the same method to operate on different types of collections.
B) To prevent runtime errors by enforcing type safety at compile time.
C) To increase the runtime efficiency of applications.
D) To provide cryptography capabilities to Java applications.
B) To prevent runtime errors by enforcing type safety at compile time.
What will the following Java code output? List<String> list = new ArrayList<>(); list.add("hello"); list.add("world"); System.out.println(list.get(0));</String>
A) hello
B) world
C) [hello, world]
D) 0
A) hello
Which interface does not allow duplicate elements?
A) List
B) Set
C) Map
D) Both List and Map
B) Set
What will happen if you try to add a duplicate element to a Set in Java?
A) Compile-time error.
B) Runtime exception.
C) The duplicate element will be ignored.
D) The original element will be replaced.
C) The duplicate element will be ignored.
What is wrong with this code snippet? Map map = new HashMap(); map.put(“key”, “value”); System.out.println(map.get(“value”));
A) “map” should be declared with specific types.
B) HashMap does not support the get method.
C) The get method is being called with the wrong parameter.
D) There is no error in the code.
C) The get method is being called with the wrong parameter.
In Java, which collection type should be used when you need to access elements frequently by an index?
A) Set
B) Queue
C) List
D) Map
C) List
What does the retainAll method do when called on a collection?
A) Removes all elements of the collection.
B) Retains only the elements in the collection that are contained in the specified collection.
C) Adds all elements from the specified collection to the collection.
D) None of the above.
B) Retains only the elements in the collection that are contained in the specified collection.
Consider the following code: Set<Integer> set = new HashSet<>(); set.add(1); set.add(2); set.add(1); System.out.println(set.size()); What will it output?</Integer>
A) 2
B) 3
C) 1
D) 4
A) 2
What is the primary difference between HashSet and TreeSet in Java?
A) HashSet allows null values, whereas TreeSet does not.
B) HashSet does not guarantee any order, while TreeSet stores elements in a sorted order.
C) There are no differences; they function identically.
D) TreeSet is faster than HashSet.
B) HashSet does not guarantee any order, while TreeSet stores elements in a sorted order.