1.
// dans une méthode main
// création du set
Set<String> set = new HashSet<String>() ;
// ajout d'élément
System.out.println("J'ajoute un : " + set.add("un")) ;
System.out.println("J'ajoute deux : " + set.add("deux")) ;
// ajout d'un doublon : échec
System.out.println("J'ajoute encore un : " + set.add("un")) ;
// affichage de la taille du set
System.out.println("Taille du set : " + set.size()) ;
2.
What are we referring to when we mention java.util.Collections ?
Correct Answer
C. A utilility class for use with collections
Explanation
The correct answer is a utility class for use with collections. The java.util.Collections class is a utility class that provides various static methods for manipulating and working with collections, such as sorting, searching, and shuffling elements. It does not refer to the interface from which lists, queues, and sets extend, nor does it refer to the entire Collections framework.
3.
What type of collection would we use if we wanted no duplicates?
Correct Answer
D. Set
Explanation
If we want to have no duplicates in a collection, we would use a Set. Sets are data structures that do not allow duplicate elements. They store unique elements and provide efficient operations for adding, removing, and checking for the presence of elements. Unlike lists, which can contain duplicate elements, sets ensure that each element is unique. Therefore, a Set would be the appropriate choice if we want to avoid duplicates in our collection.
4.
What type of List is used with synchronized access?
Correct Answer
B. Vector
Explanation
Vector is the correct answer because it is a type of list that provides synchronized access. This means that multiple threads can safely access and modify a Vector object without causing data corruption or inconsistency. Vector achieves this by using synchronized methods, which ensure that only one thread can access the Vector at a time. In contrast, ArrayList and LinkedList do not provide synchronized access by default, so they are not suitable for scenarios where multiple threads need to access the list concurrently.
5.
Question 4 :What type of collection does not extend the Collection<E> interface?
Correct Answer
B. Map
Explanation
Map is the correct answer because it does not extend the Collection interface. Map is a key-value pair data structure that allows storing and retrieving values based on a unique key. It is not considered a collection in the traditional sense because it does not implement the Collection interface. The other options (List, Queue, and Set) all extend the Collection interface and are considered collections.
6.
What type of Map is used with synchronized access?
Correct Answer
C. Hashtable
Explanation
Hashtable is the correct answer because it is a synchronized implementation of the Map interface. It ensures that multiple threads can access and modify the Hashtable object in a synchronized manner, preventing any concurrent modification exceptions or data inconsistencies. Other options like HashMap and LinkedHashMap are not synchronized, meaning they are not thread-safe and can lead to issues in a multi-threaded environment. Set is not a type of Map, so it is not the correct answer.
7.
There are no direct implementation of the Collection interface?
Correct Answer
A. True
Explanation
The statement is true because the Collection interface is an abstract interface in Java that provides a set of methods to manipulate a group of objects. It does not have any direct implementations in the Java Collections Framework. Instead, it has subinterfaces such as List, Set, and Queue, which provide concrete implementations like ArrayList, HashSet, and LinkedList, respectively. Therefore, there are no classes that directly implement the Collection interface.
8.
What type of collection has always been type safe
Correct Answer
A. Array
Explanation
Arrays in most programming languages are type safe, meaning that they can only store elements of a specific data type. This is because arrays have a fixed size and data type when they are declared. Therefore, when elements are added or accessed in an array, the compiler or interpreter ensures that they are of the correct type. This helps prevent type errors and ensures that the array remains type safe throughout its usage.
9.
What is the following code snippet an example of <T extends Number> ?
Correct Answer
A. bounded type
Explanation
The given code snippet is an example of a bounded type. This is indicated by the syntax "". The "extends Number" part specifies that the type parameter T can only be substituted with a type that is a subclass of Number. This restricts the possible types that can be used with T to a specific range, in this case, any subclass of Number.
10.
We can use generics with any type?
Correct Answer
B. False
Explanation
The statement that we can use generics with any type is false. Generics in programming allow us to create reusable code that can work with different types. However, there are certain restrictions on the types that can be used with generics. For example, we cannot use primitive types such as int or boolean directly with generics. Instead, we need to use their corresponding wrapper classes like Integer or Boolean. Additionally, the type used with generics must be a reference type and cannot be a value type. Therefore, generics cannot be used with any type.
11.
How are generics implemented?
Correct Answer
B. Erasure
Explanation
Generics in Java are implemented using erasure. Erasure means that type parameters are replaced with their bounds or with the Object type if no bounds are specified. During compilation, the compiler removes all generic type information, resulting in type erasure. This is done to ensure backward compatibility with older versions of Java that do not support generics. At runtime, the generic type information is not available, and all generic types are treated as their upper bound or as Object.
12.
What is the following code snippet an example of ?
Correct Answer
D. Unbounded wildcard type
Explanation
The given code snippet is an example of an unbounded wildcard type. This is because it does not specify any upper or lower bounds for the type parameter. The code uses a wildcard character "?" without any restrictions, allowing any type to be used as the argument.
13.
Is the following code snippet valid? ListaList = new ArrayList();
Correct Answer
B. NO
Explanation
The given code snippet is not valid because it is missing the generic type parameter in the declaration of the ArrayList. In Java, when creating an instance of a generic class like ArrayList, you need to specify the type of objects that the list will hold. Therefore, the correct way to create an ArrayList would be: ArrayList<Type> ListaList = new ArrayList<>();
14.
Is the following code snippet valid?public class SimpleGenericInterfaceImpl implements SimpleGenericInterface { ?
Correct Answer
A. YES
Explanation
The given code snippet is valid because it implements the interface "SimpleGenericInterface". This means that the class "SimpleGenericInterfaceImpl" provides an implementation for all the methods defined in the interface.
15.
Question 14:What is the following code snippet an example of <? extends Number> ?
Correct Answer
C. Bounded wildcard type
Explanation
The given code snippet is an example of a bounded wildcard type. The syntax "
16.
What do sets care about
Correct Answer
C. Uniqueness
Explanation
Sets care about uniqueness because they are a data structure that only allows unique elements. This means that duplicate values are not allowed in a set. Sets use this property to efficiently store and retrieve data, as they can quickly determine if an element is already present in the set or not. By ensuring uniqueness, sets can effectively eliminate duplicates and provide a collection of distinct elements.
17.
What type of set would you use if you wanted a fast access ordered set but didn't care about sorting?
Correct Answer
B. LinkedHashSet
Explanation
A LinkedHashSet would be the appropriate choice in this scenario. While a HashSet provides fast access, it does not guarantee any specific order. On the other hand, a TreeSet does provide a sorted order, but it may not be the fastest option. A LinkedHashSet combines the advantages of both, offering fast access like a HashSet and maintaining the insertion order. Therefore, it would be the ideal choice when speed is a priority, but sorting is not necessary.
18.
Can we have a sorted and unordered set?
Correct Answer
B. NO
Explanation
A set is an unordered collection of unique elements. The elements in a set have no specific order. Therefore, it is not possible to have a sorted set as sorting implies a specific order.
19.
What type of set would you use if you wanted fast access and didn't care about ordering?
Correct Answer
A. HashSet
Explanation
HashSet would be the appropriate type of set to use if fast access and ordering are not important. HashSet uses a hashing mechanism to store elements, allowing for constant-time operations such as adding, removing, and checking for the presence of an element. The elements in a HashSet are not ordered, meaning that the order in which they are inserted or accessed may not be preserved. However, this lack of ordering allows for faster access to elements compared to LinkedHashSet or TreeSet, which maintain some form of ordering.
20.
What type of exception is raised when we run a program with objects that are not mutually comparable in a sorted set?
Correct Answer
B. ClassCastException
Explanation
A ClassCastException is raised when we run a program with objects that are not mutually comparable in a sorted set. This exception occurs when we try to compare or sort objects that do not implement the Comparable interface or do not have a custom Comparator defined. It indicates that the objects cannot be cast to a comparable type, resulting in a runtime error.
21.
What type of set would you use if you wanted it sorted?
Correct Answer
C. TreeSet
Explanation
A TreeSet would be the appropriate choice if you wanted a sorted set. Unlike HashSet and LinkedHashSet, TreeSet maintains the elements in a sorted order based on their natural ordering or a custom comparator. It uses a balanced tree data structure (specifically a Red-Black tree) to achieve this sorting. Therefore, when elements are added to a TreeSet, they are automatically arranged in ascending order, allowing for efficient retrieval of elements in a sorted manner.
22.
Which type of list implements the Queue interface
Correct Answer
B. LinkedList
Explanation
LinkedList is a type of list that implements the Queue interface. It is a doubly-linked list that allows for efficient insertion and removal of elements at both ends. This makes it suitable for implementing a queue, which follows the First-In-First-Out (FIFO) principle. Elements can be added to the end of the list using the "offer" or "add" methods, and removed from the front of the list using the "poll" or "remove" methods. Therefore, LinkedList is the correct answer for this question.
23.
What type of collections are Lists?
Correct Answer
A. Ordered
Explanation
Lists are a type of collection that is ordered. This means that the elements in a list are stored in a specific sequence and can be accessed by their position or index. The order of the elements in a list is maintained and remains consistent unless explicitly changed. This allows for predictable and reliable retrieval of elements from a list.
24.
Which List class is synchronized?
Correct Answer
C. Vector
Explanation
Vector is the correct answer because it is a synchronized implementation of the List interface in Java. This means that Vector provides thread-safe operations, ensuring that multiple threads can access and modify the Vector object without causing any data inconsistencies or race conditions. In contrast, ArrayList and LinkedList are not synchronized by default, so they are not suitable for concurrent access unless synchronized externally.
25.
What do Lists have in common?
Correct Answer
A. An index
Explanation
Lists have in common an index. This means that each element in a list can be accessed and located using its position or index within the list. The index allows for easy retrieval and manipulation of specific elements within the list.
26.
Which List would you use if wanted fast access and were doing lots of insertions and deletions?
Correct Answer
B. LinkedList
Explanation
LinkedList would be the best choice if fast access, insertions, and deletions are required frequently. Unlike ArrayList, which needs to shift elements when inserting or deleting, LinkedList only needs to update the references to the next and previous elements. This makes LinkedList more efficient for these operations. However, it is important to note that LinkedList might not perform as well as ArrayList for random access, as it requires traversing the list from the beginning to reach a specific element.
27.
You can't put null elements into Lists in the API?
Correct Answer
B. False
Explanation
The statement "You can't put null elements into Lists in the API" is incorrect. In the API, it is possible to add null elements to a List. The List data structure allows for the storage of null values along with other types of objects. Therefore, the correct answer is False.
28.
What type of ordering does a PriorityQueue have?
Correct Answer
A. Ordered and sorted
Explanation
A PriorityQueue has an ordered and sorted type of ordering. This means that the elements in the queue are arranged in a specific order based on their priority. The element with the highest priority is placed at the front of the queue and is the first one to be removed. The remaining elements are also arranged in descending order of priority. This ensures that the elements are processed in the correct order according to their priority level.
29.
What type of Queue is a PriorityQueue?
Correct Answer
C. PIPO
30.
A PriorityQueue is always sorted in natural order?
Correct Answer
B. False
Explanation
A PriorityQueue is not always sorted in natural order. It is a data structure that stores elements in a queue-like manner, but each element has a priority associated with it. The priority determines the order in which the elements are served. The elements in a PriorityQueue are not sorted based on their natural order but rather based on their priority. The element with the highest priority is served first, not necessarily the smallest or largest element.
31.
Within a PriorityQueue the elements sorted last are processed first?
Correct Answer
B. False
Explanation
In a PriorityQueue, the elements are sorted based on their priority, with the highest priority elements being processed first. Therefore, the statement that the elements sorted last are processed first is incorrect. The correct answer is False.
32.
What do maps care about
Correct Answer
C. Uniqueness
Explanation
Maps care about uniqueness because it is important for a map to have distinct and non-repetitive elements. In a map, each key is unique and associated with a specific value. This ensures that the map can accurately store and retrieve data without any ambiguity. If there were duplicate keys in a map, it would lead to conflicts and make it difficult to access or modify the desired data. Therefore, uniqueness is a crucial aspect that maps prioritize to maintain data integrity and efficient operations.
33.
What type of map would you use if you wanted a fast access ordered map but didn't care about sorting?
Correct Answer
C. LinkedHashMap
Explanation
A LinkedHashMap would be the best choice for a fast access ordered map without the need for sorting. It maintains the insertion order of the elements, which allows for efficient retrieval of data. The LinkedHashMap achieves this by using a combination of a hash table and a linked list. The hash table provides fast access to the elements, while the linked list maintains the order of insertion. This makes it a suitable option when the order of insertion is important, but sorting is not necessary.
34.
Can we have a sorted and unordered map?
Correct Answer
B. False
Explanation
No, we cannot have a sorted and unordered map. A map is an associative container that stores elements in a key-value pair, allowing fast retrieval of values based on their keys. An unordered map does not maintain any particular order of elements, while a sorted map arranges the elements in a specified order, typically based on the keys. Therefore, a map cannot be both sorted and unordered at the same time.
35.
What type of map would you use if you wanted fast access and didn't care about ordering?
Correct Answer
A. HashMap
Explanation
If you want fast access and don't care about ordering, you would use a HashMap. HashMap is a data structure in which elements are stored in key-value pairs, and it uses hashing techniques to provide fast access to elements. It does not maintain any particular order of the elements. Therefore, HashMap is the most suitable choice in this scenario.
36.
What type of exception is raised when we try run a program with objects that are not mutually comparable in a sorted map?
Correct Answer
B. ClassCastException
Explanation
When we try to run a program with objects that are not mutually comparable in a sorted map, a ClassCastException is raised. This exception occurs when we attempt to cast an object to a type that is not compatible with its actual type. In this case, the objects in the sorted map are not comparable, so trying to compare them will result in a ClassCastException being thrown.
37.
What type of map would you use if you wanted it sorted?
Correct Answer
D. TreeMap
Explanation
A TreeMap is a type of map that is sorted based on the natural ordering of its keys. It uses a red-black tree data structure to maintain the order of the keys. This means that when you iterate over the entries in a TreeMap, they will be returned in ascending order of their keys. Therefore, if you want a map that is sorted, a TreeMap would be the appropriate choice.
38.
Which static nested class of Map<K,V> allows us to get a reference to a mapping via an iterator ?
Correct Answer
A. Map.Entry
Explanation
The static nested class Map.Entry allows us to get a reference to a mapping via an iterator. This class represents a key-value mapping in a Map and provides methods to retrieve both the key and the value associated with the mapping. By using an iterator on a Map.Entry object, we can iterate over the mappings in a Map and access each mapping individually.
39.
What type of map has synchronized methods?
Correct Answer
B. Hashtable
Explanation
Hashtable is a type of map that has synchronized methods. This means that when multiple threads try to access or modify the Hashtable at the same time, the methods ensure that only one thread can access the Hashtable at a time, preventing any data inconsistencies or conflicts. This synchronization ensures thread safety, making Hashtable suitable for concurrent applications where multiple threads may access or modify the map simultaneously.
40.
What type of variables can we use with the java.util.Arrays class?
Correct Answer
D. Both
Explanation
The java.util.Arrays class in Java can be used with both primitive variables and reference variables. This means that we can use this class to work with arrays of primitive data types such as int, char, or boolean, as well as arrays of objects. The Arrays class provides various methods for manipulating arrays, such as sorting, searching, and filling, making it a versatile tool for working with both types of variables.
41.
If two arrays hold the same elements they are considered equal by the Arrays.equals() method?
Correct Answer
B. False
Explanation
The given statement is false. The Arrays.equals() method in Java compares the elements of two arrays to check if they are equal. However, it does not consider two arrays with the same elements but in a different order as equal. The order of elements in an array matters when comparing arrays using the Arrays.equals() method.
42.
We can use any object with the methods of the java.util.Arrays class?
Correct Answer
A. True
Explanation
We can use any object with the methods of the java.util.Arrays class because the methods in this class are static, meaning they can be called directly on the class itself rather than on an instance of the class. This allows us to use the methods of the Arrays class without needing to create an object of that class.
43.
What type of variables can we use with the java.util.Collections Class?
Correct Answer
B. reference variables
Explanation
The java.util.Collections Class can only use reference variables. Primitive variables cannot be used with this class.
44.
Which method do we need to implement in the java.lang.Comparable interface?
Correct Answer
B. compareTo()
Explanation
The method that needs to be implemented in the java.lang.Comparable interface is compareTo(). This method is used to compare the current object with another object of the same type. It returns a negative integer if the current object is less than the other object, zero if they are equal, and a positive integer if the current object is greater than the other object. By implementing this method, we can define the natural ordering of objects and use it for sorting or searching operations.
45.
Which interface would we use to sort a class that can't be modified?
Correct Answer
B. java.util.Comparator
Explanation
To sort a class that cannot be modified, we would use the java.util.Comparator interface. This interface allows us to define a separate class that implements the Comparator interface and provides a custom comparison logic for the objects of the class that needs to be sorted. By using the Comparator interface, we can sort objects based on different criteria without modifying the original class.
46.
Which method do we need to implement in the java.util.Comparator interface?
Correct Answer
A. Compare()
Explanation
In the java.util.Comparator interface, the method that needs to be implemented is compare(). This method is used to compare two objects and determine their order. It takes two parameters representing the objects to be compared and returns an integer value indicating their relative order. The compare() method is commonly used in sorting operations, where it helps in determining the order of elements in a collection based on a specific criteria.
47.
How many ways does the java.lang.Comparable interface allow us to sort a collection?
Correct Answer
B. 1
Explanation
The java.lang.Comparable interface allows us to sort a collection in one way. This interface provides a single method called compareTo(), which compares the current object with another object and returns a negative value, zero, or a positive value depending on the comparison result. This method is used by sorting algorithms to determine the order of elements in the collection. Therefore, the correct answer is 1.
48.
What is returned from both the compare() and compareTo() methods?
Correct Answer
C. Int
Explanation
The compare() and compareTo() methods both return an integer value. These methods are often used for comparing two objects and determining their relative ordering. The returned integer value indicates whether the first object is less than, equal to, or greater than the second object. The specific value of the integer can vary depending on the implementation, but it typically follows a convention such as returning a negative value for less than, zero for equal, and a positive value for greater than.
49.
See the following code, what is the result ?
Correct Answer
N/A