Question 11 (5 points) To declare an interface named A with two generic types, u
ID: 3673443 • Letter: Q
Question
Question 11 (5 points)
To declare an interface named A with two generic types, use
Question 11 options:
public interface A(E) { ... }
public interface A(E, F) { ... }
public interface A<E> { ... }
public interface A<E, F> { ... }
Question 12 (5 points)
Will the following code have a runtime error?
Comparable date = new Date();
int i = date.compareTo("time");
Question 12 options:
Always
Only when date is not the current date
Only when date contains an invalid date
Never
Question 13 (5 points)
To create a list to store integers, use
Question 13 options:
ArrayList<int> list = new ArrayList();
ArrayList<Object> list = new ArrayList<Integer>();
ArrayList<Integer> list = new ArrayList();
ArrayList<Number> list = new ArrayList<Integer>();
Question 14 (5 points)
To create a generic type bounded by Number, use
Question 14 options:
<E>
<E extends Object>
<E extends Integer>
<E extends Number>
Question 15 (5 points)
Which of the following describes the benefit of using generic classes to implement collections?
Question 15 options:
It eliminates the need to downcast objects when they are inserted into a collection
It eliminates the need to upcast objects when they are inserted into a collection
It eliminates the need to downcast objects when they are removed from a collection
It eliminates the need to upcast objects when they are removed from a collection
Question 16 (5 points)
Suppose list list1 is [1, 2, 5] and list list2 is [2, 3, 6]. After list1.addAll(list2), list2 is __________.
Question 16 options:
[1, 5]
[1, 2, 2, 3, 5, 6]
[2, 3, 6]
[1, 2, 3, 5, 6]
Question 17 (5 points)
Suppose a list contains {red, red, red, red}. What is the list after the following code?
String element = "red";
for (int i = list.size() - 1; i >= 0; i--)
if (list.get(i).equals(element))
list.remove(element);
Question 17 options:
[]
[red, red, red]
[red]
[red, red]
Question 18 (5 points)
Which of the following is correct to sort the elements in a list aList?
Question 18 options:
new LinkedList<String>(new String[]{red, green, blue})
aList.sort()
Collections.sort(aList)
Arrays.sort(aList)
Question 19 (5 points)
Which of the following best describes all objects of type List?
Question 19 options:
They define an unordered collection that allows duplicates
They define an ordered collection that prohibits duplicates
They define an unordered collection that prohibits duplicates
They define an ordered collection that allows duplicates
Question 20 (5 points)
Which of the following problems would be a good candidate for using a stack?
Question 20 options:
A print spooler that dispatches jobs based on shortest job first
A task scheduler that schedules tasks in the order that they are received
A program that is designed to evaluate expressions
An inventory system that processes product records by product number
public interface A(E) { ... }
public interface A(E, F) { ... }
public interface A<E> { ... }
public interface A<E, F> { ... }
Explanation / Answer
Answers:
Answer – 11:
The generic types are defined inside the angular brackets.
So, if a single generic type is used to define a class or an interface, the syntax can be used is given as follows,
public interface/class ClassName<E>{}
Similarly, if two or more generic types are need to be defined for an interface or for a class, then the declaration is given by number of generics are being used and they are separated by comma(,).
The syntax is given as,
public interface/class ClassName<E, F>{}
Therefore, the answer is public interface A<E, F>.
Answer – 12:
Yes, always there will be a runtime error.
Therefore, it always raises the runtime error.
Answer – 13:
Therefore the syntax is given as,
ArrayList<Integer> list = new ArrayList();
Answer - 14:
If the generic type is bounded by a Number class then the syntax for it is given as,
<E extends Number>
Answer – 15:
The usage of the generic classes over the collections is to easily define the object to be hold in the collection container class.
Therefore, the answer is,
It eliminates the need to downcast objects when they are removed from a collection.
Answer – 16:
Therefore after the call of the method, elements in the list2 will be same that is [2, 3, 6].
Answer – 17:
Therefore, after the code execution, list becomes empty([]).
Answer – 18:
A list is a collection of same type of objects. List comes under the Collections class. In a Collections class, the sort() method is available. Only thing need to do is pass the list object.
Therefore, the answer is Collections.sort(aList).
Answer – 19:
A list itself is a collection of unordered collection that allows duplicates.
Answer – 20:
An expression is needs to be evaluated in the order the data is entered into the stack.
The expression is evaluated from last in first out bases.
Therefore, a program that is designed to evaluate expressions is a good candidate for using a stack.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.