面向对象编程基础习题及答案

public static void xMethod(int[] x, int length) { for (int i = 0; i < length; i++) System.out.print(\ }

}

A. The program displays 0 1 2 3 4.

B. The program displays 0 1 2 3 4 and then raises a runtime

exception. C. The program displays 0 1 2 3 4 5.

D. The program displays 0 1 2 3 4 5 and then raises a runtime

exception.

92. Analyze the following code:

public class Test {

public static void main(String[] args) { int[] x = new int[5];

for (int i = 0; i < x.length; i++) x[i] = i;

System.out.println(x[i]); }

}

A. The program displays 0 1 2 3 4. B. The program displays 4.

C. The program has a runtime error because the last statement in

the main method causes ArrayIndexOutOfBounds exception. D. The program has syntax error because i is not defined in the

last statement in the main method. 93. Assume

double[]

scores

=

{1,

2,

3,

4,

5},

what

value

does

java.util.Arrays.binarySearch(scores, 3.5) return? A. 0 B. -4 C. 3 D. -3 E. 4

94. When you return an array from a method, the method returns __________.

A. a copy of the array

B. a copy of the first element

C. the reference of the array D. the length of the array

第 21 页 共 34 页

PartII. Fill blanks (1pt for each blank).

1. One byte has ________ bits. // 8 or eight

2. Computer can execute the code in ________ language. // machine

3. ________ contains predefined classes and interfaces for developing Java programs. // JAVA

API 4. ________ consists of a set of separate programs for developing and testing Java programs,

each of which is invoked from a command line. // JAVA JDK

5. ________ provide an integrated development environment (IDE) for rapidly developing Java

programs. Editing, compiling, building, debugging, and online help are integrated in one

graphical user interface. // JAVA IDE

6. Java compiler translates Java source code into ________. // JAVA bytecode 7. ________ is a software that interprets Java bytecode. // Java virtual machine 8. The extension name of a Java source code file is ________. // .java 9. The extension name of a Java bytecode file is________. // .class

10. To declare a constant MAX_LENGTH inside a method with value 99.98, you write

________. // final double MAX_LENGTH = 99.98; 11. To declare an int variable number with initial value 2, you write ________. // int number = 2; 12. -24 % 5 is ________. // -4 13. -24 % -5 is ________. // -4

14. A Java character is stored in ________ bytes. // 2 or two 15. The printout of System.out.println('z' - 'a') is ________. // 25

16. The expression \ // Java15

17. The expression (int)(3.1415926 * 100) / 100.0 evaluates to _________. // 3.14

18. If a program compiles fine, but it produces incorrect result, then the program suffers a

________ error. // logic 19. Suppose your method does not return any value, the keyword ________ should be used as a

return type. // void 20. Each time a method is invoked, the system stores parameters and local variables in an area of

memory, known as a ________, which stores elements in last-in first-out fashion. // stack 21. The JVM stores the array in an area of memory, called a ________, which is used for

dynamic memory allocation where blocks of memory are allocated and freed in an arbitrary order. // heap

22. When you invoke a method with a parameter, the value of the argument is passed to the

parameter. This is referred to as pass by ________ . // value 23. (char)('a' + Math.random() * ('z' - 'a' + 1)) returns a random character between ?a? and

________ . // ?z? 24. The representation of the third element in an array called a is ________. // a[2]

PartIII. Correct errors in the following code (2pts for each error).

1.

public class Test {

public void Main(String[] args) { ( 1 ) /* public static void main(String[] args) { */

第 22 页 共 34 页

i = 0; ( 2 ) /* int i = 0; */ int j = i + 1;

int k = 5.5; ( 3 ) /* int k = (int)5.5; */ /* int k = 5; */

/* double k = 5.5; */

System.out.println(\ } }

2.

public class Test

{

public static void main(String[] args) {

int j = 0;

for (int i = 0; i < 10; i++); ( 4 ) /* for (int i = 0; i < 10; i++) */ {

if (j > i) then ( 5 ) /* if (j > i) */

j++ ( 6 ) /* j++; */ else j--;

}

System.out.println(\ } }

3.

public class Test {

public static void main(String[] args) {

double[4] list = {3, 4, 5, 6.3}; ( 7 ) /* double[] list = {3, 4, 5, 6.3}; */ double sum = xMethod(list);

System.out.println(\ }

public static double xMethod(int[] list) { ( 8 ) /*public static double xMethod(double[] list) { */ double sum = 0;

for (int i=0; i <= list.length; i++) { ( 9 ) /* for (int i=0; i < list.length; i++) { */

第 23 页 共 34 页

sum += list[i]; }

return sum; } }

4.

/** Use binary search to find the key in the list. Suppose the numbers

in the list are in ascending order. */

public static int binarySearch(int[] list, int key) { int low = 0;

int high = list.length - 1;

while (high > low) { ( 10 ) /* while (high >= low) { */

double mid = (low + high) / 2; ( 11 ) /* int mid = (low + high) / 2; */

if (key > list[mid]) ( 12 ) /* if (key < list[mid]) */ high = mid - 1;

else if (key == list[mid]) return mid; else

low = mid + 1; }

return –(low+1); }

PartIV. Show the output of the following code (5pts for each problem).

1.

public class Test {

public static void main(String[] args) { int x, y, i, j, k; float f; x = 1; y = 5 + x--; i = 1;

i += i + 3;

j = 25 / 2;

f = (float)((2 / 5) * j);

System.out.println(\

第 24 页 共 34 页

联系客服:779662525#qq.com(#替换为@)