MCQ IN COMPUTER SCIENCE & ENGINEERING

COMPUTER SCIENCE AND ENGINEERING

ALGORITHMS

Question [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
What must be true in order to sort objects?
A
They must be wrapper classes of primitives (Integer, Double, etc.)
B
They must be Comparable.
C
At least one instance variable must be a primitive.
D
They must be Strings
E
Objects are not sortable, only primitives are sortable.
Explanation: 

Detailed explanation-1: -Sort an Object with Comparable To sort an Object by its property, you have to make the Object implement the Comparable interface and override the compareTo() method.

Detailed explanation-2: -Remember, though: if the object doesn’t implement Comparable, a ClassCastException will be thrown.

Detailed explanation-3: -Java Comparable interface is used to order the objects of the user-defined class. This interface is found in java. lang package and contains only one method named compareTo(Object). It provides a single sorting sequence only, i.e., you can sort the elements on the basis of single data member only.

Detailed explanation-4: -Comparator is used to sort an ArrayList of User-defined objects. In java, Comparator is provided in java. util package. Using Comparator we can sort ArrayList on the basis of multiple variables.

There is 1 question to complete.