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

to y.

D. The program has a syntax error since x is redeclared in the

statement int y = x = x + 1. 30. If a program compiles fine, but it terminates abnormally at runtime, then the program suffers

__________.

A. a syntax error B. a runtime error C. a logic error

31. Suppose x=0 and y=0 what is x after evaluating the expression (y > 0) & (1 > x++).

A. 0

B. -1 C. 1

32. Suppose x=0 and y=0 what is x after evaluating the expression (y > 0) && (1 > x++).

A. 0

B. -1 C. 1

33. Suppose you define a Java class as follows:

public class Test { }

In order to compile this class, the class should be stored in a file named

A. Test.class B. Test.doc C. Test.txt

D. Test.java

E. Any name with extension .java 34. The command to compile a class in the file Test.java is

A. java Test

B. java Test.java

C. javac Test.java D. javac Test E. JAVAC Test.java

35. Which JDK command is correct to run a Java application in ByteCode.class?

A. java ByteCode

B. java ByteCode.class C. javac ByteCode.java D. javac ByteCode E. JAVAC ByteCode 36. What is 1 % 2?

A. 0

第 5 页 共 34 页

B. 1 C. 2

37. What is \

A. Welcome11*2 B. Welcome4

C. Welcome12 D. Welcome3

38. What is the printout of the following code:

double x = 10.1; int y = (int)x;

System.out.println(\

A. x is 10 and y is 10 B. x is 10.0 and y is 10.0 C. x is 11 and y is 11 D. x is 10.1 and y is 10 E. x is 10.1 and y is 10.0

39. Which of the following code displays the area of a circle if the radius is positive.

A. if (radius != 0) System.out.println(radius * radius * Math.PI); B. if (radius >= 0) System.out.println(radius * radius * Math.PI);

C. if (radius > 0) System.out.println(radius * radius * Math.PI); D. if (radius <= 0) System.out.println(radius * radius * Math.PI); 40. Suppose x = 1, y = -1, and z = 1. What is the printout of the following statement?

if (x > 0) if (y > 0)

System.out.println(\else if (z > 0)

System.out.println(\

A. x > 0 and y > 0; B. x < 0 and z > 0; C. x < 0 and z < 0; D. no printout. 41. Analyze the following code:

boolean even = false; if (even = true) {

System.out.println(\}

A. The program has a syntax error.

第 6 页 共 34 页

B. The program has a runtime error.

C. The program runs fine, but displays nothing. D. The program runs fine and displays It is even!. 42. Analyze the following code:

// Enter an integer

String numString = JOptionPane.showInputDialog(null,

\\

JOptionPane.QUESTION_MESSAGE);

int number = Integer.parseInt(numString);

if (number <= 0)

System.out.println(number);

A. The if statement is wrong, because it does not have the else clause;

B. System.out.println(number); must be placed inside braces; C. If number is zero, number is displayed;

D. If number is positive, number is displayed.

E. number entered from the keyboard cannot be negative. 43. Analyze the following code.

import javax.swing.*;

public class ShowErrors {

public static void main(String[] args) { int i; int j;

String s = JOptionPane.showInputDialog(null, \ JOptionPane.QUESTION_MESSAGE); j = Integer.parseInt(s);

i = (i + 4); } }

A. The program cannot compile because j is not initialized. B. The program cannot compile because i does not have an initial

value when it is used in i = i + 4; C. The program compiles but has a runtime error because i deos

not have an initial value when it is used in i = i + 4;

第 7 页 共 34 页

D. The program compiles and runs fine.

44. What is the output of the following code: (Please indent the statement correctly first.)

int x = 9, y = 8, z = 7;

if (x > 9)

if (y > 8)

System.out.println(\

else if (z >= 7)

System.out.println(\else

System.out.println(\

A. x > 9 and y > 8; B. x <= 9 and z >= 7; C. x <= 9 and z < 7; D. None of the above. 45. Analyze the following code:

String numString = JOptionPane.showInputDialog(null,

\\

JOptionPane.QUESTION_MESSAGE);

int number = Integer.parseInt(numString); if (number <= 0)

System.out.println(number); System.out.println(number);

A. number is always printed out at least once; B. number is printed out twice if number is zero;

C. number is printed out twice if number is negative; D. number is printed out once if number is positive. E. All of the above.

46. What is y after the following switch statement?

int x = 0; int y = 0; switch (x + 1) {

case 0: y = 0; case 1: y = 1; default: y = -1 }

A. 1

第 8 页 共 34 页

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