Exam Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) You can always convert a while loop to a for loop. 1) 2) Will the following program terminate?int balance = 10;while (true) { if (balance < 9) break; balance = balance - 9;} 2) A) Yes B) No 3) You can always write a program without using break or continue in a loop. 4) What is sum after the following loop terminates?int sum = 0;int item = 0;do { item++; sum += item; if (sum >= 4) continue;}while (item < 5); 3) 4) A) 18 B) 16 C) 15 D) 17 5) Analyze the following code.int count = 0;while (count < 100) { // Point A System.out.println("Welcome to Java!"); count++; // Point B} // Point C 5) (choose all that apply) A) count < 100 is always false at Point C B) count < 100 is always false at Point B C) count < 100 is always true at Point C D) count < 100 is always true at Point A E) count < 100 is always true at Point B 6) Is the following loop correct?for (; ; ); 6) A) Yes B) No 7) What is sum after the following loop terminates?int sum = 0;int item = 0;do { item++; sum += item; if (sum > 4) break;}while (item < 5); 7) A) 7 B) 8 C) 6 D) 5 8) How many times will the following code print "Welcome to Java"?int count = 0;do { System.out.println("Welcome to Java");} while (count++ < 10); 8) A) 11 B) 9 C) 0 D) 10 E) 8 9) To add 0.01 + 0.02 +... + 1.00, what order should you use to add the numbers to get better accuracy? A) add 1.00, 0.99, 0.98,..., 0.02, 0.01 in this order to a sum variable whose initial value is 0. B) add 0.01, 0.02,..., 1.00 in this order to a sum variable whose initial 9)
value is 0. 10) What is the number of iterations in the following loop: for (int i = 1; i <= n; i++) { // iteration } 10) A) n + 1 B) n - 1 C) 2*n D) n 11) Analyze the following statement:double sum = 0;for (double d = 0; d<10;) { d += 0.1; sum += sum + d;} 11) A) The program has a syntax error because the adjustment is missing in the for loop. B) The program runs in an infinite loop because d<10 would always be true. C) The program has a syntax error because the control variable in the for loop cannot be of the double type. D) The program compiles and runs fine. 12) Analyze the following fragment:double sum = 0;double d = 0;while (d!= 10.0) { d += 0.1; sum += sum + d;} 12) A) After the loop, sum is 0 + 0.1 + 0.2 + 0.3 +... + 1.9 B) The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers. C) The program does not compile because sum and d are declared double, but assigned with integer value 0. D) The program never stops because d is always 0.1 inside the loop. 13) What is 1.0 + 1.0 + 1.0 == 3.0? 13) A) true B) false C) There is no guarantee that 1.0 + 1.0 + 1.0 == 3.0 is true. 14) What is the output of the following fragment? for (int i = 0; i < 15; i++) { if (i % 4 == 1) System.out.print(i + " "); } 14) A) 1 5 9 13 16 B) 1 4 8 12 C) 1 3 5 7 9 11 13 D) 1 3 5 7 9 11 13 15 E) 1 5 9 13 15) What is y after the following for loop statement is executed?int y = 0;for (int i = 0; i < 10; ++i) { y += 1; } 15) A) 10 B) 9 C) 11 D) 12 16) What the output of the following code:for ( ; false ; ) System.out.println("Welcome to Java"); 16) A) prints out Welcome to Java one time. B) prints out Welcome to Java two times.
C) does not print anything. D) prints out Welcome to Java forever. 17) After the continue outer statement is executed in the following loop, which statement is executed? outer: for (int i = 1; i < 10; i++) { inner: for (int j = 1; j < 10; j++) { if (i * j > 50) continue outer; System.out.println(i * j); } } next: A) The program terminates. B) The control is in the inner loop, and the next iteration of the inner loop is executed. C) The control is in the outer loop, and the next iteration of the outer loop is executed. D) The statement labeled next. 18) Will the following program terminate?int balance = 10;while (true) { if (balance < 9) continue; balance = balance - 9;} 17) 18) A) Yes B) No 19) What is the output for y?int y = 0;for (int i = 0; i<10; ++i) { y += i; }System.out.println(y); A) 13 B) 45 C) 11 D) 10 E) 12 20) What is the output of the following fragment?int i = 1;int j = 1;while (i < 5) { i++; j = j * 2;}System.out.println(j); 19) 20) A) 16 B) 8 C) 4 D) 32 E) 64 21) What is the value in count after the following loop is executed?int count = 0;do { System.out.println("Welcome to Java");} while (count++ < 9);System.out.println(count); 21) A) 8 B) 9 C) 11 D) 0 E) 10 22) How many times will the following code print "Welcome to Java"?int count = 0;while (count < 10) { System.out.println("Welcome to Java"); count++;} 22) A) 11 B) 0 C) 8 D) 9 E) 10 23) After the break outer statement is executed in the following loop, which statement is executed? outer: for (int i = 1; i < 10; i++) { inner: for (int j = 1; j < 10; j++) { if (i * j > 50) break outer; System.out.println(i * j); } } next: A) The program terminates. B) The statement labeled inner. C) The statement labeled outer. D) The statement labeled next. 24) Assume x is 0. What is the output of the following statement?if (x > 0) printf("x is greater than 0");else if (x < 0) printf("x is less than 0");else printf("x equals 0"); 23) 24)
A) x equals 0 B) x is greater than 0 C) x is less than 0 D) None 25) What balance after the following code is executed?int balance = 10;while (balance >= 1) { if (balance < 9) continue; balance = balance - 9;} 25) A) 1 B) The loop does not end C) 2 D) -1 E) 0 26) What is Math.round(3.6)? 26) A) 3.0 B) 4.0 C) 4 D) 3 27) is a simple but incomplete version of a method. 27) A) A main method B) A stub C) A method developed using top-down approach D) A non-main method 28) Which of the following is a possible output for (int)(51* Math.random())? (choose all that apply) A) 0 B) 50 C) 500 D) 100 29) Analyze the following code.public class Test { public static void main(string[] args) { System.out.println(m(2)); } public static int m(int num) { return num; } public static void m(int num) { System.out.println(num); }} 28) 29) A) The program runs and prints 2 once. B) The program has a syntax error because the second m method is defined, but not invoked in the main method. C) The program runs and prints 2 twice. D) The program has a syntax error because the two methods m have the same signature. 30) What is Math.floor(3.6)? 30) A) 5.0 B) 4 C) 3.0 D) 3 31) Does the method call in the following method cause syntax errors?public static void main(string[] args) { Math.pow(2, 4);} 31) A) Yes B) No 32) Suppose static void nprint(string message, int n) { while (n > 0) { System.out.print(message); n--; }}What is the printout of the call nprint('a', 4)? 32) A) aaa B) aaaa C) aaaaa D) invalid call
33) Which of the following method results in 8.0? (choose all that apply) 33) A) Math.rint(8.5) B) Math.ceil(8.5) C) Math.round(8.5) D) Math.floor(8.5) 34) A method can be defined inside a method in Java. 34) 35) Math.floor(5.5) evaluates to. 35) A) 5 B) 6 C) 5.0 D) 6.0 36) Math.ceil(5.5) evaluates to. 36) A) 6 B) 6.0 C) 5.0 D) 5 37) Suppose static void nprint(string message, int n) { while (n > 0) { System.out.print(message); n--; }}What is k after invoking nprint("a message", k)?int k = 2;nPrint("A message", k); 37) A) 0 B) 2 C) 3 D) 1 38) Analyze the following code:class Test { public static void main(string[] args) { System.out.println(xMethod((double)5)); } public static int xmethod(int n) { 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 given in A and B. D) The program does not compile. 39) You can define two methods in the same class with the same name and parameter list. 38) 39) 40) Which of the following should be declared as a void method? 40) A) Write a method that checks whether current second is today is integers from 1 to 100. B) Write a method that returns a random integer from 1 to 100. C) Write a method that converts an uppercase letter to lowercase. D) Write a method that prints integers from 1 to 100. 41) A variable defined inside a method is referred to as. 41) A) a local variable B) a block variable C) a global variable D) a method variable 42) Each time a method is invoked, the system stores parameters and local variables in an area of memory, known as, which stores elements in last-in first-out fashion. A) a stack B) storage area C) a heap D) an array 42) 43) is to implement one method in the structure chart at a time fro m the
top to the bottom. 43) A) Bottom-up and top-down approach B) Bottom-up approach C) Top-down approach D) Stepwise refinement 44) Analyze the following code:public class Test { public static void main(string[] args) { int[] oldlist = {1, 2, 3, 4, 5}; reverse(oldlist); for (int i = 0; i < oldlist.length; i++) System.out.print(oldList[i] + ""); } public static void reverse(int[] list) { int[] newlist = new int[list.length]; for (int i = 0; i < list.length; i++) newlist[i] = list[list.length - 1 - i]; list = newlist; }} 44) A) The program displays 1 2 3 4 5 and then raises an ArrayIndexOutOfBoundsException. B) The program displays 5 4 3 2 1 and then raises an ArrayIndexOutOfBoundsException. C) The program displays 1 2 3 4 5. D) The program displays 5 4 3 2 1. 45) The array size is specified when the array is declared. 45) 46) 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]); }} 46) A) The program has a runtime error because the array element x[0] is not defined. B) The program has a runtime error because the array elements are not initialized. C) The program has a syntax error because the size of the array wasn't specified when declaring the array. D) The program runs fine and displays x[0] is 0. 47) Consider the following statements:int[] numbers = new int[10];int[] numbers = new int[5];what is numbers.length? A) 10. B) 5 C) undefined. D) The above statements are illegal. 48) Show the output of the following code:public class Test { public static void main(string[] args) { 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++; }} 47) 48)
A) 2 2 B) 0 0 C) 1 1 D) 2 1 E) 1 2 49) Given the following statement int[ ] list = new int[10]; list.length has the value A) 9 B) 11 C) 10 D) The value depends on how many integers are stored in list. 50) Given the following declaration: int[ ][] m = new int[5][6]; Which of the following statements is true? A) The name m represents a two-dimensional array of 30 int values. B) m[2][4] represents the element stored in the 2nd row and the 4th column of m. C) m[0].length has the value 5. D) m.length has the value 6. 51) Analyze the following code:public class Test { public static void main(string[] args) { final 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] + " "); }} 49) 50) 51) A) The program displays 0 0 B) The program has a syntax error on the statement x = new int[2], because x is final and cannot be changed. C) The program displays 1 2 3 4 D) The elements in the array x cannot be changed, because x is final. 52) Analyze the following code:public class Test1 { public static void main(string[] args) { xmethod(new double[]{3, 3}); xmethod(new double[5]); xmethod(new double[3]{1, 2, 3}); } public static void xmethod(double[] a) { System.out.println(a.length); }} 52) A) The program has a syntax error because xmethod(new double[3]{1, 2, 3}) is incorrect. B) The program has a runtime error because a is null. C) The program has a syntax error because xmethod(new double[5]) is incorrect. D) The program has a syntax error because xmethod(new double[]{3, 3}) is incorrect. 53) In the following code, what is the printout for list1?class Test { public static void main(string[] args) { 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 < list1.length; i++) System.out.print(list1[i] + ""); }} 53) A) 0 1 2 B) 1 1 1 C) 1 2 3 D) 0 1 3 54) The array index is not limited to int type. 54)
55) What would be the result of attempting to compile and run the following code? public class Test { public static void main(string[] args) { double[] x = new double[]{1, 2, 3}; System.out.println("Value is " + x[1]); }} 55) A) The program has a syntax error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by new double[3]{1, 2, 3}; B) The program has a syntax error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by new double[]{1.0, 2.0, 3.0}; C) The program compiles and runs fine and the output "Value is 2.0" is printed. D) The program has a syntax error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by {1, 2, 3}. E) The program compiles and runs fine and the output "Value is 1.0" is printed. 56) Which of the following declarations are correct? (choose all that apply) 56) A) public static void print(double... numbers) B) public static double... print(double d1, double d2) C) public static void print(string... strings, double... numbers) D) public static void print(int n, double... numbers) E) public static void print(double... numbers, String name) 57) When you return an array from a method, the method returns. 57) A) the reference of the array B) a copy of the first element C) the length of the array D) a copy of the array 58) The size of can grow and shrink at runtime. 58) A) an ArrayList B) an array 59) Normally you depend on the JVM to perform garbage collection automatically. However, you can explicitly use to request garbage collection. A) System.gc(0) B) System.gc() C) System.exit() D) System.exit(0) 60) Analyze the following code.// Program 1:public class Test { public static void main(string[] args) { Object a1 = new A(); Object a2 = new A(); System.out.println(a1.equals(a2)); }}class A { int x; public boolean equals(a a) { return this.x == a.x; }}// Program 2:public class Test { public static void main(string[] args) { A a1 = new A(); A a2 = new A(); System.out.println(a1.equals(a2)); }}class A { int x; public boolean equals(a a) { return this.x == a.x; }} A) Program 1 displays false and Program 2 displays true B) Program 1 displays false and Program 2 displays false C) Program 1 displays true and Program 2 displays false D) Program 1 displays true and Program 2 displays true 59) 60)
61) Given the following code, find the syntax error?public class Test { public static void main(string[] args) { 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"; }} 61) (choose all that apply) A) m(new Object()) causes an error B) m(new Student()) causes an error C) m(new GraduateStudent()) causes an error D) m(new Person()) causes an error 62) Analyze the following code:public class Test { public static void main(string[] args) { 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; }} 62) (choose all that apply) 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. 63) If a parameter is of the java.lang.object type, you can pass any object to it. This is known as generic programming. 63) 64) Which of the following statements are true? (choose all that apply) 64) A) It is a compilation error if two methods differ only in return type. B) A private method cannot be overridden. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated. C) To override a method, the method must be defined in the subclass using the same signature and return type as in its superclass. D) Overloading a method is to provide more than one method with the same name but with different signatures to distinguish them. E) A static method cannot be overridden. If a static method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden. 65) The UML uses before a member name to indicate that the me mber is
private. 65) A) + B) - C) # D) None of the above. 66) The getvalue() method is overridden in two ways. Which one is correct?i:public class Test { public static void main(string[] args) { A a = new A(); System.out.println(a.getValue()); }}class B { public String getvalue() { return "Any object"; }}class A extends B { public Object getvalue() { return "A string"; }}II:public class Test { public static void main(string[] args) { A a = new A(); System.out.println(a.getValue()); }}class B { public Object getvalue() { return "Any object"; }}class A extends B { public String getvalue() { return "A string"; }} 66) A) I B) II C) Neither D) Both I and II 67) Which of the following statements are true? (choose all that apply) 67) A) A subclass is a subset of a superclass. B) "class A extends B" means B is a subclass of A. C) A subclass is usually extended to contain more functions and more detailed information than its superclass. D) "class A extends B" means A is a subclass of B. 68) Which of the following loops produces the following output? (choose all that apply)1 234123121(I) for (int i = 5; i > 0; i--) { for (int j = 1; j < i; j++) System.out.print(j + ""); System.out.println(); }(II) for (int i = 1; i < 5; i++) { for (int j = 1; j < i; j++) System.out.print(j + ""); System.out.println(); } (III) int i = 0;while (i < 5) { for (int j = 1; j < i; j++) System.out.print(j + ""); System.out.println(); i++; }(IV) int i = 5;while (i > 0) { for (int j = 1; j < i; j++) System.out.print(j + " "); System.out.println(); i--; } 68) A) (I) B) (II) C) (IV) D) (III) 69) How many times will the following code print "Welcome to Java"?int count = 0;do { System.out.println("Welcome to Java");} while (++count < 10); 69) A) 0 B) 8 C) 9 D) 10 E) 11 70) You can always convert a for loop to a while loop. 70) 71) Analyze the following code. int x = 1; while (0 < x) && (x < 100) System.out.println(x++); 71) A) The code does not compile because (0 < x) && (x < 100) is not enclosed in a pair of parentheses.
B) The numbers 1 to 99 are displayed. C) The loop runs for ever. D) The numbers 2 to 100 are displayed. E) The code does not compile because the loop body is not in the braces. 72) In a for statement, if the continuation condition is blank, the condition is assumed to be. 73) The while loop and the do loop are equivalent in their expressive power; in other words, you can rewrite a while loop using a do loop, and vice versa. 74) Suppose cond1 is a Boolean expression. When will this while condition be true?while (cond1)... 72) 73) 74) A) always false B) in case cond1 is false C) in case cond1 is true D) always true 75) A break statement can be used only in a loop. 75) 76) The client can use a method without knowing how it is implemented. The details of the implementation are encapsulated in the method and hidden from the client who invokes the method. This is known as.(choose all that apply) A) method hiding B) encapsulation C) simplifying method D) information hiding 76) 77) Which of the following is not an advantage of using methods? 77) A) Using methods makes programs easier to read. B) Using methods makes reusing code easier. C) Using methods hides detailed implementation from the clients. D) Using methods makes programs run faster. 78) Variables defined in the method header are called. 78) A) parameters B) arguments C) local variables D) global variables 79) When you invoke a method with a parameter, the value of the argument is passed to the parameter. This is referred to as. A) method invocation B) pass by value C) pass by reference D) pass by name 79) 80) Math.sqrt(4) returns. 80) A) 1 B) 2 C) 1.0 D) 2.0 81) Which of the following is a possible output from invoking Math.random()? (choose all that apply) A) 1.0 B) 3.43 C) 0.5 D) 0.0 81)
82) Analyze the following code.int[] list = new int[5];list = new int[6]; 82) A) The code has syntax errors because the variable list cannot be changed once it is assigned. B) The code can compile and run fine. The second line assigns a new array to list. C) The code has syntax errors because you cannot assign a different size array to list. D) The code has runtime errors because the variable list cannot be changed once it is assigned. 83) Suppose a method p has the following heading:public static int[][] p()what return statement may be used in p()? A) return new int[]{1, 2, 3}; B) return 1; C) return {1, 2, 3}; D) return new int[][]{{1, 2, 3}, {2, 4, 5}}; E) return int[]{1, 2, 3}; 84) Analyze the following code: int[][] matrix = new int[5][5]; for (int column = 0; column < matrix[4].length; column++) matrix[4][column] = 10; A) After the loop, the last row of matrix 10, 10, 10, 10, 10. B) After the loop, the last column of matrix 10, 10, 10, 10, 10. C) After the loop, the last row of matrix 0, 0, 0, 0, 10. D) After the loop, the last row of matrix 0, 0, 0, 0, 0. 85) In the following code, what is the printout for list2?class Test { public static void main(string[] args) { int[] list1 = {3, 2, 1}; int[] list2 = {1, 2, 3}; list2 = list1; list1[0] = 0; list1[1] = 1; list2[2] = 2; for (int i = list2.length - 1; i >= 0; i--) System.out.print(list2[i] + ""); }} A) 013. B) 321 C) 123 D) 012 E) 210 83) 84) 85) 86) Which of the statements regarding the super keyword is incorrect? 86) A) You cannot invoke a method in superclass's parent class. B) You can use super to invoke a super class method. C) You can use super.super.p to invoke a method in superclass's parent class. D) You can use super to invoke a super class constructor. 87) Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following method will cause the list to become [Beijing, Chicago, Singapore]? A) x.add(2, "Chicago") B) x.add(0, "Chicago") C) x.add("chicago") D) x.add(1, "Chicago") 88) You can use the operator == to check whether two variables refer to the same object, and use the equals() method to check the equality of contents of the two objects. 87) 88)
89) What is the value of balance after the following code is executed?int balance = 10;while (balance >= 1) { if (balance < 9) break; balance = balance - 9;} 89) A) 1 B) -1 C) 0 D) 2 90) What is i after the following for loop?int y = 0;for (int i = 0; i<10; ++i) { y += i; } 90) A) 10 B) undefined C) 11 D) 9 91) Which of the following expression yields an integer between 0 and 100, inclusive? A) (int)(math.random() * 100) B) (int)(math.random() * 100 + 1) C) (int)(math.random() * 101) D) (int)(math.random() * 100) + 1 92) The actual parameters of a method must match the formal parameters in type, order, and number. 93) The signature of a method consists of the method name, parameter list and return type 94) 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}; C) return 1; D) return {1, 2, 3}; 91) 92) 93) 94) 95) Which of the following are Java keywords? 95) A) instanceof B) casting C) cast D) instanceof 96) What is the number of iterations in the following loop: for (int i = 1; i < n; i++) { // iteration } A) 2*n B) n + 1 C) n D) n - 1 97) The modifiers and return value type do not contribute toward distinguishing one method from another, only the parameter list. 96) 97) 98) Suppose int[] numbers = new int[10], what is numbers[0]? 98) A) undefined B) 0 C) 10 D) null 99) can search in an unsorted list. 99) A) Binary search B) Linear search 100) What the output of the following code:for ( ; ; ) System.out.println("Welcome to Java"); 100)
A) prints out Welcome to Java two times. B) prints out Welcome to Java one time. C) prints out Welcome to Java forever. D) does not print anything.
1) A 2) A 3) A 4) C 5) A, D 6) A 7) C 8) A 9) B 10) D 11) D 12) B 13) C 14) E 15) A 16) C 17) C 18) B 19) B 20) A 21) E 22) E 23) D 24) A 25) B 26) C 27) B 28) A, B 29) D 30) C 31) B 32) D 33) A, D 34) B 35) C 36) B 37) B 38) D 39) B 40) D 41) A 42) A 43) C 44) C 45) B 46) D 47) A 48) D 49) C 50) A 51) B
52) A 53) A 54) B 55) C 56) A, D 57) A 58) A 59) B 60) A 61) A, D 62) A, D 63) A 64) A, B, C, D, E 65) B 66) B 67) C, D 68) A, C 69) D 70) A 71) A 72) A 73) A 74) B 75) B 76) B, D 77) D 78) A 79) B 80) D 81) C, D 82) B 83) D 84) A 85) E 86) C 87) D 88) A 89) A 90) B 91) C 92) A 93) B 94) B 95) D 96) D 97) A 98) B 99) B 100) C