Unit 8 Flashcards

1
Q

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.

A

B) A collection of data organized in some fashion, providing operations like insert, remove, sort, and find.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which of the following is NOT a typical operation provided by data structures?

A) Compile
B) Insert
C) Sort
D) Find

A

A) Compile

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Which of the following is an example of a data structure provided by Java?

A) String
B) ArrayList
C) int
D) println()

A

B) ArrayList

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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.

A

C) It provides high-performance, high-quality implementations of useful data structures and algorithms.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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.

A

B) An ordered collection of elements accessed by integer indexes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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.

A

D) A collection of key-value pairs where each key uniquely maps to a value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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.

A

B) To prevent runtime errors by enforcing type safety at compile time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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

A) hello

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which interface does not allow duplicate elements?

A) List
B) Set
C) Map
D) Both List and Map

A

B) Set

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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.

A

C) The duplicate element will be ignored.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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.

A

C) The get method is being called with the wrong parameter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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

A

C) List

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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.

A

B) Retains only the elements in the collection that are contained in the specified collection.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

A) 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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.

A

B) HashSet does not guarantee any order, while TreeSet stores elements in a sorted order.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the use of the keySet method in the Map interface?

A) It returns a collection of the values in the map.
B) It checks if a particular key exists in the map.
C) It returns a Set view of the keys contained in the map.
D) It removes a key from the map.

A

C) It returns a Set view of the keys contained in the map.

17
Q

What will be the result of this Java code? List<String> list = new ArrayList<>(); list.add("A"); list.add("B"); list.remove(0); System.out.println(list.get(0));</String>

A) A
B) B
C) An IndexOutOfBoundsException
D) null

A

B) B

18
Q

What does the Collections.sort() method do?

A) Sorts any type of collection.
B) Sorts elements of a List according to natural ordering or by a provided Comparator.
C) Returns a sorted collection without modifying the original.
D) Sorts a Map by its keys.

A

B) Sorts elements of a List according to natural ordering or by a provided Comparator.

19
Q

Why are generics important in the context of the Java Collections Framework?

A) They allow Java collections to be type-safe.
B) They make Java collections run faster.
C) They are required to use Java collections.
D) They allow collections to change size dynamically.

A

A) They allow Java collections to be type-safe.

20
Q

Why might you choose LinkedList over ArrayList in Java?
A) If frequent random access to elements is needed.
B) If memory usage is the primary concern.
C) If frequent insertion and deletion of elements are needed.
D) If only sequential access of elements is required.

A

C) If frequent insertion and deletion of elements are needed.

21
Q

What operation would you use to find common elements between two sets in Java?
A) addAll()
B) retainAll()
C) removeAll()
D) clear()

A

B) retainAll()

22
Q

What Java method would you use to iterate over both the keys and values of a Map simultaneously?
A) keySet()
B) values()
C) entrySet()
D) get()

A

C) entrySet()

23
Q

What is a benefit of using generics in Java Collections?
A) Allows collections to store different types of objects.
B) Prevents ClassCastException at runtime.
C) Increases the processing speed of the program.
D) Automatically sorts any type of collection.

A

B) Prevents ClassCastException at runtime.

24
Q

What is a key advantage of using an array-based structure like ArrayList over a link-based structure like LinkedList?
A) Faster insertion and deletion at any point in the list.
B) Less memory overhead per element.
C) Elements can be accessed in constant time.
D) Better performance with a larger size of elements.

A

C) Elements can be accessed in constant time.