面向对象编程基础习题及答案_第1页
面向对象编程基础习题及答案_第2页
面向对象编程基础习题及答案_第3页
面向对象编程基础习题及答案_第4页
面向对象编程基础习题及答案_第5页
已阅读5页,还剩54页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、面向对象编程基础习题及答案PartI. Choice Questions (1pt for each question.1. To add a to b and store result in b, you write (Note: Java is case-sensitiveA. b += a;B. a = b + a;C. b = A + b;D. a += b;2. To declare a constant PI, you writeA. final static PI = 3.14159;B. final float PI = 3.14159;C. static double PI

2、= 3.14159;D. final double PI = 3.14159;3. To improve readability and maintainability, you should declare _ instead of using literal values such as 3.14159.A. variablesB. methodsC. constantsD. classes4. To declare an int variable x with initial value 200, you writeA. int x = 200L;B. int x = 200l;C. i

3、nt x = 200;D. int x = 200.0;5. To assign a double variable d to an int variable x, you writeA. x = (longdB. x = (intd;C. x = d;D. x = (floatd;6. In Java, the word true is _.A. a Java keywordB. a Boolean literalC. same as value 1D. same as value 07. Which of the Boolean expressions below has incorrec

4、t syntax?A. (true && (3 > 4B. !(x > 0 && (x > 0C. (x > 0 | (x < 0D. (x != 0 | (x = 08. Which of the following is the correct expression that evaluates to true if the number x is between 1 and 100 or the number is negative?A. 1 < x < 100 && x < 0B. (x &

5、lt; 100 && (x > 1 | (x < 0C. (x < 100 && (x > 1 && (x < 0D. (1 > x > 100 | (x < 09. Which of the following is the correct expression of character a?A. 'a'B. "a"C. '000a'D. None of the above.10. Which of the following statement

6、 prints smithexam1test.txt?A. System.out.println("smithexam1test.txt"B. System.out.println("smithexam1test.txt"C. System.out.println("smith"exam1"test.txt"D. System.out.println("smith"exam1"test.txt"11. Suppose i is an int type variable. Wh

7、ich of the following statements display the character whose Unicode is stored in variable i?A. System.out.println(i;B. System.out.println(chari;C. System.out.println(inti;D. System.out.println(i + " "12. The Unicode of 'a' is 97. What is the Unicode for 'c'?A. 96B. 97C. 98D

8、. 9913. Which of the following is a constant, according to Java naming conventions?A. MAX_VALUEB. TestC. readD. ReadInt14. Which of the following assignment statements is illegal?A. float f = -34;B. int t = 23;C. short s = 10;D. float f = 34.0;15. A Java statement ends with a _.A. comma (,B. semicol

9、on (;C. period (.D. closing brace16. The assignment operator in Java is _.A. :=B. =C. = =D. <-17. Suppose m and r are integers. What is the correct Java expression for m / r2 to obtain a floating point result value? (r2 denotes r raised to the power of 2.A. m / r * rB. m / (r * rC. 1.0 * m / r *

10、rD. 1.0 * m / (r * r18. Which of these data types requires the least amount of memory?A. floatB. doubleC. shortD. byte19. Which of the following operators has the highest precedence?A. castingB. +C. *D. /20. An int variable can hold _.A. 'x'B. 93C. 98.3D. trueE. a and b21. Which of the follo

11、wing assignment statements is correct to assign character 5 to c?A. char c = '5'B. char c = 5;C. char c = "5"D. char c = "344"22. Note that the Unicode for character A is 65. The expression "A" + 1 evaluates to _.A. 66B. BC. A1D. Illegal expression23. The not eq

12、ual comparison operator in Java is _.A. <>B. !=C. != =D. =24. If you attempt to add an int, a byte, a long, and a double, the result will be a _ value.A. byteB. int;C. long;D. double;25. What is the value of (double5/2?A. 2;B. 2.5;C. 3;D. 2.0;26. What is the value of (double(5/2?A. 2;B. 2.5;C.

13、 3;D. 2.0;27. Analyze the following code.public class Test public static void main(String args int month = 09;System.out.println("month is " + month;A. The program displays month is 09B. The program displays month is 9C. The program displays month is 9.0D. The program has a syntax error, b

14、ecause 09 is an incorrect literal value.28. To assign a double variable d to a float variable x, you writeA. x = (longdB. x = (intd;C. x = d;D. x = (floatd;29. what is y displayed in the following code?public class Test1 public static void main(String args int x = 1;int y = x = x + 1;System.out.prin

15、tln("y is " + y;A. y is 0.B. y is 1 because x is assigned to y first.C. y is 2 because x + 1 is assigned to x and then x is assignedto 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 a

16、t runtime, then the program suffers _.A. a syntax errorB. a runtime errorC. a logic error31. Suppose x=0 and y=0 what is x after evaluating the expression (y > 0 & (1 > x+.A. 0B. -1C. 132. Suppose x=0 and y=0 what is x after evaluating the expression (y > 0 && (1 > x+.A. 0B.

17、-1C. 133. 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 namedA. Test.classB. Test.docC. Test.txtD. Test.javaE. Any name with extension .java34. The command to compile a class in the file Test.java isA. java TestB. ja

18、va Test.javaC. javac Test.javaD. javac TestE. JAVAC Test.java35. Which JDK command is correct to run a Java application in ByteCode.class?A. java ByteCodeB. java ByteCode.classC. javac ByteCode.javaD. javac ByteCodeE. JAVAC ByteCode36. What is 1 % 2?A. 0B. 1C. 237. What is "Welcome" + 1 +

19、1*2?A. Welcome11*2B. Welcome4C. Welcome12D. Welcome338. What is the printout of the following code:double x = 10.1;int y = (intx;System.out.println("x is " + x + " and y is " + y;A. x is 10 and y is 10B. x is 10.0 and y is 10.0C. x is 11 and y is 11D. x is 10.1 and y is 10E. x is

20、 10.1 and y is 10.039. 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

21、.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 > 0if (y > 0System.out.println("x > 0 and y > 0"else if (z > 0System.out.println("x < 0 and z > 0&qu

22、ot;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("It is even!"A. The program has a syntax error.B. The program has a runtime error.C. The program runs fine, bu

23、t displays nothing.D. The program runs fine and displays It is even!.42. Analyze the following code:/ Enter an integerString numString = JOptionPane.showInputDialog(null,"Enter a number:","Exam Input",JOptionPane.QUESTION_MESSAGE;int number = Integer.parseInt(numString;if (number

24、 <= 0System.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 ne

25、gative.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,"Enter an integer", "Input",JOptionPane.QUESTION_MESSAGE;j = Integer.parseInt(s;i = (i + 4;A. The program

26、 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;D. The program compiles and runs fine

27、.44. What is the output of the following code: (Please indent the statement correctly first. int x = 9, y = 8, z = 7;if (x > 9if (y > 8System.out.println("x > 9 and y > 8"else if (z >= 7System.out.println("x <= 9 and z >= 7"elseSystem.out.println("x <

28、= 9 and z < 7"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,"Enter a number:","Exam Input",JOptionPane.QUESTION_MESSAGE;int number = Int

29、eger.parseInt(numString;if (number <= 0System.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

30、 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 = -1A. 1B. -1C. 0D. None of the above47. What is the printout of the following switch statement?char ch = 'a'switch (ch case 'a':case 'A':

31、System.out.print(ch; break;case 'b':case 'B':System.out.print(ch; break;case 'c':case 'C':System.out.print(ch; break;case 'd':case 'D':System.out.print(ch;A. abcdB. aC. aaD. abE. abc48. Analyze the following x = 0;int y = (x<100 & (x>

32、;0 ? 1: -1;A. The code has a syntax error because & must be &&.B. y becomes 1 after the code is executed.C. y becomes -1 after the code is executed.D. None of the above.49. Which of the following is not a valid boolean expression.A. (1 < x < 100B. (x = 1 | (x != 1C. a, b are all co

33、rrectD. a, b are all wrong50. Which of the following expression is equivalent to (x > 1.A. x >= 1B. !(x <= 1C. !(x = 1D. !(x < 1E. None of the above51. Analyze the following two code fragments.(iint x = 5;if (0 < x && (x < 100System.out.println("x is between 1 and 100&

34、quot;(iiint x = 5;if (0 < x && x < 100System.out.println("x is between 1 and 100"A. The first fragment has a syntax error.B. The second fragment has a syntax error.C. Both fragments produce the same output.D. Both fragments compile, but produce different result.E. None of the

35、 above.52. Analyze the following fragment.double x = 0;double d = 1;switch (d + 4 case 5: x+;case 6: -x;A. The required break keyword is missing in the switch statement.B. The required default keyword is missing in the switch statementC. The switch control variable cannot be doubleD. a, b, and c are

36、 all correct.E. a, b, and c are all incorrect.53. Analyze the following x = 0;if (x > 0;System.out.println("x"A. The symbol x is always printed.B. The value of variable x is always printed.C. Nothing is printed because x > 0 is false.D. None of the above.54. Which of the loo

37、p statements always have their body executed at least once.A. The while loopB. The do-while loopC. The for loopD. None of the above55. Analyze the following count = 0;while (count < 100 / Point ASystem.out.println("Welcome to Java!"count+;/ Point B/ Point CA. count < 100 is

38、always true at Point AB. count < 100 is always true at Point BC. count < 100 is always false at Point BD. count < 100 is always true at Point C56. What is the value in count after the following loop is executed?int count = 0;do System.out.println("Welcome to Java" while (count+ &l

39、t; 9;System.out.println(count;A. 8B. 9C. 10D. 1157. What is the output for y?int y = 0;for (int i = 0; i<10; +i y += i;System.out.pri ntln(y;A. 10B. 11C. 12D. 4558. Analyze the following x = 1;while (0 < x & (x < 100System.out.println(x+;A. The loop runs for ever.B. The code do

40、es not compile because the loop body is not in the braces.C. The code does not compile because (0 < x & (x < 100 is not enclosed in a pair of parentheses.D. The number 1 to 99 are displayed.E. The number 2 to 100 are displayed.59. Analyze the following code.double sum = 0;for (double d = 0

41、; d < 10; sum += sum + d d += 0.1;A. The program has a syntax error because the adjustment statement is incorrect in the for loop.B. The program has a syntax error because the control variable in the for loop cannot be of the double type.C. The program compiles but does not stop because d would a

42、lways be less than 10.D. The program compiles and runs fine.60. Analyze the following fragment:double sum = 0;double d = 0;while (d != 10.0 d += 0.1;sum += sum + d;A. The program does not compile because sum and d are declareddouble, but assigned with integer value 0.B. The program never stops becau

43、se d is always 0.1 inside the loop.C. The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers.D. After the loop, sum is 0 + 0.1 + 0.2 + 0.3 + . + 1.9 61. What is y after the following for loop statement is executed?int y = 0;fo

44、r (int i = 0; i < 10; +i y += 1;A. 9B. 10C. 11D. 12E. None of the above.62. What balance after the following code is executed?int balance = 10;while (balance >= 1 if (balance < 9 continue;balance = balance - 9;A. -1B. 0C. 1D. 2E. The loop does not end63. What is the value of balance after t

45、he following code is executed?int balance = 10;while (balance >= 1 if (balance < 9 break;balance = balance - 9;A. 1B. 0C. 1D. 2E. None of the above64. What is x after evaluatingx = (2 > 3 ? 2 : 3;A. 2B. 3C. 4D. None of the above65. The signature of a method consists of _.A. method nameB. me

46、thod name and parameter listC. return type, method name, and parameter listD. parameter list66. A variable that is declared inside a method is called _ variable.A. a staticB. an instanceC. a localD. a globalE. a class67. Each Java class must contain a main method.A. trueB. false68. What is (intMath.

47、random(?A. 0B. 1C. both 0 and 1 are possibleD. None of the above69. 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;A. The program has a syntax error bec

48、ause the two methods m have the same signature.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 once.D. The program runs and prints 2 twice.70. Analyze the following code:class Test public static void main(S

49、tring args System.out.println(xMethod(double5;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 int followed by 5.B. The program displays long followed by 5.C. The program r

50、uns fine but displays things other than given in A and B.D. The program does not compile.E. None of the above.71. Analyze the following code:public class Test public static void main(String args System.out.println(xMethod(5, 500L;public static int xMethod(int n, long l System.out.println("int,

51、long"return n;public static long xMethod(long n, long l System.out.println("long, long"return n;A. The program displays int, long followed by 5.B. The program displays long, long followed by 5.C. The program runs fine but displays things other than given in a and b.D. The program does

52、 not compile because the compiler c annot distinguish which xmethod to invoke.72. Y ou may have a return statement in a void method.A. trueB. falseNote: Questions 73 75 are based on the following method:static void nPrint(String message, int n while (n > 0 System.out.print(message;n-;73. What is

53、the printout of the call nPrint("a", 4?A. aaaaaB. aaaaC. aaaD. invalid call74. What is k after invoking nPrint(“A message”, k+1?int k = 2;nPrint("A message", k+1;A. 0B. 1C. 2D. 3E. None of the above.75. Analyze the following code.public static void main(String args for (int i = 1

54、; i < 10; i+ int k = 3;nPrint("A message", k;System.out.println("k is " + k;A. The code has a syntax error because k is not defined in System.out.println("k is " + k.B. The code prints k is 0.C. The code prints k is 1.D. The code prints k is 2.E. The code prints k is

55、 3.76. Analyze the following code.public class Test 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 syntax error because xMethod does not return a value.B. The code has a syntax error because xMethod is not decl

56、ared static.C. The code prints n is 1.D. The code prints n is 2.E. The code prints n is 3.77. Which of the following is not an advantage of using methods.A. Using methods makes program run faster.B. Using methods makes reusing code easier.C. Using methods makes programs easier to read.D. Using metho

57、ds hides detailed implementation from the clients.78. Which of the following is a possible output for 50 * Math.random(?A. 0B. 50C. 100D. 500E. A and B.79. Which of the following method results in 8.0?A. Math.round(8.5B. Math.rint(8.5C. Math.ceil(8.5D. Math.floor(8.5E. b and d.80. Which of the following method returns the sine of 90 degree?A. Math.sine(90B. Math.sin(90C. Math.sin(PID. Math.sin(Math.toRadian(90E. Math.sin(Math.PI81. Suppose array a is int a = 1, 2, 3, what is a0 - a2?A. 1B. 2C. 3D. None of

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论