exam2_Fall2005 - COMP 1210 Fall 2005 Exam 2 – 11/03/2005...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: COMP 1210 Fall 2005 Exam 2 – 11/03/2005 – Page 1 of 10 Name:__________________________________________________________________ Lab: (circle one) 12MW 2MW 4MW Code: ___________________ 5:30MW 7:00MW Multiple Choice 70 points (35 items @ 2 point each) Select the letter in front of the most correct answer, and mark your answer scan sheet accordingly. Be sure and consider each response. For questions 1-10, write the word "nothing" if no output is created. Assume the following variables have been declared: int n1, n2; double d1, d2; 1. What is the exact output of the following code segment? n1 = 3; n2 = 8; if (n1 <= n2) System.out.print(n1 + " "); System.out.print(n2 + " "); System.out.println ("end"); a) 3 8 end b) 8 end c) 3 8 end d) 8 end e) nothing 2. What is the exact output of the following code segment? d1 = 13.4; d2 = 9.801; if (d1 == d2) System.out.print ("equal "); else if (d1 > d2) System.out.print ("greater "); else { System.out.print ("less "); System.out.print ("end"); } a) end b) greater c) greater end 3. e) nothing What is the exact output of the following code segment? n1 = 16; d1 = 3.2; n2 = 0; while (d1 < n1) { n2 = n2 + 1; d1 = d1 + 5; } System.out.println (n2 + " " + d1); a) 0 3.2 4. d) greater less end b) 1 13.2 c) 2 18.2 d) 3 18.2 e) nothing What is the exact output of the following code segment? if ( 23 <= 5) if (4 >= 2) System.out.print ("middle "); else System.out.print ("not "); System.out.print ("end"); a) end b) not end c) middle end d) middle not end e) nothing COMP 5. 1210 8. b) sad c) sad end d) happy end d) nothing What is the exact output of the following code segment? if ( 13 <= 5) if (4 <= 2) System.out.print ("red "); else { System.out.print ("blue "); System.out.print ("end"); } a) red 7. Exam 2 – 11/03/2005 – Page 2 of 10 What is the exact output of the following code segment? if ( 3 <= 15) if (4 >= 2) System.out.print ("happy "); else System.out.print ("sad "); System.out.println ("end"); a) happy 6. Fall 2005 b) blue end c) red end d) red blue end What is the exact output of the following code segment? if ( 13 <= 5) { if (4 <= 2) System.out.print ("red "); else System.out.print ("blue "); System.out.print ("end"); } a) end b) blue end c) red end e) nothing d) red blue end e) nothing What is the exact output of the following code segment? n1 = 3; while (n1 < 8) { if (n1 % 2 == 0) System.out.print (n1 + “ “); n1 = n1 + 3; if (n1 % 3 == 0) System.out.print (n1 + “ “); n2 = n1; while (n2 < 6) { System.out.print (n2); n2 = n2 + 1; } } a) 3 6 9 9. b) 3 3 9 c) 6 6 9 d) 3 4 5 e) nothing What is the exact output of the following code segment? n2 = 0; for (n1 = 24; n1 <= 30; n1 = n1 + 2) n2 = n2 + n1; System.out.println (n2); a) 78 b) 108 c) 24 50 78 d) 24 50 78 108 e) nothing COMP 1210 Fall 2005 Exam 2 – 11/03/2005 – Page 3 of 10 10. What is the exact output of the following code segment? char ch; String s1 = new String("this is it"); n1 = 0; n2 = 0; do { if (s1.charAt(n1) == 'i') n2 = n2 + 2; else n2++; n1 = n1 + 1; } while (n1 < s1.length()); System.out.println (n2); a) 6 b) 10 c) 11 d) 13 e) nothing 11. What is the exact output of the following code segment? Ϩ¹íÏint num = 6; Ϩ¹¹±while (num < 16) { ϧÏÏ7¹¹´switch (num % 4) { ϧÏÏ5ÏÏ÷¹¹´case 0: System.out.print ("multiple "); ϧÏÏ5¹½ÏϾ¹¹Ïbreak; ϧÏÏ5ÏÏ÷¹¹´case 1: System.out.print ("one "); ϧÏÏ5¹½ÏϾ¹¹Ïbreak; ϧÏÏ5ÏÏ÷¹¹´case 2: System.out.print ("two "); ϧÏÏ5ÏÏ÷¹¹Ãcase 3: System.out.print ("three "); ϧÏÏ5¹½ÏϾ¹¹Ïbreak; ϧÏÏ5Ï϶} ϧÏÏ7¹¹Ïnum += 3; ϧÏÏ°} a) one multiple one two three b) one multiple one three two c) two three one multiple three d) two one multiple three e) none of the above 12. Which of the following is a correct interface? Code 1: interface A { void print() { }; } a) Code 1 b) Code 2 c) Code 3 d) Code 4 e) none of the above Code 2: abstract interface A { print(); } Code 3: abstract interface A { abstract void print() { }; } Code 4: interface A { void print(); } 13. Given the following code segment: where A is an interface, B is a concrete class with a default constructor that implements A. ÏØÓìinterface A{ Ïϧ ÏÏ©} ÏÕÖ×class B implements A{ Ïϧ ÏÏ©} Which of the following is correct? a) A a = new A(); b) A a = new B(); c) B b = new A(); d) B b = new B(); e) b and d COMP 1210 Fall 2005 14. What is the representation of the third element in an array called a? a) a[2] b) a(2) c) a[3] d) a(3) Exam 2 – 11/03/2005 – Page 4 of 10 e) none of the above 15. Analyze the following code. public class Test { public static void main(String args) { int x = new int[3]; System.out.println("x[0] is " + x[0]); } } a) The program has a compiler error because the size of the array wasn't specified when declaring the array. b) The program has a runtime error because the array elements are not initialized. c) The program runs fine and displays x[0] is 0. d) The program has a runtime error because the array element x[0] is not defined. e) none of the above 16. Which of the following statements is valid? a) int i = new int(30); b) int i = {3, 4, 3, 2}; c) double d[30] = new double; d) char c = new char(); e) char c = new char[4]{'a', 'b', 'c', 'd'}; 17. What is the output of the following code? public class Test17 { public static void main(String args) { int x = {1, 2, 3, 4}; int y = x; x = new int[2]; for (int i = 0; i < y.length; i++) System.out.print(y[i] + " "); } } a) 1 2 3 4 b) 0 0 c) 0 0 3 4 d) 0 0 0 0 e) none of the above 18. Assume double x = new double[4][5], what are x.length and x[2].length? a) 4 and 4 b) 4 and 5 c) 5 and 4 d) 5 and 5 e) none of the above 19. What is wrong in the following code? class TempClass { int i; public void TempClass(int j) { int i = j; } } public class C { public static void main(String args) { TempClass temp = new TempClass(2); } } a) The program has a compilation error because TempClass does not have a default constructor. b) The program has a compilation error because TempClass does not have a constructor with an int argument. c) The program compiles fine, but it does not run because class C is public. d) a and b. e) None of the above. COMP 1210 Fall 2005 Exam 2 – 11/03/2005 – Page 5 of 10 20. Variables that are shared by every instances of a class are ________________ variables. a) public b) private c) protected d) static e) instance 21. To declare a constant MAX_LENGTH as a static variable with value 99.98, you write a) final static MAX_LENGTH = 99.98; b) final static double MAX_LENGTH = 99.98; c) static double MAX_LENGTH = 99.98; d) final double MAX_LENGTH = 99.98; e) final double static MAX_LENGTH = 99.98; 22. To set a FlowLayout in panel jp, you can use the method __________. a) jp.setLayout(new FlowLayout(FlowLayout.center)); b) jp.setLayout(new FlowLayout(FlowLayout.CENTER)); c) jp.setLayout(new FlowLayout()); d) jp.setLayout(FlowLayout()); e) a or b 23. What is the output of the following code? public class Test23 { public static void main(String args) { int n = 2; xMethod(n); System.out.println("n is " + n); } void xMethod(int n) { n++; } } a) The code has a compiler error because xMethod does not return a value. b) The code has a compiler error because xMethod is not declared static. c) The code prints n is 1. d) The code prints n is 2. e) The code prints n is 3. 24. What does the first System.out.println in the main method print? ÏÕÖ×public class Foo { ÏϧÏíÏstatic int i = 0; ÏϧÏíÏstatic int j = 0; Ïϧ ÏϧÏÞßàpublic static void main(String args) { ÏϧÏϨ¹íÏint i = 2; ÏϧÏϨ¹¹´{ ÏϧÏϧÏϨ¹íÏint j = 3; ÏϧÏϧÏϨ¹¹ÏSystem.out.print("i + j = " + (i + j)); ÏϧÏϧÏÏ©} ÏϧÏϧ ÏϧÏϨ¹¹ÏSystem.out.println("; j = " + j); ÏϧÏÏ©} ÏÏ©} a) i + j = 5; j = 0 b) i + j = 5; j = 3 c) i + j = 0; j = 0 d) i + j = 0; j = 3 e) compiler error COMP 1210 Fall 2005 Exam 2 – 11/03/2005 – Page 6 of 10 25. The default layout out of a contentPane in a JFrame is __________. a) FlowLayout b) GridLayout c) BorderLayout d) GridBagLayout e) TabbedLayout 26. Java arrays can store primitive types and Strings, but cannot store any other type of Object other than Strings. a) True b) False 27. What is the printout of the following switch statement? ÏÏϨ¹íÏchar ch = 'b'; ÏÏϧ ÏÏϨ¹¹´switch (ch) { ÏÏϧÏÏ÷¹¹´case 'a': ÏÏϧÏÏ6ÏϨ¹¹ÏSystem.out.print(ch); ÏÏϧÏÏ÷¹¹Ãcase 'b': ÏÏϧÏÏ6ÏϨ¹¹ÏSystem.out.print(ch); ÏÏϧÏÏ÷¹¹Ãcase 'c': ÏÏϧÏÏ6ÏϨ¹¹ÏSystem.out.print(ch); ÏÏϧÏÏ÷¹¹Ãcase 'd': ÏÏϧÏÏ6ÏϾ¹¹ÏSystem.out.print(ch); ÏÏϧÏ϶} a) abcd b) bcd c) bbb d) bb e) b 28. What is the output of the following code? ÏÕÖ×class Test29{ ÏϧÏÞßàpublic static void main(String args) { ÏϧÏϨ¹íÏint x = 3; ÏϧÏϨ¹íÏint y = 0; ÏϧÏϧ ÏϧÏϨ¹¹´switch (x + 3) { ÏϧÏϧÏÏ÷¹¹´case 6: y = 0; ÏϧÏϧÏÏ÷¹¹Ãcase 7: y = 1; ÏϧÏϧÏÏ÷¹¹»default: y += 1; ÏϧÏϧÏ϶} ÏϧÏϧ ÏϧÏϨ¹¹ÏSystem.out.print (y); ÏϧÏÏ©} ÏÏ©} } a) 1 b) 2 c) 3 d) 4 e) 9 29. What is y after the following statement is executed? int x = 0; int y = (x > 0) ? 10 : -10; a) 0 b) 10 30. The following loop is syntactically correct. for (; ; ); a) True b) False c) 20 d) -10 e) Illegal expression COMP 1210 Fall 2005 Exam 2 – 11/03/2005 – Page 7 of 10 31. What is the printout of the following switch statement? Ϩ¹íÏchar ch = 'a'; Ïϧ ÏϨ¹¹´switch (ch) { ÏϧÏÏ÷¹¹´case 'a': ÏϧÏÏ6Ïϧcase 'A': ÏϧÏÏ6ÏϨ¹¹ÏSystem.out.print(ch); Ïϧ¹½ÏϾ¹¹Ïbreak; ÏϧÏÏ÷¹¹´case 'b': ÏϧÏÏ6Ïϧcase 'B': ÏϧÏÏ6ÏϨ¹¹ÏSystem.out.print(ch); Ïϧ¹½ÏϾ¹¹Ïbreak; ÏϧÏÏ÷¹¹´case 'c': ÏϧÏÏ6Ïϧcase 'C': ÏϧÏÏ6ÏϨ¹¹ÏSystem.out.print(ch); Ïϧ¹½ÏϾ¹¹Ïbreak; ÏϧÏÏ÷¹¹´case 'd': ÏϧÏÏ6Ïϧcase 'D': ÏϧÏÏ6ÏϾ¹¹ÏSystem.out.print(ch); ÏϧÏ϶} a) ab b) aa c) a d) abcd e) compiler error 32. The following two statements result in the same value in sum int sum = 0; for (int i = 0; i < 3; i++) { sum += i; } a) True int sum = 0; for (int i = 0; i < 3; i = i+1) { sum += i; } b) False 33. In order to implement Comparable in a class, what method(s) must be defined in that class? a) equals b) compares c) both lessThan and greaterThan d) compareTo e) both compares and equals 34. Aggregation is a) described as a has-a relationship b) using an object reference as a local variable c) a form of software documentation d) all of the above e) none of the above 35. When a class implements an interface, what must it do? a) it must redefine each constant from the interface. b) it must declare and provide a method body for each method in the interface. c) it must declare a variable for each constant in the interface. d) it must include a private method for each method in the interface. e) none of the above COMP 1210 Fall 2005 Exam 2 – 11/03/2005 – Page 8 of 10 Short Answer 10 points (4 + 3 + 3 points each) What is the output of the following code fragments? Place your answer in the space provided) Output: 36) ÏÕÖ×public class Short1 { ÏϧÏÞßàpublic static void main(String args) { ÏϧÏϨ¹íÏint list = {1, 2, 3, 4}; ÏϧÏϧ ÏϧÏϨ¹íÏint newList = new int[list.length]; ÏϧÏϧ ÏϧÏϨ¹¹±for (int i = 0; i < list.length; i++) ÏϧÏϧÏÏй¹ÏnewList[i] = list[list.length - 1 - i]; ÏϧÏϧ ÏϧÏϨ¹¹±for (int i = 0; i < newList.length; i++) ÏϧÏϧÏÏй¹ÏSystem.out.print(newList[i] + " "); ÏϧÏÏ©} ÏÏ©} 37) Ï Output: ÕÖ×public class Short2 { ÏϧÏÞßàpublic static void main(String argv) { ÏϧÏϨ¹íÏMyClass m1 = new MyClass (); ÏϧÏϨ¹¹Ïm1.iMyVal=0; ÏϧÏϨ¹íÏMyClass m2 = new MyClass (); ÏϧÏϨ¹¹Ïm2.iMyVal=1; ÏϧÏϨ¹íÏMyClass m3 = new MyClass (); ÏϧÏϨ¹¹Ïm3.iMyVal=99; ÏϧÏϨ¹¹ÏSystem.out.println("m1.iMyVal = "+m1.iMyVal); ÏϧÏϨ¹¹ÏSystem.out.println("m2.iMyVal = "+m2.iMyVal); ÏϧÏϨ¹¹ÏSystem.out.println("m3.iMyVal = "+m3.iMyVal); ÏϧÏÏ©} ÏÏ©} ÏÕÖ×class MyClass{ ÏϧÏíÏpublic static int iMyVal=0; ÏÏ©} 38) Ï ÕÖ×class Short3{ ÏϧÏÞßàpublic static void main (String args){ ÏϧÏϨ¹íÏint result = 0; ÏϧÏϨ¹íÏint j = 7; ÏϧÏϨ¹³´if (3 > 2) { ÏϧÏϧÏ6¨¹¹Ïresult = 1; ÏϧÏϧÏ6¾¹³´if (j > 4) ÏϧÏϧÏ6ÏÏ6¾¹¹Ïresult = 6; ÏϧÏϧÏ6ÏÏö´else ÏϧÏϧÏ6ÏÏȾ¹¹Ïresult = 5; ÏϧÏϧ϶Ï} ÏϧÏϨ¹¹ÏSystem.out.println ("Result = " + result); ÏϧÏϧ ÏϧÏϨ¹íÏString i = (j<=6)? "lesser" : "greater"; ÏϧÏϨ¹¹ÏSystem.out.println ("i = " + i); ÏϧÏϧ ÏϧÏϨ¹¹Ïi = (j<=8)? "lesser" : "greater"; ÏϧÏϨ¹¹ÏSystem.out.println ("i = " + i); ÏϧÏÏ©} ÏÏ©} Output: COMP 1210 Fall 2005 Exam 2 – 11/03/2005 – Page 9 of 10 COMP 1210 Fall 2005 Exam 2 – 11/03/2005 – Page 10 of 10 39) Problem (20 points) In the following driver program we create an array of 3 books and print out the details of each using a for loop. Then using the method largest we determine the book with the most number of pages. Fill in the missing parts (20 blanks @ 1 point each) of the following Java program. Use the SAMPLE OUTPUT to help determine your answers. Place your answers in spaces provided. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ÏÕÖ×class DriverProgram{ ÏϧÏíÏstatic private Book __________________ = new _____________________[3]; Ïϧ ÏϧÏÞßàpublic static void main (String args){ ÏϧÏϨ¹¹Ïlibrary[______] = new Book("William", "Strunk Jr.", "The Elements of Style", 105 ); ÏϧÏϨ¹¹Ïlibrary[______] = new Book("Thomas", "Friedman", "The World Is Flat", 496 ); ÏϧÏϨ¹¹Ïlibrary[______] = new Book("Dan", "Brown", "Angels & Demon", 608); ÏϧÏϧ ÏϧÏϨ¹¹±for (int i= ________; i < library.____________; ______________ ) ÏϧÏϧÏÏй¹ÏSystem.out.println( ________________________________________ ); ÏϧÏϧ ÏϧÏϨ¹¹ÏSystem.out.println ("\n\nBook with most number of pages = "); ÏϧÏϨ¹¹ÏSystem.out.println( largest(library[0], library[1], library[2]) ); ÏϧÏÏ©} Ïϧ ÏϧÏÞßàstatic Comparable ____________________________ (Comparable b1, ÏϧÏϧÏComparable b2, Comparable b3 ){ ÏϧÏϨ¹íÏ_________________________________ largest=null; ÏϧÏϧ ÏϧÏϨ¹³´if (b1.compareTo(b2)>0 && b1.compareTo(b3)>0) ÏϧÏϧ϶¾¹¹Ïlargest = b1; ÏϧÏϧ ÏϧÏϨ¹³´if (b2.compareTo(b3)>0 && b2.compareTo(b1)>0) ÏϧÏϧ϶¾¹¹Ïlargest = b2; ÏϧÏϧ ÏϧÏϨ¹³´if (b3.compareTo(b2)>0 && b3.compareTo(b1)>0) SAMPLE OUTPUT ÏϧÏϧ϶¾¹¹Ïlargest = b3; Ïϧ¹Ĺ¹Ïreturn largest; Author: William Strunk Jr. Title: The Elements of Style Pages: 105 ÏϧÏÏ©} Author: Thomas Friedman Title: The World Is Flat Pages: 496 Author: Dan Brown Title: Angels & Demon Pages: 608 ÏÏ©} 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ÏÕÖ×class Book implements ____________________{ Book with most number of pages = ÏϧÏíÏprivate String authorFirst, Author: Dan Brown Title: Angels & Demon Pages: 608 ÏϧÏÏÏ authorLast, title; ÏϧÏíÏprivate int pages; Ïϧ ÏϧÏÞßàpublic Book (String authorF, String authorL, ÏϧÏϧÏString title, int pages){ ÏϧÏϨ¹¹ÏauthorFirst = __________________________; ÏϧÏϨ¹¹ÏauthorLast = authorL; ÏϧÏϨ¹¹Ïthis.title = _____________________________; ÏϧÏϨ¹¹Ïthis.pages = pages; ÏϧÏÏ©} Ïϧ ÏϧÏÞßàpublic String toString(){ ÏϧÏϨ¹íÏString result = "Author: " + ________________ +" " +_______________ + "\tTitle: " + ÏϧÏϧÏÏÏÏÏÏ title + "\tPages: " + pages; Ïϧ¹Ĺ¹Ïreturn _______________________________; ÏϧÏÏ©} Ïϧ ÏϧÏÞßàpublic int compareTo(Object _________________________________){ ÏϧÏϨ¹íÏBook obj2 = (Book) obj; ÏϧÏϨ¹íÏInteger ____________________________ = new Integer (pages); ÏϧÏϨ¹íÏInteger ____________________________ = new Integer (obj2.pages); Ïϧ¹Ĺ¹Ïreturn (pagesObj1.compareTo(pagesObj2)); ÏϧÏÏ©} ÏÏ©} ...
View Full Document

Ask a homework question - tutors are online