java考试题带答案.docx

上传人:豆**** 文档编号:29892906 上传时间:2022-08-02 格式:DOCX 页数:13 大小:24.08KB
返回 下载 相关 举报
java考试题带答案.docx_第1页
第1页 / 共13页
java考试题带答案.docx_第2页
第2页 / 共13页
点击查看更多>>
资源描述

《java考试题带答案.docx》由会员分享,可在线阅读,更多相关《java考试题带答案.docx(13页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、第一章复习题1.4 Source program means the original program written in a high-level language. A compiler is a program that translates source program into executable code.1.7 Developed by a team led by James Gosling at Sun Microsystems in 1991. Originally called Oak, it became Java in 1995 when it was redesi

2、gned for developing Internet applications.1.11 Keywords have specific meaning to the compiler and cannot be used for other purposes in the program such as variables or method names. Examples of keywords are class, static, and void.1.12 Java source code is case sensitive. Java keywords are always in

3、lowercase.1.13 The source file extension is .java and the bytecode file extension is .class.1.16 public class Welcome public static void main(String args) System.out.println(morning); System.out.println(afternoon); 1.17 Line 2. Main should be main. Line 2. static is missing.Line 3: Welcome to Java!

4、should be enclosed inside double quotation marks.Line 5: The last ) should be . 1.18 javac is the JDK command to compile a program. java is the JDK command to run a program.1.19 Java interpreter cannot find the .class file. Make sure you placed the .class in the right place, and invoked java command

