Exam JP2011(Java Programming) MidTerm Test 5/6 2011 Name RegNo 1. 25 % 1 is. a) 1 b) 2 c) 3 d) 4 e) 0 2. -24 % -5 is. a) 3 b) -3 c) 4 d) -4 e) 0 3. Which of these data types requires the most amount of memory? a) long b) int c) byte d) short 4. What is the printout of the following code: double x = 5.5; int y = (int)x; System.out.println(ʺx is ʺ + x + ʺ and y is ʺ + y); a) x is 5.5 and y is 5.0 b) x is 6 and y is 6 c) x is 5 and y is 6 d) x is 5.5 and y is 5 e) x is 6.0 and y is 6.0 5. Suppose x is 1. What is x after x -= 1? a) 0 b) 1 c) 2 d) -1 e) -2 6. The expression ʺJava ʺ + 1 + 2 + 3 evaluates to. a) java 123 b) Java 123 c) Java6 d) Java123 e) Illegal expression 7. What is 1.0 + 1.0 + 1.0 + 1.0 + 1.0 == 5.0? a) true b) false c) There is no guarantee that 1.0 + 1.0 + 1.0 + 1.0 + 1.0 == 5.0 is true. 8. What is the printout of the following switch statement? char ch = ʹbʹ; switch (ch) { case ʹaʹ: System.out.print(ch); case ʹbʹ: System.out.print(ch); case ʹcʹ: System.out.print(ch); case ʹdʹ: System.out.print(ch); a) abcd b) bb c) bcd d) b e) bbb 9. The following code displays. double temperature = 50; if (temperature >= 100) System.out.println(ʺtoo hotʺ); else if (temperature <= 40) System.out.println(ʺtoo coldʺ); else System.out.println(ʺjust rightʺ); a) too cold b) just right c) too hot d) too hot too cold just right 10. What is the number of iterations in the following loop: for (int i = 1; i < n; i++) { // iteration a) n + 1 b) n - 1 c) 2*n d) n 11. (int)(ʹaʹ + Math.random() * (ʹzʹ - ʹaʹ + 1)) returns a random number. a) between (int)ʹaʹ and (int)ʹzʹ b) between ʹaʹ and ʹzʹ c) between ʹaʹ and ʹyʹ d) between 0 and (int)ʹzʹ 1
12. Analyze the following code fragments that assign a boolean value to the variable even. Code 1: if (number % 2 == 0) even = true; else even = false; Code 2: even = (number % 2 == 0)? true: false; Code 3: even = number % 2 == 0; a) Code 2 has a compile error, because you cannot have true and false literals in the conditional expression. b) All three are correct, but Code 2 is preferred. c) All three are correct, but Code 1 is preferred. d) All three are correct, but Code 3 is preferred. e) Code 3 has a compile error, because you attempt to assign number to even. 13. How many times will the following code print ʺWelcome to Javaʺ? int count = 0; do { System.out.println(ʺWelcome to Javaʺ); while (++count < 10); a) 8 b) 10 c) 9 d) 11 e) 0 14. How many times will the following code print ʺWelcome to Javaʺ? int count = 0; do { System.out.println(ʺWelcome to Javaʺ); count++; while (count < 10); 15. Analyze the following fragment: double sum = 0; double d = 0; while (d!= 10.0) { d += 0.1; sum += sum + d; a) The program never stops because d is always 0.1 inside the loop. b) The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers. c) After the loop, sum is 0 + 0.1 + 0.2 + 0.3 +... + 1.9 d) The program does not compile because sum and d are declared double, but assigned with integer value 0. 16. Will the following program terminate? int balance = 10; while (true) { if (balance < 9) break; balance = balance - 9; a) Yes b) No 17. Which of the following statements are correct to invoke the printmax method in Listing 6.5 in the textbook? (Choose all that apply.) a) printmax(new double[ ]{1, 2, 3); b) printmax(1.0, 2.0, 2.0, 1.0, 4.0); c) printmax(1, 2, 2, 1, 4); d) printmax(new int[ ]{1, 2, 3); 18. What is Math.ceil(3.6)? a) 4.0 b) 3 c) 5.0 d) 3.0 a) 9 b) 10 c) 0 d) 11 e) 8 2
19. The selectionsort method is defined in this section. What is list1 after executing the following statements? double[ ] list1 = {3.1, 3.1, 2.5, 6.4; selectionsort(list1); a) list1 is 3.1, 2.5, 3.1, 6.4 b) list1 is 6.4, 3.1, 3.1, 2.5 c) list1 is 3.1, 3.1, 2.5, 6.4 d) list1 is 2.5 3.1, 3.1, 6.4 20. Analyze the following code: class Test { System.out.println(xmethod(5)); public static int xmethod(int n, long t) { System.out.println(ʺintʺ); return n; public static long xmethod(long n) { System.out.println(ʺlongʺ); return n; a) The program displays long followed by 5. b) The program displays int followed by 5. c) The program runs fine but displays things other than 5. d) The program does not compile because the compiler cannot distinguish which xmethod to invoke. 22. What is k after the following block executes? { int k = 2; nprint(ʺa messageʺ, k); System.out.println(k); a) 1 b) 2 c) 0 d) k is not defined outside the block. So, the program has a compile error 23. In the following code, what is the printout for list2? class Test { int[ ] list1 = {1, 2, 3; int[ ] list2 = {1, 2, 3; list2 = list1; list1[0] = 0; list1[1] = 1; list2[2] = 2; for (int i = 0; i < list2.length; i++) System.out.print(list2[i] + ʺ ʺ); a) 0 1 2 b) 1 1 1 c) 0 1 3 d) 1 2 3 24. Which of the following statements is valid? (Choose all that apply.) a) double d[ ] = new double[30]; b) int[ ] i = {3, 4, 3, 2; c) int i = new int(30); d) char[ ] c = new char(); e) char[ ] c = new char[4]{ʹaʹ, ʹbʹ, ʹcʹ, ʹdʹ; 21. The keyword is required to declare a class. a) class b) public c) private d) All of the above. 3
25. Show the output of the following code: int[ ] x = {1, 2, 3, 4, 5; increase(x); int[ ] y = {1, 2, 3, 4, 5; increase(y[0]); System.out.println(x[0] + ʺ ʺ + y[0]); public static void increase(int[ ] x) { for (int i = 0; i < x.length; i++) x[i]++; public static void increase(int y) { y++; a) 2 1 b) 2 2 c) 1 2 d) 1 1 e) 0 0 26. The default value for data field of a boolean type, numeric type, object type is, respectively. a) false, 1, null b) true, 0, null c) true, 1, Null d) true, 1, null e) false, 0, null 27. Suppose you wish to provide an accessor method for a boolean property finished, what signature of the method should be? a) public void getfinished() b) public boolean isfinished() c) public void isfinished() d) public boolean getfinished() 29. Analyze the following code: boolean[ ][ ] x = new boolean[3][ ]; x[0] = new boolean[1]; x[1] = new boolean[2]; x[2] = new boolean[3]; System.out.println(ʺx[2][2] is ʺ + x[2][2]); a) The program has a compile error because new boolean[3][ ] is wrong. b) The program runs and displays x[2][2] is true. c) The program runs and displays x[2][2] is false. d) The program has a runtime error because x[2][2] is null. e) The program runs and displays x[2][2] is null. 30. When invoking a method with an object argument, is passed. a) the reference of the object b) a copy of the object c) the object is copied, then the reference of the copied object d) the contents of the object 31. Suppose a method p has the following heading: public static int[ ][ ] p() What return statement may be used in p()? a) return int[ ]{1, 2, 3; b) return new int[ ][ ]{{1, 2, 3, {2, 4, 5; c) return new int[ ]{1, 2, 3; d) return {1, 2, 3; e) return 1; 28. An object is an instance of a. a) class b) method c) program d) data 4
32. What is the value of mycount.count displayed? Count mycount = new Count(); int times = 0; for (int i=0; i<100; i++) increment(mycount, times); System.out.println( ʺmyCount.count = ʺ + mycount.count); System.out.println(ʺtimes = ʺ+ times); public static void increment(count c, int times) { c.count++; times++; class Count { int count; Count(int c) { count = c; Count() { count = 1; a) 99 b) 100 c) 101 d) 98 33. Assume s is ʺABCABCʺ, the method returns a new string ʺaBCaBCʺ. a) s.replace(ʺabcabcʺ, ʺaBCaBCʺ) b) s.replace(ʹaʹ, ʹAʹ) c) s.tolowercase(s) d) s.replace(ʹaʹ, ʹaʹ) e) s.tolowercase() 34. Suppose Character x = new Character(ʹaʹ), returns true. (Choose all that apply.) a) x.comparetoignorecase(ʹaʹ) b) x.equals(ʹaʹ) c) x.equals(ʺaʺ) d) x.equalsignorecase(ʹaʹ) e) x.equals(new Character(ʹaʹ)) 35. Analyze the following code: (Choose all that apply.) A a = new A(); a.print(); class A { String s; A(String s) { this.s = s; void print() { System.out.println(s); a) The program would compile and run if you change A a = new A() to A a = new A(ʺ5ʺ). b) The program has a compilation error because class A does not have a default constructor. c) The program compiles and runs fine and prints nothing. d) The program has a compilation error because class A is not a public class. 36. is invoked to create an object. a) A method with a return type b) The main method c) A constructor d) A method with the void return type 5
37. Analyze the following code and choose the best answer: public class Foo { private int x; Foo foo = new Foo(); System.out.println(foo.x); a) Since x is private, it cannot be accessed from an object foo. b) You cannot create a self-referenced object; that is, foo is created inside the class Foo. c) Since x is an instance variable, it cannot be directly used inside a main method. However, it can be accessed through an object such as foo in this code. d) Since x is defined in the class Foo, it can be accessed by any method inside the class without using an object. You can write the code to access x without creating an object such as foo in this code. 38. Which of the following is the correct header of the main method? (Choose all that apply.) a) public static void main(string x[ ]) b) public static void main(string[ ] args) c) public static void main(string args[ ]) d) static void main(string[ ] args) e) public static void main(string[ ] x) 39. Which of the following statements creates an instance of File on Window for the file c:\t.txt? a) new File(ʺc://txt.txtʺ) b) new File(ʺc:/txt.txtʺ) c) new File(ʺc:\\txt.txtʺ) d) new File(ʺc:\txt.txtʺ) 40. Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following methods will cause the list to become [Beijing, Chicago, Singapore]? a) x.add(ʺchicagoʺ) b) x.add(1, ʺChicagoʺ) c) x.add(0, ʺChicagoʺ) d) x.add(2, ʺChicagoʺ) 41. What is the output of the following code? String s1 = ʺWelcome to Java!ʺ; String s2 = s1; if (s1 == s2) System.out.println(ʺs1 and s2 reference to the same String objectʺ); else System.out.println(ʺs1 and s2 reference to different String objectsʺ); a) s1 and s2 reference to different String objects b) s1 and s2 reference to the same String object 42. Analyze the following code: (Choose all that apply.) Object a1 = new A(); Object a2 = new Object(); System.out.println(a1); System.out.println(a2); class A { int x; public String tostring() { return ʺAʹs x is ʺ + x; a) When executing System.out.println(a2), the tostring() method in the Object class is invoked. b) The program cannot be compiled, because System.out.println(a1) is wrong and it should be replaced by System.out.println(a1.toString()); c) When executing System.out.println(a1), the tostring() method in the Object class is invoked. d) When executing System.out.println(a1), the tostring() method in the A class is invoked. 6
43. Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following method will cause runtime errors? (Choose all that apply.) a) x.remove(2) b) x.get(2) c) x.set(2, ʺNew Yorkʺ); d) x.get(1) e) x.size() 44. You can create an ArrayList using. a) ArrayList() b) new ArrayList() c) new ArrayList[ ] d) new ArrayList[100] 45. Which of the following statements are true? (Choose all that apply.) a) A method may be implemented in several subclasses. The Java Virtual Machine dynamically binds the implementation of the method at runtime. b) You can always pass an instance of a subclass to a parameter of its superclass type. This feature is known as polymorphism. c) The compiler finds a matching method according to parameter type, number of parameters, and order of the parameters at compilation time. d) Dynamic binding can apply to static methods. e) Dynamic binding can apply to instance methods. 46. Which of the following statements are true? (Choose all that apply.) a) A method can be overridden in the same class. b) If a method overrides another method, these two methods must have the same signature. c) If a method overloads another method, these two methods must have the same signature. d) A method can be overloaded in the same class. 47. To create an instance of BigDecimal for 454.45, use a) new BigDecimal(ʺ454.45ʺ); b) BigInteger(454.45); c) BigInteger(ʺ454.45ʺ); d) new BigInteger(454.45); 48. Suppose A is an abstract class, B is a concrete subclass of A, and both A and B have a default constructor. Which of the following is correct? (Choose all that apply.) a) A a = new A(); b) B b = new B(); c) A a = new B(); d) B b = new A(); 49. What is the output of the following code: Object o1 = new Object(); Object o2 = new Object(); System.out.print((o1 == o2) + ʺ ʺ + (o1.equals(o2))); a) false true b) false false c) true true d) true false 50. Object-oriented programming allows you to derive new classes from existing classes. This is called. a) generalization b) abstraction c) encapsulation d) inheritance 51. Analyze the following code: (Choose all that apply.) ArrayList list = new ArrayList(); list.add(ʺbeijingʺ); list.add(ʺtokyoʺ); list.add(ʺshanghaiʺ); list.set(3, ʺHong Kongʺ); a) The last line in the code causes a runtime error because there is no element at index 3 in the array list. b) If you replace the last line by list.add(4, ʺHong Kongʺ), the code will compile and run fine. c) If you replace the last line by list.add(3, ʺHong Kongʺ), the code will compile and run fine. d) The last line in the code has a compile error because there is no element at index 3 in the array list. 7
52. Assume Calendar calendar = new GregorianCalendar(). returns the number of days in a month. a) calendar.get(calendar.week_of_year) b) calendar.getactualmaximum(calendar.day_of_month) c) calendar.get(calendar.month_of_year) d) calendar.get(calendar.month) e) calendar.get(calendar.week_of_month) 53. Given the following code, find the compile error. (Choose all that apply.) m(new GraduateStudent()); m(new Student()); m(new Person()); m(new Object()); public static void m(student x) { System.out.println(x.toString()); class GraduateStudent extends Student { class Student extends Person { public String tostring() { return ʺStudentʺ; class Person extends Object { public String tostring() { return ʺPersonʺ; a) m(new Person()) causes an error b) m(new Object()) causes an error c) m(new Student()) causes an error d) m(new GraduateStudent()) causes an error 54. Suppose A is an interface, B is a concrete class with a default constructor that implements A. Which of the following is correct? (Choose all that apply.) a) A a = new B(); b) B b = new B(); c) B b = new A(); d) A a = new A(); 55. is a reference type. (Choose all that apply.) a) An interface type b) A class type c) A primitive type d) An array type 56. Which statements are most accurate regarding the following classes? class A { private int i; protected int j; class B extends A { private int k; protected int m; a) An object of B contains data fields j, m. b) An object of B contains data fields k, m. c) An object of B contains data fields i, j, k, m. d) An object of B contains data fields j, k, m. 57. Which of the following statements are true? (Choose all that apply.) a) If you compile an interface without errors, a.class file is created for the interface. b) If you compile an interface without errors, but with warnings, a.class file is created for the interface. c) If you compile a class without errors but with warnings, a.class file is created. d) If you compile a class with errors, a.class file is created for the class. 58. is a reference type. (Choose all that apply.) a) An array type b) A primitive type c) An interface type d) A class type 59. Which of the following classes are immutable? (Choose all that apply.) a) Double b) BigDecimal c) String d) Integer e) BigInteger 60. To assign a double variable d to a float variable x, you write a) x = (int)d; b) x = d; c) x = (long)d d) x = (float)d; 8
Answer Key Testname: JP2011MIDTERMTESTV2 1. e 2. d 3. a 4. d 5. a 6. b 7. c 8. e 9. b 10. b 11. a 12. d 13. b 14. b 15. b 16. a 17. a, b, c 18. a 19. d 20. a 21. a 22. d 23. a 24. a, b 25. a 26. e 27. b 28. a 29. c 30. a 31. b 32. c 33. d 34. b, e 35. a, b 36. c 37. c 38. a, b, c, e 39. c 40. b 41. b 42. a, d 43. a, b, c 44. b 45. a, b, c, e 46. b, d 47. a 48. b, c 49. b 50. d 51. a, c 52. b 53. a, b 54. a, b 55. a, b, d 56. c 57. a, b, c 58. a, c, d 59. a, b, c, d, e 60. d 9
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. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 10