CMPS 200 Final, Fall 2004

American University of Beirut · Introduction to Programming · 23 pages

All CMPS 200 files
CMPS 200 Final, Fall 2004, page 1

Stuck on a question? A tutor can walk you through it.

CMPS 200 Final, Fall 2004, page 2 CMPS 200 Final, Fall 2004, page 3 CMPS 200 Final, Fall 2004, page 4 CMPS 200 Final, Fall 2004, page 5 CMPS 200 Final, Fall 2004, page 6 CMPS 200 Final, Fall 2004, page 7 CMPS 200 Final, Fall 2004, page 8 CMPS 200 Final, Fall 2004, page 9 CMPS 200 Final, Fall 2004, page 10

Showing the first 10 of 23 pages. Download the PDF to see all of it.

More CMPS 200 files

Show the text of this final exam
Faculty of Arts & Sciences Department of Computer Science CMPS 200—Introduction to Computer Programming Fall 2004-2005 Thursday, January 27, 2005 GatehiGay UN{VERSE: joe ee, i LIBRARY . MERION UN ener | av BERET Final Exam { Lt hi ran ev = ' OP ey Name: Student Id: Sw a ne Signature: Section: Duration: 120 minutes General Instructions e There are 2 parts and 22 pages. Make sure that you have all of them. ¢ The exam is closed book, closed notes, and closed neighbor. e Please note that wrong answers to multiple choice questions carry a penalty: four wrong answers cancel one correct answer. Do not write anything besides your name on the pages with multiple choice questions. Write your answers on the separate answer sheet provided with Part II of the exam handout. Your handwriting should be readable so it can be graded. Final Exam (2005-01-27) Part | Answer the following questions by circling the appropriate answer on the answer sheet. Qo9 8.5 (d) Consider the code below. Note that the catch statements in the code are not implemented, but you will not need those details. Assume filename is a String, x is an int, a is a double array and i is an int. Use the comments il, i2, i3, e1, e2, e3, e4, eS to answer the questions (i for instruction, e for exception handler). try { BufferedReader infile = new BufferedReader (new FileReader (filename) ) ; int x = Integer.parseInt (infile.readLine( )); a[++i] = (double) (1 / x); } catch (FileNotFoundException ex) {...} // catch (NumberFormatException ex) {...} // catch (ArithmeticException ex) {...} // catch (ArrayIndexOutOfBounds ex) {...} // catch (IOException ex) {...} // An exception raised by the instruction in i3 would be caught by the catch statement labeled e2 e3 e4 either e3 or e4 either €2, ¢3 or ¢4 Which of the following is a listener class fora Slider? 9 a) ListListener b) ActionListener c) ButtonListener d) ChangeListener e) SlideListener Assume that Student, Employee, and Retired are all extended classes of Person, and all four classes have different implementations of the method getMoney. Consider the following code where ... are the required parameters for the constructors: Person p = new Person(...); int ml = p.getMoney( ); // assignment 1 p = new Student(...); int m2 = p.getMoney( }); // assignment 2 if (m2 < 100000) p = new Employee(...); else if (ml > 50000) p = new Retired(...); int m3 = p.getMoney( ); // assignment 3 The reference to getMoney () in assignment 3 is to the class a) Person b) Student c) Employee d) Retired e) None of the above, this cannot be determined by examining the code CMPS 200 Final Exam (2005-01-27) Initials: Which of the following messages passed to the String str could throw a StringIndexOutOfBoundsException? a) str.length( ) b) str.charAt (2); ce) replace('a', 'A'); d) str.equals(str); e) Any of the above could throw a StringIndexOutOfBoundsException. Consider the following class definition: public class SomeClass { private int x; public SomeClass (int newValue) { ) x = newValue; } If q1 and q2 are objects of SomeClass, then q1.equals (q2) a) is a syntax error since equals is not defined in the SomeClass class b) is true if q1 and q2 both store the same value of x c) is true if q1 and q2 reference the same SomeClass object d) is never true e) throws aNullPointerException Which of the following is not a method of the Object class? a) clone b) compareTo c) equals d) toString e) All of the above are methods of the Object class. Consider the following class definition: public class AClass { protected int x; protected int y; public AClass(int a, int b) { x = a; y= b; } public int addEm( ) { } public void changeEm( ) { return x + y; X++; CMPS 200 Final Exam (2005-01-27) Yuu } public String toString( ) { } return "" +x + " "+ yj } Which of the following would best redefine the toString method for BClass, a subclass of AClass? a) public String toString(int z) { } public String toString( ) { } public String toString( ) { } public String toString( ) { return " "+x+" "+ y+ it return super.toString( ); return super.toString( ) +" return super.toString( ) +" "ox a Heyer "4 2; } public String toString( ) { return " "+x + "+y+" } 8. Consider the following partial class definitions: public class Al { public int x; private int y; protected int 2; } public class A2 extends Al { protected int a; private int b; } public class A3 extends A2 { private int q; } Which of the following is true with respect to Al, A2 and A3? a) Al isa subclass of A2 and A2 is a subclass of A3 CMPS 200 Final Exam (2005-01-27) Initials: A3 is a subclass of A2 and A2 is a subclass of Al Al and A2 are both subclasses of A3 A2 and A3 are both subclasses of Al Al, A2 and A3 are all subclasses of the class A In order to allow the user to activate a JButton using the keyboard, you would add a to the JButton. a) shortcut b) KeyListener c) mnemonic d) ActionListener e) RadioButton . Java does not support multiple inheritance, but some of the abilities of multiple inheritance are available by a) importing classes b) implementing interfaces c) overriding parent class methods d) creating aliases e) using public rather than protected or private modifiers . Assume that int[ ] a =. {6, 2, 4, 6, 2, 1, 6, 2, 5} and consider the two recursive methods below foo and bar. public int foo(int[ ] a, int b, int j) { if (j < a.length) if (a(j) != b) return foo (a, b, else return foo (a, b, j+l) + 1; else return 0; } public int bar(int[ ] a, int j) { if (j < a.length) return a[I] + bar(a, j+1); else return 0; } What is the result of calling foo(a, 2, 0);? a) 0 b) c) qd) e) 12. Consider the following recursive method. public int questionA(int x, int y) { if (x == y) return 0; else return questionA(x-1, y) + 1; } If the method is called as questionA(8, 3), what is returned? a) 11 CMPS 200 Final Exam (2005-01-27) b) 8 ec) 5 d) 3 e) 24 13. Consider the following recursive factorial method: public int factorial (int x) { if (x > 1) return x * factorial (x - 1); else return 1; } How many times is the factorial method invoked if originally called with factorial(5)? Include the original method call in your counting. a) 1 b) 4 ce) 5 d) 6 e) 7 14. Which of the following methods would properly compute the value of x % y (x mod y) recursively, assuming that x and y not negative numbers? a) public int recursiveMod(int x, int y) { if (x < y) return y; else return recursiveMod(x, y - } public int recursiveMod(int x, int y) if (x == y) return 0; else return recursiveMod(x - y, } public int recursiveMod(int x, int y) { if (x < y) return x; else return recursiveMod(x - y, } public int recursiveMod(int x, int y) { if (x < y) return x; else return recursiveMod(x - y, } public int recursiveMod(int x, int y) { while (x > y) x= xX ~ Yi return x; 15. Assume JButton jb1 has already been instantiated. To add a pop-up GUI box that will display “Closes the GUI” whenever the mouse is moved to the button and left there, you would do a) jbl.add("Closes the GUI"); b) jb1.setCaption("Closes the GUI"); CMPS 200 Final Exam (2005-01-27) Initials: c) jbil.addPopUpMessage("Closes the GUI"); d) jbl.setMnemonic("Closes the GUI"); e) jbl.setToolTipText ("Closes the GUI"); 16 | Q16 16. The following code would be used to create which of the JButtons below? 9.24 JButton jb = new JButton ("Open"); . jb.setMnemonic ('0'); (a) jb.setToolTipText ("Opens a new Window"); a) c) e) 17] Qil 17. Consider the following skeletal code. 812 public static void main(String[ ] args) : { (e) try { Except ionThrowerCode etc = new Except ionThrowerCode () ; etc.ml( ); etc.m2( ); } catch (ArithmeticException ae) { ... } } public class ExceptionThrowerCode { public void m1( } { } public void m2( ) { try { m3( ); } catch (ArithmeticException ae) {...} catch(NullPointerException npe) {...} } public void m3( }) { CMPS 200 7 of 22 Final Exam (2005-01-27) Initials: try { } catch (ArithmeticException ae) {... } } Ifa NullPointerException arises in the try statement in m1, a) itis caught in main b) it is caught in ml c) itis caught in m2 d) itis caught in m3 e) it is not caught leading to the program terminating . The following method should return true if the int parameter is even and either positive or 0, and false otherwise. Which set of code should you use to replace ... so that the method works appropriately? public boolean question3(int x) { ... } a) if (x ==0) retum true; else if (x < 0) return false; else return question3(x — 1); if (x = = 0) return false; else if (x < 0) return true; else return question3(x — 1); if (x = = 0) return true; else if (x <0) return false; else return question3(x — 2); if (x == 0) return false; else if (x < 0) return true; else return question3(x — 2); return(x = = 0); . Consider the following recursive method. public int questionl_2(int x, int y) { if (x == y) return 0; else return questioni_2(x-1, y) + 1; } Calling this method will result in infinite recursion if which condition below is initially true? a) (x==y) b) &IFy) c) (x>y) d) (x<y) e) ==0&& y!=0) . Consider the code below. Note that the catch statements in the code are not implemented, but you will not need those details. Assume filename is a String, x is an int, a is a double array and i is an int. Use the comments i], i2, i3, el, e2, e3, e4, e5 to answer the questions (i for instruction, e for exception handler). try { BufferedReader infile = new BufferedReader(new FileReader (filename) ) ; // il int x = Integer.parseInt (infile.readLine( )); // i2 al++i] = (double) (1 / x); // i3 } catch (FileNotFoundException ex) {... catch (NumberFormatException ex) {. oe catch (ArithmeticException ex) {... catch (ArrayIndexOutOfBounds ex) {... CMPS 200 Final Exam (2005-01-27) Initials: catch (IOException ex) {...} // eS An exception raised by the instruction in il would be caught by the catch statement labeled a) el b) e2 c) ed d) either el or e5 e) either el, e4 or eS . Assume that al and a2 are arrays of lengthn. W hich ofthe following pieces of code correctly determines the equality of all elements in the arrays: 1. boolean result = true; for (int i = 0; i < al.length; i++) result = result && (al[i] == a2[i]); boolean result = true; for (int i = 0; i < al.length && result; i++) result = result && (al[i] == a2[i]); boolean result = true; for (int i = 0; i < al.length; i++) result = al[i] == a2[i] boolean result = true; for (int i = 0; i < al.length; if (alfi] == a2[i]) result = true; else result = false; boolean result = true; for (int i = 0; i «< al.length; i£ (al[il != a2[i]) result = false; 1 2 3&4 1,2,5 All of the above. 22. Why is the following method one which has infinite recursion? public int infiniteRecursion(int n) { if (n > 0) return infiniteRecursion(n) + 1; else return 0; } Because there is no base case Because the base case will never be true Because the recursive call does not move the parameter closer to the base case Because the recursive call moves the problem further away from the base case None of the above, there is no infinite recursion in this method Q05 23. A variable declared to be of one class can later reference an extended class of that class. This variable is known as 7.19 a) protected (d) CMPS 200