COMP 110 SP16 1
COMP 110 Final Exam Study Guide Questions are grouped into parts 113 for reference. PART 1. Use the code below to answer the first set of questions. public class Phone { private String _brand; private int _number; private int _power; private int _lastDialed; public Phone(String brand, int number) { _brand = brand; _number = number; _power = 0; _lastDialed = 0; } private int setNumber() { _number = number; } public void dial( int number, double mins) { _power = (( int ) mins / 5); if (_power 60) { System. out .println("Could be your best friend"); } else if (mins > > 2) A 3) Replace the if condition with (print.equals(“===”)) or (i == 2) 4) False (it is still a String)
COMP 110 SP16 8
PART 5. Use the following methods to answer the next set of questions. public int larger(int x, int y) { if (x q) && (r > p)) { if (q > p) { return true; } else { return false; } } return false; }
Determine if each of the following method calls cause an error. If not, what is the returned value? 1. larger(1, 2) 2. larger(4, 2.5) 3. larger(3, 3) 4. ascending(1, 2, 3) 5. ascending(0.1, 0.2, 0.3) 6. ascending(2, 1, 1) 7. ascending(3, 3, 3) ANSWER KEY 1. 2 2. Error 3. 3 4. True 5. Error 6. False 7. False PART 6. Use the code below to answer the next problem set. public static void abc(double x) { if (x > 0) { System.out.print(“A”); } if (x > 1) { System.out.print(“B”); } if (x > 2) { System.out.print(“C”); } if (x 0) { System.out.print(“D”); x; } }
public static void xyz(double y) { if (y > 0) { System.out.print(“A”); } else if (y > 1) { System.out.print(“B”); } else if (y > 2) { System.out.print(“C”); } else { System.out.print(“NO”); } while (y > 0) { System.out.print(“D”); y; } }
COMP 110 SP16 9
Determine if following method calls cause an error or not. If not, what is printed to the console? 8. abc(1.0) 9. abc(6.5) 10. abc(4) 11. abc(true) 12. xyz(2.2) 13. xyz(9.0) 14. xyz(12.0, 45.0) 15. xyz(3.0) ANSWER KEY 8. AD 9. ABCDDDDDDD 10. NO 11. Error 12. ADDD 13. ADDDDDDDDD 14. Error 15. NO PART 7. Use the code below to answer the next set of questions. ArrayList arr = new ArrayList (); for (int i = 0; i = _arr . length ) { return acc; } return this.fold ( start + 1 , max ( acc , _arr [ start ])); } private static int max ( int a , int b ) { if ( a > b ) { return a; } else { return b; } } } 11. What will be the value of the result variable after executing the following set of statements? int [] arr = { 7 , 2 , 3 , 1 , 3 , 0}; Test test = new Test ( arr ); int result = test . foo ( 3 ); a. 4 b. 5 c. 6 d. 7 e. 8 12. Which of the following expressions will return the sum of the elements in _arr? a. test.foo() b. test.foo(1) c. test.foo(0) d. test.foo(_arr.length) e. None of the above
COMP 110 SP16 14
13. What will be the value of the result variable after executing the following set of statements? int [] arr = { 1 , 2 , 3 , 8 , 1 , 0}; Test test = new Test ( arr ); int result = test . fold ( 2, 4 ); HINT: The fold method is recursive. You can use a table like this to keep track of the two values passed each time it calls itself. Watch out for the base case!
INCOMING PARAMETERS
ARGUMENTS PASSED
( start, acc )
( start + 1 , max ( acc , _arr [ start ] )
Initial call
2, 4 >
2 + 1
max(4, 3)
2nd call
3, 4 >
...
...
a. 1 b. 0 c. 1 d. 3 e. 8 14. What is returned by calling the bar method? a. Index of the minimum element b. Index of the maximum element c. The minimum element d. The maximum element e. None of the above ANSWER KEY: 11. A 12. C 13. E 14. D
COMP 110 SP16 15
PART 11. Refer to the ViewingList and Movie classes for the next set of questions. package comp110; import java.util.ArrayList; public class ViewingList { ArrayList<Movie> _list; public ViewingList() { _list = new ArrayList<Movie>(); } public void addMovie(Movie m) { _list.add(m); } public String toString() { String s = “”; for (int i = 0; i var) to (_list.get(i).getYear()