java考试题带答案.docx
《java考试题带答案.docx》由会员分享,可在线阅读,更多相关《java考试题带答案.docx(13页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 考试题 答案
限制150内