In: Computer Science
Question 17 (1 point)
What is the return type of this method?
public <E extends Comparable<E>> int theMethod(E arg)
Question 17 options:
E |
|
Comparable |
|
int |
|
arg |
Question 18 (1 point)
What is the primary benefit of writing generic classes and methods?
Question 18 options:
Generic classes and methods are more type-safe |
|
Generic classes and methods are shorter |
|
Generic classes and methods are faster |
|
Generic classes and methods are backwards compatible |
Question 19 (1 point)
What is wrong with the following method?
public static E copy(E arg) {
E theCopy = arg.clone();
return theCopy;
}
Question 19 options:
The method cannot be static if it uses generics |
|
The method calls clone on the argument, but that method is not guaranteed to exist for the argument |
|
You cannot declare a variable (e.g. theCopy) with a generic type |
|
The returned value does not match the method signature |
Question 20 (1 point)
Which of these data structures is the best choice in the following situation? You are writing a class scheduling application, and you need to keep track of the wait list for overbooked classes. If a student tries to register for a course that is full, she should be placed at the end of the wait list. If a student drops the course, the student currently at the beginning of the wait list should be moved into the class.
Question 20 options:
list |
|
stack |
|
queue |
|
priority queue |
Question 21 (1 point)
Which of these data structures is the best choice in the following situation? You are writing a program to play a card game, and you need to keep track of the discard pile. When a player discards a card, it is placed at the top of the discard pile. If a player wants to pick up a card from the discard pile, she must take the card that is currently on the top.
Question 21 options:
list |
|
stack |
|
queue |
|
priority queue |
Question 22 (1 point)
Which of these data structures is the best choice in the following situation? You are writing a program to help the human resource department of a company in scheduling interviews. As applications come in, the hiring manager gives them a score from 0 to 100 according to how well the applicant appears to fit the needs of the position. After a period of two weeks, interviews need to be scheduled. You need a data structure to tell the HR employee which applicant should next be scheduled for an interview. The choice is based on the applicant with the highest score.
Question 22 options:
list |
|
stack |
|
queue |
|
priority queue |
Question 23 (1 point)
Which of these data structures is the best choice in the following situation? You are writing a program to track various statistics for the players on the active roster of a baseball team. The order in which the players are stored is not important, but you will frequently need to iterate over all of the players to compute aggregate statistics for the team.
Question 23 options:
list |
|
stack |
|
queue |
|
priority queue |
Question 24 (1 point)
What is the output of the following code?
public class Widget implements Cloneable {
public int count;
public FiddlyBit bit;
public Widget() {
bit = new FiddlyBit();
}
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
public String toString() {
return "count = " + count + "; bit part number = " + bit.partNo;
}
class FiddlyBit {
int partNumber;
}
public static void main(String[] args) {
Widget widgetA = new Widget();
widgetA.count = 5;
widgetA.bit.partNo = 111;
Widget widgetB = (Widget) widgetA.clone();
widgetB.count = 6;
widgetB.bit.partNo = 222;
System.out.println("widgetA: " + widgetA);
System.out.println("widgetB: " + widgetB);
}
}
Question 24 options:
widgetA: count = 5; bit part number = 111; |
|
widgetA: count = 6; bit part number = 222; |
|
widgetA: count = 6; bit part number = 111; |
|
widgetA: count = 5; bit part number = 222; |
Question 25 (1 point)
Which of the following is valid, assuming myPetStore is an instance of PetStore? Choose all that apply.
public interface Animal { public abstract void eat(); } public class Pet implements Animal { public void eat() { System.out.println("Eating pet food"); } } public class Dog extends Pet { } public class PetStore { public void feedingTime(Animal a) { a.eat(); } }
Question 25 options:
Animal anAnimal = new Animal();myPetStore(anAnimal); |
|
Pet aPet = new Pet();myPetStore(aPet); |
|
Dog aDog = new Dog();myPetStore(aDog); |
|
None of these are valid |
Previous PageNext Page
Hi,
Please find the below answers according to problem statement.
Q17. Comparable
Because <E extends Comparable<E>> is a upper bounded wildcard which represents wildcard character (‘E’), followed by the extends keyword, followed by its upper bound.
Q18. Generic classes and methods are more type-safe
The basic purpose of generics in java is to achieve type safety and to avoid type casting.
Q19. The method calls clone on the argument, but that method is not guaranteed to exist for the argument.
Q20. Queue data structure will be suite in this particular situtation. becuase queue uses first in first out.
Q21. Stack data structure will suits in this particular situation. becuase Stack uses last in first out.
Q22. In this scenario Priority Queue data structure will be helpful. because In a priority queue, elements can be inserted in any order but removal of the elements is in a sorted order.
Q23.List data structure can be used here because in list insertion of elements can be done in any order.
Q24.
widgetA: count = 5; bit part number = 222;
widgetB: count = 6; bit part number = 222;
Q25. Pet aPet = new Pet();myPetStore(aPet);
Dog aDog = new Dog();myPetStore(aDog);
These two are the valid ones. becuase Pet is sub class of Animal and Dog is a subclass of Pet. So both can use Petstore.
I hope this helps you!!
Thanks!!