5、 with appropriate package name.1.20 The class does not have a main method, or the signature of the main method is incorrect.编程题1-6public class Exercise1_6 public static void main(String args) System.out.println(1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9); 1-7public class Exercise1_7 public static void main(S

6、tring args) System.out.println(4 * (1 - 1.0 / 3 + 1.0 / 5 - 1.0 / 7 + 1.0 / 9 - 1.0 / 11 + 1.0 / 13); 第二章复习题2.1 Valid identifiers: applet, Applet, $4, apps, x, y, radiusInvalid identifiers: a+, -a, 4#R, #44, class, public, intKeywords: class, public, int2.14 Line 2: Missing static for the main metho

7、d. Line 2: string should be String. Line 3: i is defined but not initialized before it is used in Line 5. Line 4: k is an int, cannot assign a double value to k. Lines 7-8: The string cannot be broken into two lines.2.24 bc-22.25 System.out.println(1 + 1); = 11System.out.println(1 + 1); = 50 (since

8、the Unicode for 1 is 49System.out.println(1 + 1 + 1); = 111System.out.println(1 + (1 + 1); = 12System.out.println(1 + 1 + 1); = 51编程练习 2.2import java.util.Scanner;public class Exercise2_2 public static void main(String args) Scanner input = new Scanner(System.in); / Enter radius of the cylinder Syst

9、em.out.print(Enter radius of the cylinder: ); double radius = input.nextDouble(); / Enter length of the cylinder System.out.print(Enter length of the cylinder: ); double length = input.nextDouble(); double volume = radius * radius * 3.14159 * length; System.out.println(The volume of the cylinder is

10、+ volume); 2.6 / Exercise2_6.java: Summarize all digits in an integer 1000public class Exercise2_6 / Main method public static void main(String args) java.util.Scanner input = new java.util.Scanner(System.in); / Read a number System.out.print(Enter an integer between 0 and 1000: ); int number = inpu

11、t.nextInt(); / Find all digits in number int lastDigit = number % 10; int remainingNumber = number / 10; int secondLastDigit = remainingNumber % 10; remainingNumber = remainingNumber / 10; int thirdLastDigit = remainingNumber % 10; / Obtain the sum of all digits int sum = lastDigit + secondLastDigit

12、 + thirdLastDigit; / Display results System.out.println(The sum of all digits in + number + is + sum); 第三章 复习题3.12 Consider score is 90, what will be the grade?3.26 Consider score is 90, what will be the grade?编程练习3.1import java.util.Scanner;public class Exercise3_1 public static void main(String ar

13、gs) Scanner input = new Scanner(System.in); System.out.print(Enter a, b, c: ); double a = input.nextDouble(); double b = input.nextDouble(); double c = input.nextDouble(); double discriminant = b * b - 4 * a * c; if (discriminant 0) double r1 = (-b + Math.pow(discriminant, 0.5) / (2 * a); double r2

14、= (-b - Math.pow(discriminant, 0.5) / (2 * a); System.out.println(The roots are + r1 + and + r2); 第四章复习题4.1 count 100 always true at Point A. count 100 always false at Point C. count 100 is sometimes true or sometimes false at Point B.4.3 (a) Infinite number of times.(b) Infinite number of times.(c)

15、 The loop body is executed nine times. The printout is 3, 5, 7, 9 on separate lines.4.7 max is 5number 04.20 After the continue statement is executed, the statementj+will be executed. The output of this fragment is1212234.21 Line 2: missing static. Line 3: The semicolon (;) at the end of the for loo

16、p heading should be removed.Line 4: sum not defined. Line 6: the semicolon (;) at the end of the if statement should be removed. Line 6: j not defined. Line 7: Missing a semicolon for the first println statement. Line 11: The semicolon (;) at the end of the while heading should be removed. Line 18:

17、Missing a semicolon at the end of the do-while loop.4.22 (A) compile error: i is not initialized.(B) Line 3: The ; at the end of for loop should be removed.for (int i = 0; i 0) countPositive+; else if (num 0) countNegative+; total += num; count+; / Read the next number num = input.nextInt(); if (cou

18、nt = 0) System.out.println(You didnt enter any number); else System.out.println(The number of positives is + countPositive); System.out.println(The number of negatives is + countNegative); System.out.println(The total is + total); System.out.println(The average is + total * 1.0 / count); 4.10public

19、class Exercise4_10 public static void main(String args) int count = 1; for (int i = 100; i = 1000; i+) if (i % 5 = 0 & i % 6 = 0) System.out.print(count+ % 10 != 0) ? i + : i + n); 4.15 public class Exercise4_15 public static void main(String args) java.util.Scanner input = new java.util.Scanner(Sys

20、tem.in); / Enter n1 System.out.print( Enter the first number: ); int n1 = input.nextInt(); / Enter n2 System.out.print( Enter the second number: ); int n2 = input.nextInt(); int d = (n1 = 1; d-) if (n1 % d = 0) & (n2 % d = 0) break; System.out.println(GCD of + n1 + and + n2 + is + d); 4.20 public cl

21、ass Exercise4_20 / Main method public static void main(String args) int count = 1; / Count the number of prime numbers int number = 2; / A number to be tested for primeness boolean isPrime = true; / If the current number is prime? System.out.println(The prime numbers from 2 to 1000 are n); / Repeate

22、dly test if a new number is prime while (number = 1000) / Assume the number is prime isPrime = true; / Set isPrime to false, if the number is prime for (int divisor = 2; divisor = number / 2; divisor+) if (number % divisor = 0) / If true, the number is prime isPrime = false; break; / Exit the for lo

23、op / Print the prime number and increase the count if (isPrime) if (count%8 = 0) / Print the number and advance to the new line System.out.println(number); else System.out.print(number + ); count+; / Increase the count / Check if the next number is prime number+; 4.26public class Exercise4_26 public

24、 static void main(String args) double e = 1; double item = 1; for (int i = 1; i = 100000; i+) item = item / i; e += item; if (i = 10000 | i = 20000 | i = 30000 | i = 40000 | i = 50000 | i = 60000 | i = 70000 | i = 80000 | i = 90000 | i = 100000) System.out.println(The e is + e + for i = + i); 第五章 复习

25、题5.8 Line 2: method1 is not defined correctly. It does not have a return type or void.Line 2: type int should be declared for parameter m.Line 7: parameter type for n should be double to match method2(3.4).Line 11: if (n0) should be removed in method, otherwise a compile error is reported.5.15 Line

26、7: int n = 1 is wrong since n is already declared in the method signature.编程练习 5.2 / Exercise5_2.java: Create a method for summarizing digits in an intpublic class Exercise5_2 public static void main(String args) java.util.Scanner input = new java.util.Scanner(System.in); System.out.print(Enter a nu

27、mber: ); int value = input.nextInt(); System.out.println(The sum of digits for + value + is + sumDigits(value); public static int sumDigits(long n) int temp = (int)Math.abs(n); int sum = 0; while (temp != 0) int remainder = temp % 10; sum += remainder; temp = temp / 10; return sum; 第六章复习题6.1 See the

28、 section Declaring and Creating Arrays.6.2 You access an array using its index. 6.3 No memory is allocated when an array is declared. The memory is allocated when creating the array.x is 60The size of numbers is 306.6 The array index type is int and its lowest index is 0. a26.8 A runtime exception o

29、ccurs.6.9 Line 3: the array declaration is wrong. It should be double. The array needs to be created before its been used. e.g. new double10Line 5: The semicolon (;) at the end of the for loop heading should be removed. Line 5: r.length() should be r.length.Line 6: random should be random()Line 6: r

30、(i) should be ri.编程练习题 6.1 二维数组实现杨辉三角 冒泡法排序import java.util.Scanner;public class Exercise6_1 /* Main method */ public static void main(String args) / Create a Scanner Scanner input = new Scanner(System.in); / Get number of students System.out.print(Enter number of students: ); int numberOfStudents =

31、 input.nextInt(); int scores = new intnumberOfStudents; / Array scores int best = 0; / The best score char grade; / The grade / Read scores and find the best score System.out.print(Enter + numberOfStudents + scores: ); for (int i = 0; i best) best = scoresi; / Declare and initialize output string St

32、ring output = ; / Assign and display grades for (int i = 0; i = best - 10) grade = A; else if (scoresi = best - 20) grade = B; else if (scoresi = best - 30) grade = C; else if (scoresi = best - 40) grade = D; else grade = F; output += Student + i + score is + scoresi + and grade is + grade + n; / Di

33、splay the result System.out.println(output); 第八章复习题8.2. Constructors are special kinds of methods that are called when creating an object using the new operator. Constructors do not have a return typenot even void.8.3. An array is an object. The default value for the elements of an array is 0 for nu

34、meric, false for boolean, u0000 for char, null for object element type.8.4. (a) There is such constructor ShowErrors(int) in the ShowErrors class.The ShowErrors class in the book has a default constructor. It is actually same as public class ShowErrors public static void main(String args) ShowErrors

35、 t = new ShowErrors(5); public ShowErrors () On Line 3, new ShowErrors(5) attempts to create an instance using a constructor ShowErrors(int), but the ShowErrors class does not have such a constructor. That is an error. (b) x() is not a method in the ShowErrors class.The ShowErrors class in the book

36、has a default constructor. It is actually same as public class ShowErrors public static void main(String args) ShowErrors t = new ShowErrors(); t.x(); public ShowErrors () On Line 4, t.x() is invoked, but the ShowErrors class does not have the method named x(). That is an error.(c) The program compi

37、les fine, but it has a runtime error because variable c is null when the println statement is executed.(d) new C(5.0) does not match any constructors in class C. The program has a compilation error because class C does not have a constructor with a double argument.8.5. The program does not compile b

38、ecause new A() is used in class Test, but class A does not have a default constructor. See the second NOTE in the Section, “Constructors.”8.6. false8.20 (Line 4 prints null since dates0 is null. Line 5 causes a NullPointerException since it invokes toString() method from the null reference.)第九章复习题9.

39、1s1 = s2 = trues2 = s3 = falses1.equals(s2) = trues2.equals(s3) = truepareTo(s2) = 0pareTo(s3) = 0s1 = s4 = trues1.charAt(0) = Ws1.indexOf(j) = -1s1.indexOf(to) = 8s1.lastIndexOf(a) = 14s1.lastIndexOf(o, 15) = 9s1.length() = 16s1.substring(5) = me to Java!s1.substring(5, 11) = me tos1.startsWith(Wel

40、) = trues1.endsWith(Java) = trues1.toLowerCase() = welcome to java!s1.toUpperCase()= WELCOME TO JAVA! Welcome .trim() = Welcomes1.replace(o, T) = WelcTme tT Java!s1.replaceAll(o, T) = WelcTme tT Java!s1.replaceFirst(o, T) = WelcTme tT Java!s1.toCharArray() returns an array of characters consisting of W, e, l, c, o, m, e, , t, o, ,

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 小学资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