Prog 2 Final Exam Reviewer
Prog 2 Final Exam Reviewer
1. What is the missing statement in the following method so that the method initializes each
element of a given two dimensional array of integers to zero?
2. Consider the following incomplete code for generateMatrix method. Assume that the
initializeArray method that is invoked by generateMatrix assigns 0 to every cell of the
array that is passed to it. Suppose the expression generateMatrix(3); will return an array
of the following form:
4. Which expression should complete the code for the compareTo method of the following
Employee class so that objects of the Employee class in an ArrayList are arranged
based on alphabetical ordering of the values of the name attributes if the sort method of
the Collections class is used to sort the
ArrayList
Answer:
this.name.compareTo(another.getName()
5. Complete the following recursive method for computing the factorial of an integer.
Assume that n is greater than or equal to 0.
6. Assume that a typical Reference Class called Student is accessible by the following
method called linearSearch. Notice that the code for linearSearch is not complete.
Assume further that the Student class has an attribute called lastName and that the
class has a getter method called getLastName() . Which statements may be used to
complete the definition of the linearSearch method so that the method uses the linear
search algorithm and that the method returns true if there is a student in an array of
Student with a lastName that matches the searchKey.
Answer: studs[index].getLastName().equals(searchKey)
9. Complete the equals method in the following class definition of Employee. The equals
1/3 method overrides the equals method that is inherited by Employee from the Object
class. The equals method returns true if an object of the Employee class has the same
String representation as another Employee object
10. Match the advantages of the Java Collection Framework to its respective description.
● Performance -> The collection framework creates extremely effective and
efficient data structures, which improve a program's speed and accuracy.
● Reusability -> Collection Framework classes can easily be mixed with other
types, resulting in increased code reusability
● Maintainability -> The collection framework's code is simple to maintain since it
ensures data consistency and interoperability throughout the implementation.
● Extensibility -> Java's Collection Framework allows programmers to customize
primitive collection types to meet their needs.
● Reverse -> Reverses the elements in a List's order.
● Fill -> Overwrites the provided value on every entry in a List. This method can be
used to reinitialize a List.
● Copy -> Accepts two parameters, a destination List and a source List, and
overwrites the contents of the destination with the elements of the source.
● Swap -> Swaps the elements in a List at the specified locations.
● addAll -> Add all the specified elements to a Collection.
13. The following interfaces are part of the Java Collections Framework except:
- SortedList
15. Which of the following is NOT true about functional interfaces in Java?
- It has only a single method that needs to be implemented.
19. Which of the following statements about the ArrayList class is/are false?
I. ArrayList class extends AbstractList class and implements the List interface
II. ArrayList supports dynamic array that can grow as needed
III. ArrayLists are synchronized
IV. It can contain Duplicate elements and maintains the insertion order
Answer: III (ArrayLists are synchronized)
20. Which of the following java statements remove duplicates from a List?
- HashSet list = new HashSet(duplicateList);
22. I. Lambda expressions are used primarily to define inline implementation of a functional
interface
II. Lambda expression eliminates the need of anonymous class and gives a very simple
yet powerful functional programming capability to Java
- I and II are TRUE
25. Which of the following lambda expressions is/are correct for multiplying two numbers
and returning their product?
- All of the Above
26. Given the following code fragment,
29. public class Two<T1 extends Class1, T2 extends Class2 & Comparable> is an accepted
line of code
- True
Answer:
ArrayList list_Strings =
new ArrayList();
33. Provide the missing code to complete the line of code for ITEM #2
Answer:
contains
34. What must be placed in the blanks under ITEM #1 to correctly perform what is asked.
Answer:
1,3
35. What is the output of the list after swap?
Answer:
Mark Go,
Rafael Wasan,
Edgardo Duque,
Nicasio Aliping Jr., Reynaldo
Diaz Jr.,
Alexis Abano
36. What will be the output after running the program (assuming that the error was removed)
Answer:
list.add(99);
Answer:
String, integer
Provide the missing code for
item number #1
Answer: FinalGUI