Course: Easy-to-follow Java programming
The quiz questions
Answer the questions below to review what you have learned in Video 11. You will
find the right answers after the questions in the solution part.
1. Sorting elements means…
… that you reverse the original order of the elements.
… that you clear elements that can not satisfy a condition.
... that you reorder the elements based on a comparable feature of
the elements (e.g. size).
… that you create a random order in the elements.
… that something makes you feel pain (slang for sore thing)
2. Sorting algorithm decides…
what elements it has to compare
how the elements has to be compared
what elements it has to replace
3. What is NOT the name of a sorting algorithm?
Insertion sort
Discount sort
Bubble sort
Champagne sort
Dual-Pivot Quicksort
Duckademy IT courses – www.duckademy.com
4. How can you tell Java how to compare objects (e.g. Fruit objects)?
Java contains a built-in artifical intelligence module that can decide
how to compare different objects.
Java has only solution for sorting numbers. You have to create your
own version of the sorting algorithm in which you can sort objects.
Java specifies interfaces that can be implemented by the method of
comparing the objects.
You have to transform each object to -1, 0 or +1, and then you can
compare them.
5. What is the return value of compare() and compareTo() methods?
The methods return -1 if the ”left” object is smaller, 0 if equals, +1 if
”right” object is smaller
The methods return +1 if the ”left” object is smaller, 0 if equals, -1 if
”right” object is smaller
The methods return negative number if the ”left” object is smaller, 0 if
equals, positive number if ”right” object is smaller
The methods return true if the ”left” object is smaller and false
otherwise.
The methods return nothing – they reorder the elements in the array
or collection.
6. What is the correct definition of Comparable (based on what we learnt)?
public class Comparable<T> {
public int compareTo(T other);
}
public interface Comparable<T> {
public int compareTo(T other);
}
public interface Comparable<T> {
public void compareTo(T other);
}
public class Comparable<T> {
public int compareTo(T left, T right);
}
Duckademy IT courses – www.duckademy.com
public interface Comparable<T> {
public boolean compareTo(T left, T
right);
}
7. What can you do if you have to make a program that sorts books
based on length (page number) and price based on user selection?
I add the user selection to the compareTo() method of the Book
class.
I create two Comparator’s, each one for one of the sorting key.
I implement one of the sorting keys and pray that the user will not
recognize the defect
I completely rewrite the sorting algorithm based on what I need.
Duckademy IT courses – www.duckademy.com
----------------------------------------------------------------------------------------------------------------
The answers
1. Sorting elements means…
… that you reverse the original order of the elements.
… that you clear elements that can not satisfy a condition.
X ... that you reorder the elements based on a comparable feature
of the elements (e.g. size).
… that you create a random order in the elements.
… that something makes you feel pain (slang for sore thing)
2. Sorting algorithm decides…
X what elements it has to compare
how the elements has to be compared
X what elements it has to replace
3. What is NOT the name of a sorting algorithm?
Insertion sort
X Discount sort
Bubble sort
X Champagne sort
Dual-Pivot Quicksort
4. How can you tell Java how to compare objects (e.g. Fruit objects)?
Java contains a built-in artifical intelligence module that can decide
how to compare different objects.
Java has only solution for sorting numbers. You have to create your
own version of the sorting algorithm in which you can sort objects.
X Java specifies interfaces that can be implemented by the
method of comparing the objects.
Duckademy IT courses – www.duckademy.com
You have to transform each object to -1, 0 or +1, and then you can
compare them.
5. What is the return value of compare() and compareTo() methods?
The methods return -1 if the ”left” object is smaller, 0 if equals, +1 if
”right” object is smaller
The methods return +1 if the ”left” object is smaller, 0 if equals, -1 if
”right” object is smaller
X The methods return negative number if the ”left” object is smaller,
0 if equals, positive number if ”right” object is smaller
The methods return true if the ”left” object is smaller and false
otherwise.
The methods return nothing – they reorder the elements in the array
or collection.
6. What is the correct definition of Comparable (based on what we learnt)?
public class Comparable<T> {
public int compareTo(T other);
}
X public interface Comparable<T> {
public int compareTo(T other);
}
public interface Comparable<T> {
public void compareTo(T other);
}
public class Comparable<T> {
public int compareTo(T left, T right);
}
public interface Comparable<T> {
public boolean compareTo(T left, T
right);
}
Duckademy IT courses – www.duckademy.com
7. What can you do if you have to make a program that sorts books
based on length (page number) and price based on user selection?
I add the user selection to the compareTo() method of the Book
class.
X I create two Comparator’s, each one for one of the sorting key.
I implement one of the sorting keys and pray that the user will not
recognize the defect
I completely rewrite the sorting algorithm based on what I need.
Duckademy IT courses – www.duckademy.com