王利国的 "Java 语言程序设计第 3 次作业 (2018)" 详细
作业结果详细
总分: 100
选择题得分: 60
1. 设有如下定义语句: String s1="My cat"; int m=s1.compareTo("My a cat"); 语句被执行后 m 的值为 ( )
- A.2
- B.1
- C.0
- D.-2
正确答案是: A
2. 语句 String s1=new String("Hello"); String s2=new String("Hello"); System.out.println(s1==s2); System.out.printlv(s1.equals(s2)); 执行后的输出结果是 ( )
- A.Hello false
- B.Hello true
- C.Hello Hello
- D.false true
正确答案是: D
3. 执行下列语句 int[] lx={2,3,4,5}; lx[3]=lx[3]==--lx[0]?++lx[1]:lx[2]--; 后, 数组 lx 的元素值分别为 ( )
- A.1,2,3,4
- B.1,3,3,3
- C.1,2,3,3
- D.1,3,3,4
正确答案是: D
4. 在一个应用程序中定义了数组 a:int[] a={1,2,3,4,5,6,7,8,9,10}, 为了打印输出数组 a 的最后一个数组元素, 下面正确的代码是 ( )
- A.System.out.println(a[10]);
- B.System.out.println(a[9]);
- C.System.out.println(a[8]);
- D.System.out.println(a[a.length]);
正确答案是: B
5. 设有定义语句 int a[]={36,72,99}; 则以下对此语句叙述错误的是 ( )
A. 该语句定义了一个名为 a 的一维数组;
B.a 数组有 3 个元素;
C. 数组中的每个元素是整型;
D.a 数组的元素的下标为 1~3;
正确答案是: D
6. 下面关于数组定义语句不正确的是 ( )
- A.float f[]=new{2.4f,3.5f,5.7f,7.9f};
- B.int a[]={1,2,3,4,5}
- C.double[]d=new double[10];
- D.int[]a2;
正确答案是: A
7. 下列语句序列执行后, c 的值是 ( ) int a=3,b=4,c=0; while ((a++)<(--b))++c;
- A.0
- B.1
- C.2
- D.3
正确答案是: A
8. 下列语句序列执行后, a 的值是 ( ) int a=1; for(int i=5;i>0;i-=2) a*=i;
- A.0
- B.1
- C.15
- D.60
正确答案是: C
9. 下列语句序列执行后, c 的值是 ( ) int a=10,b=18,c=30; switch(b-a){ case 8 : c++; case 9 : c+=2; case 10 : c+=3; default :c/=b; }
- A.31
- B.32
- C.2
- D.33
正确答案是: C
10. 设 a,b 为 int 型变量, c,d 为 fload 类型变量, ch 为 char 类型变量, 且所有变量均已赋值, 则下列正确的 switch 语句是 ( )
- A.switch(a+b);{...}
- B.switch(ch+1){...}
- C.switch ch {...}
- D.switch(c+d){...}
正确答案是: B
11. 下列语句序列执行后, c 的值是 ( ) int a=4,b=5,c=9,d=6; if (a>b||c<d) c--; else c++;
- A.6
- B.10
- C.8
- D.9
正确答案是: B
12. 下列语句序列执行后, c 的值是 ( ) int a=6,b=3,c=5; if (a==b) c+=a;else c=++a*c;
- A.15
- B.25
- C.35
- D.45
正确答案是: C
13. 下列语句序列执行后, c 变量的值为 ( ) int a=2,b=4,c=5; if (a<--b) c*=a;
- A.5
- B.20
- C.15
- D.10
正确答案是: D
14. 下列语句序列执行后, ch1 变量中的值为 ( ) char ch1='A',ch2='B'; if (ch1+2<ch2)++ch1;
- A.'A'
- B.'B'
- C.A
- D.B
正确答案是: A
15. 以下选项中, 合法的赋值语句是 ( )
- A.++m!=n--;
- B.++m;
- C.m=m+1=5;
- D.m==1;
正确答案是: B
16. 设有定义 "int a=22;long b=56;", 下面赋值不正确的语句是 ( )
- A.a=b;
- B.b=(long)a;
- C.a=(int)b;
- D.b=a;
正确答案是: A
17. 下面选项中,( ) 是正确的输出结果 int m=2,n=1; m+=m-=n; System.out.println("m="+m);
- A.m=1
- B.m=2
- C.m=3
- D.m=4
正确答案是: C
18. 下列语句序列执行后的结果为 ( ) int a=10,b=4,c=20,d=6; System.out.println(a++*b+c*--d);
- A.144
- B.140
- C.28
D. 不能执行
正确答案是: B
19. 假设以下选项中的变量都已经正确定义, 则不合法的表达式是 ( )
- A.a>4==6<1;
- B.'n'-3;
- C.'a'=8;
- D.'A'%6
正确答案是: C
20. 假设 a 为已经声明并以赋初值的 int 类型变量, 则对于 a 的赋值语句正确的是 ( )
- A.int a=6;
- B.a==3;
- C.a=3.2f;
- D.a+=a*3;
正确答案是: D
编程题得分: 40
打印每月有几天 得分: 10 / 10
- import java.util.Scanner;
- /**
- * @Author liguo
- * @Description
- * @Data 2018-04-03
- */
- public class Main {
- public static void main(String[] args) {
- int year;
- int month;
- int[] a = {1, 3, 5, 7, 8, 10, 12};
- int[] b = {4, 6, 9, 11};
- Scanner in = new Scanner( System.in );
- year = in.nextInt();
- month = in.nextInt();
- // 判断二月, 考虑闰年情况
- if (month == 2) {
- if (year % 100 != 0 && year % 4 == 0 || year % 400 == 0)
- System.out.println( year + "-" + month + "-29" );
- else
- System.out.println( year + "-" + month + "-28" );
- }
- // 判断三十一天的情况
- for (int element : a) {
- if (month == element) {
- System.out.println( year + "-" + month + "-31" );
- }
- }
- // 判断三十天的情况
- for (int element : b) {
- if (month == element) {
- System.out.println( year + "-" + month + "-30" );
- }
- }
- }
- }
4-3-2 百分制成绩转换等级制成绩 得分: 10 / 10
- import java.util.Scanner;
- /**
- * @Author liguo
- * @Description 从键盘上读入一个百分制成绩 x(0 <= x < = 100),
- * 将其转换为等级制成绩输出. 本题在 C 语言和 Java 语言中要求使用 switch 分支实现.
- * 等级制成绩 (百分制成绩)
- * A(90<=x<=100)
- * B(80<=x<90)
- * C(70<=x<80)
- * D(60<=x<70)
- * E(0<=x<60)
- * @Data 2018-04-03
- */
- public class Main {
- static void judge(int mark) {
- char degree = 'A';
- int temp = mark / 10;
- if (temp>= 0 && temp <6)
- degree = 'E';
- if (temp == 6)
- degree = 'D';
- if (temp == 7)
- degree = 'C';
- if (temp == 8)
- degree = 'B';
- if (temp == 9 || temp == 10)
- degree = 'A';
- System.out.println( mark + "--" + degree );
- }
- public static void main(String[] args) {
- int temp;
- Scanner in = new Scanner( System.in );
- int x = in.nextInt();
- temp = x / 10;
- if (temp>= 0 && temp <6)
- temp = 5;
- switch (temp) {
- case 5:
- judge( x );
- break;
- case 6:
- judge( x );
- break;
- case 7:
- judge( x );
- break;
- case 9:
- judge( x );
- break;
- case 10:
- judge( x );
- break;
- }
- }
- }
用 if 语句求解分段函数 得分: 10 / 10
- import java.util.Scanner;
- /**
- * @Author liguo
- * @Description 分段函数求解: 输入 x , 计算并输出 y 的值:
- * y=x+100 ( 当 x < 20)
- * y= x ( 当 2 0 x 100)
- * y=x-100 ( 当 x > 100)
- * @Data 2018-04-03
- */
- public class Main {
- public static void main(String[] args) {
- double x, y;
- Scanner in = new Scanner( System.in );
- x = in.nextDouble();
- if (x < 20)
- y = x + 100;
- else if (x>= 20 && x <= 100)
- y = x;
- else
- y = x - 100;
- System.out.printf( "x=%.2f,y=%.2f", x, y );
- }
- }
2-2 混合类型数据格式化输入 得分: 10 / 10
- import java.util.Scanner;
- /**
- * @Author liguo
- * @Description 输入在一行中顺序给出浮点数 1, 整数, 字符, 浮点数 2, 其间以 1 个空格分隔.
- * 输入描述
- * 输入在一行中顺序给出浮点数 1, 整数, 字符, 浮点数 2, 其间以 1 个空格分隔.
- * 输出描述
- * 在一行中按照字符, 整数, 浮点数 1, 浮点数 2 的顺序输出, 其中浮点数保留小数点后 2 位.
- * @Data 2018-04-03
- */
- public class Main {
- public static void main(String[] args) {
- Scanner in = new Scanner( System.in );
- double d1 = in.nextDouble();
- int i = in.nextInt();
- String s = in.next();
- char c = s.charAt( 0 );
- double d2 = in.nextDouble();
- System.out.printf( "%c %d %.2f %.2f", c, i, d1, d2 );
- }
- }
3-4-1a 计算三位数的位数和 得分: 10 / 10
- import java.util.Scanner;
- /**
- * @Author liguo
- * @Description 从键盘输入任意一个三位数的整数, 请编写程序计算这个整数的数位和.
- 输入描述
- 输入一个三位数的整数
- 输出描述
- * @Data 2018-04-03
- */
- public class Main {
- public static void main(String[] args) {
- Scanner in = new Scanner( System.in );
- int temp = in.nextInt();
- temp = Math.abs(temp);
- int a = temp/100;
- int b = temp /10%10;
- int c = temp %10;
- int sum = a+b+c;
- System.out.println(sum);
- }
- }
来源: http://www.bubuko.com/infodetail-2551785.html