0% found this document useful (0 votes)
369 views10 pages

Prog 2 Final Exam Reviewer

This document provides a 30 item review for a programming 2 final exam. The review covers topics like: - Implementing methods to initialize arrays and matrices - Using recursion to calculate factorials - Applying search algorithms like linear search, binary search, and balloon sort - Overriding methods like equals in classes - Characteristics of lists, maps, and sets in the Java Collections Framework - Using lambda expressions The review contains multiple choice and code completion questions testing understanding of arrays, classes, interfaces, inheritance, exceptions, generics, collections, algorithms, and lambda expressions.

Uploaded by

Rovic Dictag
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
369 views10 pages

Prog 2 Final Exam Reviewer

This document provides a 30 item review for a programming 2 final exam. The review covers topics like: - Implementing methods to initialize arrays and matrices - Using recursion to calculate factorials - Applying search algorithms like linear search, binary search, and balloon sort - Overriding methods like equals in classes - Characteristics of lists, maps, and sets in the Java Collections Framework - Using lambda expressions The review contains multiple choice and code completion questions testing understanding of arrays, classes, interfaces, inheritance, exceptions, generics, collections, algorithms, and lambda expressions.

Uploaded by

Rovic Dictag
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

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?

Answer: for (int column = 0; column < array[row].length; column++)

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:

Answer: row < 0 && col > size - 1


3. 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(5) will return an array
of the following form:

Answer: row > (size - 1) && col > size - 1

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.

Answer: result = n * factorial(n - 1)

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)

7. Complete following method that


implements the binary search
algorithm in determining whether the
value of key is an element of the array
grades or not. The array grades are
assumed to be sorted in ascending
order.

Answer: grades[mid] < key


8. Complete the following method that implements the Balloon Sort Algorithm in arranging
the elements of an array of objects of a Student class such that the string
representations of the objects of Student follow lexicographic ordering. The method
creates a copy of a given array of objects of Student, the method arranges the elements
of the array by using the Balloon Sort Algorithm and the method returns the sorted array.
Assume that the copyArray method is accessible by the sortArray method and that the
usage of the copyArray method in the sortArray method is correct. In addition, assume
that the Student class has a default constructor

Answer: int x = 0; x < sorted.length - 1; x++

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

Answer: toString().equals(((Employee) another).toString())


9. Which of the following cannot be used as constructors:
- T reference = new T()
- T[] arr = (T[]) new Object[5];
- T[] array = new T[10];

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.

11. Which of the following is/are not characteristic/s of a LIST?


- Duplicate values are not allowed
- Doesn’t contains any legacy class

12. Which of the following is/are not characteristic/s of a MAP?


- Duplicate elements are allowed
- Stores elements based on Array Data Structure

13. The following interfaces are part of the Java Collections Framework except:
- SortedList

14. Which method can randomly generate all elements in a list?


- Shuffle()

15. Which of the following is NOT true about functional interfaces in Java?
- It has only a single method that needs to be implemented.

16. How can we write a parameter less than a Lambda expression?


- Pass empty set of parentheses on the left side of the arrow

17. Which of these interfaces must have a unique element?


- Set
18. Which of the following methods for obtaining the maximum element is incorrect?
- max(Comparator Comp)

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);

21. Which assertion about the HashMap class is not true?


- HashMap maintain order of its element.

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

23. Which of the following statement is True about Lambda:


I. Lambda is denoted with => sign
II. It is neither a function nor an interface
III. Lambda expression enable functions to be passed as argument
IV. A return keyword is always optional in a lambda expression
Answer: III (Lambda expression enable functions to be passed as argument.

24. You can use a Lambda expression as follows:


I. As a method argument
II. As a conditional expression in an if statement
III. In a return statement
IV. In a throw statement
Answer: I and III (As a method argument), (In a return statement)

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,

Which of the following is a valid statement?


I. ThisIsInterface varA = varA -> varA.length();
II. ThisIsInterface varX = varY -> {return varY;};
III. ThisIsInterface varS = “2” -> Integer.parseInt(varS);
IV. ThisIsInterface varB = (String varS) -> 1;
Answer: IV (ThisIsInterface varB = (String varS) -> 1;)

27. This generates an error: public class GEx extends Exception


- True

28. public class RClass is an accepted line of code.


- False

29. public class Two<T1 extends Class1, T2 extends Class2 & Comparable> is an accepted
line of code
- True

30. Is it possible to traverse the elements of a Set without using an Iterator?


- True

31. The functionality of functional interfaces is limited to one.


- True

32. Provide the line of code for ITEM #1

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);

37. What must be placed on the blanks to have a working program?

Answer:
String, integer
Provide the missing code for
item number #1
Answer: FinalGUI

Provide the missing code for


item number #2
Answer:

Provide the missing code for


item number #3
Answer: true

Provide the missing code for


item number #4
Answer: action1

Provide the missing code for


item number #5
Answer:ActionListener

Provide the missing code for


item number #6
Answer: false

Provide the missing code for


item number #7
Answer: true

Provide the missing code for


item number #8
Answer: false

Provide the missing code for


item number #9
Answer:
startButton.setEnabled(true);

Provide the missing code for


item number #10
Answer: label

You might also like