0.061

五煦查题

快速找到你需要的那道考题与答案

超星Java语言程序设计C期末答案(学习通2023完整答案)

35 min read

超星Java语言程序设计C期末答案(学习通2023完整答案)

第二章 Java环境搭建和程序初体验

第二章作业 单步调试程序

1、超星程序public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); int a = 1; a=a+1; a=a+2; System.out.println("a is 设计" + a); a=a+3; //断点行 a=a+4; System.out.println("a is " + a); } } (1). 编写以上程序,并运行。末答将运行结果截图提交。案学 (2). 将断点设置在第八行,习通查看变量a的完整值,提交此刻的答案屏幕截图。

第三章 Java类基础知识

Java语言基础单元测试

1、超星程序在switch(expression)语句中,设计expression的末答数据类型不能是:
A、double
B、案学char
C、习通byte
D、完整short

2、答案下面程序的超星程序运行结果为: public class Conditional{ public static void main( String args[]){ int x = 4; System.out.println("value is " + ( (x > 4) ? 99.99 : 9 )); } }
A、value is 99.99
B、value is 9
C、value is 9.0
D、在第4行出现编译错误

3、以下程序的编译运行结果为: public class Test2 { public static void main(String args[]){ int i = 10; int j = 10; boolean b = false; if(b = i == j) //A行 System.out.println("True"); else System.out.println("False"); } }
A、A行出现编译错误
B、A行出现运行错误
C、输出True
D、输出False

4、假设a是int类型变量,并初始化为1,则下列哪个为合法的条件语句?
A、if (a) { }
B、if (a<<3) { }
C、if (a=2) { }
D、if (true) { }

5、设有如下代码: public class ex2 { public static void main(String[] args) { int x = 0;int y = 0; outer: for (x = 0;x < 100;x++) middle: for(y = 0;y < 100;y++){ System.out.println("x=" + x + ";y=" + y); if(y == 10){ /*insert code*/} } } } 在“insert code”处插入什么代码可以结束所有循环?
A、continue middle;
B、break outer;
C、break middle;
D、continue outer;

6、以下程序的编译运行结果为: public class ex2 { public static void main(String[] args) { int total = 0; for(int i = 0,j = 10;total > 30;++i,--j){ System.out.println("I = " + i + " ;j = " + j); total += (i+j); } System.out.println("Total: " + total); } }
A、产生运行错误
B、产生编译错误
C、输出“Total: 0”
D、输出如下结果: i = 0 ;j = 10 i = 1 ;j = 9 i = 2 ;j = 8 Total: 30

7、以下程序段执行完后,i、j的值分别为: int i = 1,j = 10; do{ if(i++>--j)continue; }while(i<5);
A、i=6 j=5
B、i=5 j=5
C、i=6 j=4
D、i=5 j=6

8、下面哪条语句存在语法错误:
A、byte b1 = 128;
B、byte b2 = 127;
C、byte b3 = ‘a’;
D、byte b4 = -128;

9、下面哪条语句存在语法错误:
A、long p1 = 1000000;
B、long p2 = 10000000000;
C、long p3 = 10000000000L;
D、long p4 = 100;

10、下面哪条语句存在语法错误:
A、double d1 = 4.56;
B、float d2 = 4.56;
C、float d3 = 4.56f;
D、double d4 = 4.56f;

11、下列字符串中,哪些是Java的合法标志符?
A、$short
B、x+y
C、2PI
D、_bytes

12、下列字符串中,哪些是Java的关键字?
A、if
B、int
C、null
D、switch

13、有如下代码段: switch(a){ case 1:System.out.println("One");break; case 2: case 3:System.out.println("Two");break; default:System.out.println("end"); } 变量a的取值是下列哪些情形时能使程序输出“Two”。
A、1
B、2
C、3
D、default

14、关于以下程序哪条叙述正确: int j = 2; switch(j){ case 2: System.out.println("value is two"); case 2+1: System.out.println("value is three"); break; default: System.out.println("value is " + j); break; }
A、输出是value is two后跟value is three
B、第5行的表达式不合法
C、输出为value is two
D、switch中的表达式可以是byte、short或int中的任何类型

15、下面不属于Java基本数据类型的是:
A、String
B、Integer
C、byte
D、boolean

第三章 Java类基础知识 作业

1、请实现程序输出以下5*5数字方格。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 需要在main函数的输入参数中设置5,输出5*5的数字方格。如果是输入7,则是7*7的数字方格。需要提交代码、(Eclipse)设置参数截图、(Eclipse)运行结果截图。

2、请实现程序输出以下星塔。 * *** ***** *** * 需要在main函数的输入参数中设置5,输出5层星塔。如果是输入7,则是7层星塔。假设输入参数都是奇数,且都大于等于5,小于等于11。需要提交代码、(Eclipse)设置参数截图、(Eclipse)运行结果截图。

3、从键盘键入三个整数,然后按照从小到大的顺序将其输出。 需要提交代码、(Eclipse)运行结果截图。

4、编写程序,输出九九乘法表。 需要提交代码、(Eclipse)运行结果截图。

5、编程计算1-100内所有素数的和。 需要提交代码、(Eclipse)运行结果截图。

6、编写程序,输出从公元1900年到2100年所有闰年的年号,每输出5个年号换一行。判断年是否为闰年的条件是:① 若年号可以被4整除,而不能被100整除,则是闰年;② 若年号可以被400整除,也是闰年。 需要提交代码、(Eclipse)运行结果截图。

7、水仙花数是指一个3位数,其个位、十位、百位上的数字的立方和等于该数本身,例如371=3的3次方+7的3次方+1的3三次方,因此371是一个水仙花数。编写程序,求所有的水仙花数。 需要提交代码、(Eclipse)运行结果截图。

8、计算e=1+1/1! +2/2! +……+n/n!。要求n由控制台输入,e精确到小数点后5位。 需要提交代码、(Eclipse)运行结果截图。

第四章 面向对象和类

第四章 面向对象和类 第一次作业

1、完善三个数字对象排序程序。

第四章 面向对象和类 (续)

前四章 单元测验

1、有如下代码段: public static void booleanTest() { int a = 1, b =1; if (a == b || b<0) a++; if (a <= 2 &&(!(b<0))) b=b<<1; System.out.println(a + "," + b); } 则运行结果为:
A、2,1
B、2,2
C、2,3
D、1,2

2、如下赋值语句中,有语法错误的是?
A、float f1 = 1.2;
B、float f1 = 1.2f;
C、float f1 = 1;
D、float f1 = 0xAE;

3、有如下类定义: public class Rectangle { public int width = 3; public int height = 4; public int area() { return width * height; } } 则如下代码输出结果为: Rectangle rectangle; rectangle.height = 5; System.out.println(rectangle.area());
A、15
B、有编译错误,程序不能运行
C、12
D、0

4、执行如下代码片段后,i和n的值分别为: int i = 10; int n =( i++) % 5;
A、11, 1
B、11, 0
C、10, 1
D、10, 0

5、执行如下代码片段后,num的值为: int num = 5; num = (num % 2) == 0 ? num – 1 : num + 1;
A、1
B、4
C、5
D、6

6、有如下代码段: if (num >= 0) if (num == 0) System.out.println("first string"); else System.out.println("second string"); System.out.println("third string"); 若num为3,则输出结果为:
A、third string
B、second string third string
C、first string third string
D、first string second string third string

7、下列变量名称中,不属于有效Java变量命名的是?
A、$num
B、_int
C、6nums
D、Jiayou

8、对于Java1.7及之后版本,如下不能用于switch的类型是:
A、String
B、int
C、char
D、double

9、如下对Java基本类型的描述,错误的是?
A、char占1个字节
B、int 占4个字节
C、short 占2个字节
D、double占8个字节

10、如下循环结构中,输出结果与其它三组不一致的一组是:
A、for (int i = 0; i < 10; i++) System.out.println(i);
B、int i = 0; while (i < 10) System.out.println(i++);
C、int i = 0; for (;i < 10;) System.out.println(i++);
D、int i = 0; while (i++ < 10) System.out.println(i);

11、swap方法定义如下: public static void swap(int num1, int num2) { int temp = num1; num1 = num2; num2 = temp; } 执行如下代码后, int num1 = 10; int num2 = 5; int num3 = 20; swap(num1, num2); swap(num2, num3); num1, num2, num3的值分别为:
A、10, 5, 20
B、5, 20, 10
C、5, 10, 20
D、20, 5, 10

12、Number类定义如下: public class Number { public int x; } swap方法定义如下: public static void swap(Number number1, Number number2) { int temp = number1.x; number1.x = number2.x; number2.x = temp; } 运行如下代码: Number number1 = new Number(); Number number2 = new Number(); Number number3 = new Number(); number1.x = 1; number2.x = 2; number3.x = 3; swap(number1, number2); swap(number2, number3); 则number1.x, number2.x, number3.x的值分别为:
A、1, 2, 3
B、2, 3, 1
C、3, 2, 1
D、1, 3, 2

13、假设有boolean变量flag1,flag2,则如下表达式中哪个不能代表异或逻辑?(异或逻辑:如果a、b两个值不相同,则异或结果为true。如果a、b两个值相同,异或结果为false。)
A、flag1 != flag2
B、(flag1 == true && flag2 == false) || (flag1 == false && flag2 == true)
C、!flag1 == flag2
D、(flag1 == true && flag2 == true) || (flag1 == false && flag2 == false)

14、如下关于Java类的说法,错误的是?
A、对象是类的实例化
B、可以通过对象访问类变量
C、java文件中只能包含一个类的定义
D、同一类的不同对象有着相同的类变量

15、如下赋值语句,有编译错误的是?
A、byte b = -127;
B、int i = (byte)512;
C、byte b = 129;
D、byte b = -0;

16、下列关于main方法的描述中,错误的是?
A、main方法是Java程序的入口
B、main方法格式为 public static void main(String[] args) { //Your code here }
C、B选项中所描述格式中形参args不能更改,如果将args改为arguments则不能编译通过
D、main方法可以被重载

17、Java有“一次编译,到处运行”的说法,此种说法中编译的结果是:
A、机器码
B、符号表
C、字节码
D、中间代码

18、下列不属于Java基本数据类型的是?
A、short
B、float
C、Double
D、int

19、如下关于JDK和JRE的说法,错误的是?
A、JDK全称Java Development Kit,意即Java开发工具包
B、JRE全程Java Runtime Environment,意即Java运行环境
C、JRE中包含了JDK
D、若只需要运行编译好的Java程序,则只有JRE就可以

20、在Java中,下面对于构造函数的描述正确的是
A、类必须显式定义构造函数
B、构造函数的返回类型是void
C、构造函数和类有相同的名称,并且不能带任何形参
D、一个类可以定义多个构造函数

21、Assume i and j are member variables with double type in class X. In the following codes, which one is NOT RIGHT constructor? ( )
A、double X(double k ){ i=k; return i; }
B、X(double m, double n ){ i=m; j=n; }
C、X( ){ i=6;j=8;}
D、X(double k ){ i=k; }

22、Given: class CardBoard { Short story = 5; CardBoard go(CardBoard cb) { cb = null; return cb; } public static void main(String[] args) { CardBoard c1 = new CardBoard(); CardBoard c2 = new CardBoard(); CardBoard c3 = c1.go(c2); c1 = null; // do Stuff } } When // doStuff is reached, how many objects of CardBoard are null?
A、0
B、1
C、2
D、Compilation fails.

23、Given the uncompleted code of a class: class Person { String name, department; int age; public Person(String n){ name = n; } public Person(String n, int a){ name = n; age = a; } public Person(String n, String d, int a) { // doing the same as two arguments version of constructor // including assignment name=n,age=a department = d; } } Which expression can be added at the "doing the same as..." part of the constructor?
A、Person(n,a);
B、this(Person(n,a));
C、this(n,a);
D、this(name,age);

24、Given the following class class MyNumber { private int num = 5; public MyNumber(int num) { this.num = num; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } } What is output after the executation of following code? MyNumber obj1 = new MyNumber(); MyNumber obj2 = new MyNumber(10); obj2 = obj1; obj2.setNum(20); System.out.println(obj1.getNum() + “,” + obj2.getNum());
A、5, 20
B、5, 10
C、20,20
D、编译错误

25、Given the following class: class Mixer { Mixer() { } Mixer(Mixer m) { m1 = m; } Mixer m1; public static void main(String[] args) { Mixer m2 = new Mixer(); Mixer m3 = new Mixer(m2); m3.go(); Mixer m4 = m3.m1; m4.go(); Mixer m5 = m2.m1; m5.go(); } void go() { System.out.print("hi "); } } What is the result?
A、Compilation fails
B、hi hi hi
C、hi hi, followed by an exception
D、hi, followed by an exception

第五章 继承、接口和抽象类

第五章 单元测验

1、现有 public class Parent{ public void change (int x){ } } public class Child extends Parent{ //覆盖父类change方法 } 下列哪个声明是正确的覆盖了父类的change方法?
A、protected void change (int x){ }
B、public void change(int x, int y){ }
C、public void change (int x){ }
D、public void change (String s){ }

2、class Ca{ int num = 1; Ca(int num){ this.num = num; System.out.print(this.num); } } class Cb extends Ca{ int num = 2; Cb(int num) { this.num = num; System.out.print(num); } public static void main(String[] args) { Ca a = new Cb(5); } } 运行代码,程序输出结果为:
A、15
B、52
C、51
D、编译报错

3、下面关于继承的叙述正确的是()
A、Java中一个类只能实现一个接口
B、Java类中只允许单一继承
C、Java中一个类不能同时继承一个类和实现一个接口
D、Java中一个类可继承于多个类

4、给定下列程序,请选出正确结果。 class Cat { Cat (int c) { System.out.print ("cat"+c+" "); } } class SubCat extends Cat { SubCat (int c){ super (5); System.out.print ("cable"); } SubCat() { this (4); } public static void main (String [] args) { SubCat s= new SubCat(); } }
A、cat5
B、cable
C、cat5 cable
D、cable cat5

5、下列程序的输出是()。 class Other{ public Other () { System.out.print("Other!"); } } public class Driver1 extends Other { public static void main( String[] args ) { new Driver1(); new Other (); } }
A、Other!
B、Other!Other!
C、Other!Other!Other!
D、编译出错

6、请选出以下程序的输出结果 class A { public void func1() { System.out.println("A func1 is calling"); } public void func2() { func1(); } } class B extends A { public void func1() { System.out.println("B func1 is calling"); } public void func3() { System.out.println("B func3 is calling"); } } class C { public static void main(String[] args) { A a = new B(); a.func1(); a.func2(); a.func3(); } }
A、A func1 is calling B func3 is calling
B、B func1 is calling B func3 is calling
C、A func1 is calling A func1 is calling B func3 is calling
D、编译错误

7、请选出以下程序的输出结果 public class Child extends People { People father; public Child(String name) { System.out.print(3); this.name = name; father = new People(name + ":F"); } public Child() { System.out.print(4); } public static void main(String[] args) { new Child("Alice"); } } class People { String name; public People() { System.out.print(1); } public People(String name) { System.out.print(2); this.name = name; } }
A、123
B、132
C、32
D、312

8、请选出正确答案 class Parent { String one, two; public Parent(String a, String b){ one = a; two = b; } public void print(){ System.out.println(one); } } public class Child extends Parent { public Child(String a, String b){ super(a,b); } public void print(){ System.out.println(one + " to " + two); } public static void main(String arg[]){ Parent p = new Parent("south", "north"); Parent t = new Child("east", "west"); p.print(); t.print(); } }
A、Cause error during compilation.
B、south to north east to west
C、south to north east
D、south east to west

9、请选择正确的输出结果 class Guy { public Guy(){ System.out.print("111,"); } } class Cowboy extends Guy { public Cowboy(){ System.out.print("222,"); } } class Wrangler extends Cowboy { public Wrangler(){ System.out.print("333,"); } } public class Greeting2 { public static void main(String[] args) { Guy g1 = new Guy(); Guy g2 = new Cowboy(); Guy g3 = new Wrangler(); } }
A、111,222,333,
B、111,111,222,222,333,
C、111,111,222,111,222,333,
D、编译错误

10、给定以下程序 class Pencil { public void write (String content){ System.out.println( "Write"+content); } } class RubberPencil extends Pencil{ public void write (String content){ System.out.println("Rubber Write"+content); } public void erase (String content){ System.out.println( "Erase "+content); } } 执行下列代码的结果是哪项? Pencil p=new Pencil(); (( RubberPencil) p).write("Hello");
A、Write Hello
B、Rubber Write Hello
C、编译失败
D、运行时抛出异常

第六章 static、final和常量设计

第六章 static和final测验

1、下面关于变量及其范围的陈述哪些是错误的
A、实例变量是类的成员变量
B、实例变量用关键字static声明
C、在方法中定义的局部变量在该方法被执行时创建
D、局部变量在使用前必须被初始化

2、下列说法错误的是
A、声明为static的方法可以被重写
B、声明为static的方法不可以调用非static变量
C、声明为final的方法可以被重写
D、声明为final的类不可以被继承

3、以下代码 class FinalTest{ int num = 1; public static void main(String[] args) { final FinalTest ft = new FinalTest();//1 ft.num = 100;//2 //3 System.out.println(ft.num);//4 } }
A、编译通过,但在//3处加上 ft.num ++; 后编译报错
B、编译通过,但在//3处加上 ft = new FinalTest();; 后编译报错
C、编译不通过,去除//1中的 final 后编译通过
D、编译不通过,删除//2 整行后编译通过

4、下列代码执行结果是 class NumTest{ static int id = 1; int id2 = 1; NumTest(int id,int id2){ this.id = id; this.id2 = id2; } void printId(){ System.out.print(id+id2+" "); } public static void main(String[] args) { NumTest a = new NumTest(1,2); NumTest b = new NumTest(2,1); NumTest c = new NumTest(0,0); a.printId(); b.printId(); c.printId(); } }
A、3 3 0
B、1 2 0
C、2 1 0
D、编译报错

5、以下代码 class FinalTest{ final int num = 1; public static void main(String[] args) { final FinalTest ft = new FinalTest();//1 ft.num = 100;//2 //3 System.out.println(ft.num);//4 } }
A、编译通过,但在//3处加上 ft.num ++; 后编译报错
B、编译通过,但在//3处加上 ft = new FinalTest(); 后编译报错
C、编译不通过,去除//1的 final 后编译通过
D、编译不通过,删除//2 整行后编译通过

6、class NumTest{ final int id = 1; int id2 = 1; NumTest(int id,int id2){ this.id = id; this.id2 = id2; } void printId(){ System.out.print(id+id2+" "); } public static void main(String[] args) { NumTest a = new NumTest(1,2); NumTest b = new NumTest(2,1); NumTest c = new NumTest(0,0); a.printId(); b.printId(); c.printId(); } }
A、3 3 0
B、1 2 0
C、2 1 0
D、编译报错

7、下列代码执行结果是 class NumTest{ final static int num1 = 1; static int num2 = 1; void printNum1(){ System.out.print(num1+" "); } void printNum2(){ System.out.print(num2+" "); } public static void main(String[] args) { NumTest a = new NumTest(); a.num2 ++; a.printNum1(); NumTest b = new NumTest(); b.printNum2(); } }
A、1 1
B、1 2
C、2 2
D、编译报错

8、下列代码执行结果是 class NumTest{ final static int num1 = 1; static int num2 = 1; void printNum1(){ System.out.print(num1+" "); } void printNum2(){ System.out.print(num2+" "); } public static void main(String[] args) { NumTest a = new NumTest(); a.num1 ++; a.printNum2(); NumTest b = new NumTest(); b.printNum1(); } }
A、2 1
B、1 2
C、1 1
D、编译报错

9、以下代码执行结果是 class StaticTest{ static{ System.out.print("a "); } static{ System.out.print("b "); } public static void main(String[] args) { StaticTest st1 = new ChildTest(); } } class ChildTest extends StaticTest{ static{ System.out.print("c "); } }
A、c a b
B、a b c
C、c
D、a b

10、以下代码执行结果是 class StaticTest{ static{ System.out.print("a "); } { System.out.print("b "); } public static void main(String[] args) { StaticTest st2 = new ChildTest(); //main1 System.out.print(“ # ”); //main2 StaticTest st = new StaticTest(); //main3 } } class ChildTest extends StaticTest{ static{ System.out.print("c "); } }
A、a c b # a b
B、a b c # a b c
C、a c b # b
D、a b c # a b

第六章 static、final和常量设计(续)

期中练习

1、有如下类定义: public?class?ClassAndVariables{ ????public?static?int?x?=?8;? ????public?int?y?=?9;? } 执行如下代码: ClassAndVariables?a?=?new?ClassAndVariables(); ClassAndVariables?b?=?new?ClassAndVariables(); a.y?=?5; b.y?=?6; a.x?=?1; b.x?=?2; 则a.y,?b.y,?a.x,?b.x的值分别为:
A、5, 6, 1, 2
B、6, 6, 1, 2
C、5, 6, 2, 2
D、6, 6, 2, 2

2、请阅读以下程序,并写出结果 public class ArgumentPassing { public static void changeValue(int a) { a = 10; } public static void changeValue(String s1){ s1 = "def"; } public static void changeValue(StringBuffer s1) { s1.append("def"); } public static void main(String[] args) { int a = 5; String b = "abc"; StringBuffer c = new StringBuffer("abc"); changeValue(a); changeValue(b); changeValue(c); System.out.print(a); System.out.print(b); System.out.print(c); } }
A、5abcabc
B、10abcabc
C、10defdef
D、5abcabcdef

3、下列关于构造方法的叙述中,错误的是
A、Java语言规定构造方法名与类名必须相同
B、Java语言规定构造方法没有返回值,但不用void声明
C、Java语言规定构造方法不可以重载
D、Java语言规定构造方法只能通过new自动调用

4、关于以下程序段,正确的说法是()。 String s1= "abc" + "def"; //1 String s2= new String(s1); //2 if (s1==s2) //3 System.out.println("= = succeeded"); //4 if (s1.equals(s2)) //5 System.out.println(".equals() succeeded"); //6
A、行4与行6都将执行
B、行4执行,行6不执行
C、行6执行,行4不执行
D、行4、行6都不执行

5、请阅读以下程序,并写成结果。 class Father { public void hello() { System.out.println("Father says hello."); } } public class Child extends Father { public void hello() { System.out.println("Child says hello"); } public static void main(String[] a) { Child foo = new Child(); //foo.hello(); Father foo2 = (Father) foo; //foo2.hello(); Child foo3 = (Child) foo2; //foo3.hello(); System.out.println(foo==foo2); System.out.println(foo==foo3); } }
A、true true
B、true false
C、false true
D、false false

6、运行如下程序,输出结果是()。 StringBuffer sb = new StringBuffer("good morning!"); String sub = sb.substring(0, 8); System.out.println(sub); System.out.print("/"); char c = sb.charAt(6); System.out.println(c);
A、good mor /o
B、good morn/o
C、good morn/m
D、good mor/ o

7、如下所示的Test类的Java程序中,共有几个构造方法()。 public class Test{ private int x; public Test(){ } public void Test(int i){ this.x=i; } public Test(String str){ }}
A、0
B、1
C、2
D、3

8、下面代码的运行结果为:() public class Foo { static String s; public static void main (String[]args) { System.out.println ("s=" + s); } }
A、代码得到编译,并输出“s=”
B、代码得到编译,并输出“s=null”
C、由于String s没有初始化,代码不能编译通过
D、代码得到编译,但捕获到 NullPointException异常

9、已知如下代码:( ) public class Test { public static void main(String arg[] ) { int i = 5; do{ System.out.print(i); }while(-i>5); System.out.print("finished"); } } 执行后的输出是什么?
A、5finished
B、4
C、6
D、finished

10、Given: abstract class Bar { public int getNum() { return 38; } } public abstract class AbstractTest { public int getNum() { return 45; } public static void main(String[] args) { AbstractTest t = new AbstractTest() { public int getNum() { return 22; } }; Bar f = new Bar() { public int getNum() { return 57; } }; System.out.println(f.getNum() + " " + t.getNum()); } } What is the result?
A、57 22
B、45 38
C、45 57
D、An exception occurs

11、public class Child extends People { People father; public Child(String name) { System.out.print(3); this.name = name; father = new People(name + ":F"); } public Child() { System.out.print(4); } public static void main(String[] args) { new Child("Alice"); } } class People { String name; public People() { System.out.print(1); } public People(String name) { System.out.print(2); this.name = name; } }
A、32
B、132
C、123
D、1234

12、现有: class Guy{ String greet(){ return "hi "; }} class Cowboy extends Guy{ String greet(){ return "howdy "; }} class Wrangler extends Cowboy{ String greet(){ return "ouch! "; } } class Greetings2 { public static void main (String [] args) { Guy g=new Wrangler(); Guy g2=new Cowboy(); Wrangler w2=new Wrangler(); System.out.print(g.greet()+g2.greet()+w2.greet()); } } 结果是什么?
A、hi hi ouch!
B、ouch! howdy ouch!
C、hi howdy ouch!
D、编译失败

13、现有: class Tree { private static String tree = "tree"; String getTree() { return tree; } } public class Elm extends Tree { private static String tree = "elm"; public static void main(String[] args) { new Elm().go(new Tree()); } void go(Tree t) { String s = t.getTree() + Elm.tree + tree + (new Elm().getTree()); System.out.println(s); } }
A、elmelmelmelm
B、treeelmelmelm
C、treeelmelmtree
D、treeelmtreeelm

14、接口是Java面向对象的实现机制之一,以下说法正确的是:( )
A、Java支持多重继承,一个类可以实现多个接口
B、Java只支持单重继承,一个类可以实现多个接口
C、Java只支持单重继承,一个类只可以实现一个接口
D、Java支持多重继承,但一个类只可以实现一个接口

15、如果想要一个类不能被任何类继承的话,需要使用哪个关键字来修饰该类?
A、abstract
B、new
C、static
D、final

16、class Person { private int a; public int change(int m){ return m; } } public class Teacher extends Person { public int b; public static void main(String arg[]){ Person p = new Person(); Teacher t = new Teacher(); int i; // point x } } Which are syntactically valid statement at // point x?
A、i = m;
B、i = b;
C、i = p.a;
D、i = p.change(30);

17、请问以下代码的输出是什么: class A { public static int x = 10; public static void printX() { System.out.print(x); } } public class Elm extends A { public int x = 20; public static void main(String[] args) { A a = new Elm(); printX(); System.out.print("和"); System.out.print(a.x); } }
A、10和20
B、20和10
C、10和10
D、20和20

18、类 Teacher 和 Student 是类 Person 的子类; Teacher t; Student s; // t and s are all non-null. if (t instanceof Person ){ s=(Student)t; } 最后一条语句的结果是:
A、将构造一个Student 对象;
B、表达式是合法的;
C、表达式是错误的;
D、编译时正确, 但运行时错误。

19、下述代码的执行结果是 class Super { public int getLength() { return 4; } } public class Child extends Super { public long getLength() { return 5; } public static void main(String[] args) { Super sooper = new Super(); Super sub = new Child(); System.out.print(sooper.getLength() + "," + sub.getLength()); } }
A、2
B、4
C、编译失败
D、6

20、下列关于interface的说法正确的是:
A、interface中可以有private方法
B、interface中可以有final方法
C、interface中可以有function实现
D、interface可以继承其他interface

第七章 package、import和classpath

第七章 package、import和classpath 作业

1、新建Work07-01项目,里面有一个com.test.Worker类,类代码如下: package com.test; class Worker { public void sayHello() { System.out.println("hello"); } } 将此类导出为worker.jar。 新建Work07-02项目,里面有一个com.bussiness.Boss类,Boss类的代码如下: package com.bussiness; import com.test.Worker; class Boss { public static void main(String[] a) { Worker w = new Worker(); w.sayHello(); } } 需要在Work07-02里面导入work.jar。 作业需要提交3个截图:导出jar,导入jar,Boss运行结果。

2、完成第七章第二节后半部分的H.java, F.java, C.java的命令行编译和运行。这几个类的源码已经在课件材料中可以下载。学生需要在控制台命令行下,执行每个类的编译和运行命令,并截图提交,共6个图,3个编译,3个运行和运行结果。

第八章 Java常用类

第八章 Java常用类 作业

1、验证身份证号码是否正确

2、验证身份证号码是否正确(带校验算法)

第九章 Java异常和异常处理

第九章 Java异常和异常处理 测验

1、给出如下代码段: try?{ ?int?x?=?Integer.parseInt("two");?} 下列哪个可以作为catch的异常?
A、ClassCastException
B、IllegalStateException
C、NumberFormatException
D、ExceptionInInitializerError

2、给出下列代码: class?Plane?{ static?String?s?=?"-"; public?static?void?main(String[]?args){ new?Plane().s1(); System.out.println(s); } void?s1()?{ try?{ s2();}catch?(Exception?e){ ?s?+=?"c";? } } void?s2()?throws?Exception?{ s3();? s?+=?"2"; s3(); s?+=?"2b"; } void?s3()?throws?Exception{ throw?new?Exception(); } } 结果是什么?
A、-
B、-c
C、-c2
D、-2c

3、下列程序的执行,说法正确的是( ) class MultiCatch { public static void main(String args[]) { try { int a=args.length; int b=42/a; int c[]={ 1}; c[42]=99; //10行 System.out.println(“b=”+b); } catch(ArithmeticException e) { System.out.println(“除0异常:”+e); //15行 } catch(ArrayIndexOutOfBoundsException e) { System.out.println(“数组超越边界异常:”+e); //19行 } } }
A、程序将输出第15行的异常信息
B、程序第10行出错
C、程序将输出“b=42”
D、程序将输出第19行的异常信息

4、下面是一些异常类的层次关系: java.lang.Exception java.lang.RuntimeException java.lang.IndexOutOfBoundsException java.lang.ArrayIndexOutOfBoundsException java.lang.StringIndexOutOfBoundsException 假设有一个方法X,能够抛出两个异常,Array Index和String Index异常,假定方法X中没有try-catch语句处理这些异常,下面哪个答案是正确的?( )
A、方法X 应该声明抛弃ArrayIndexOutOfBoundsException和StringIndexOutOfBounds-Exception。
B、如果调用X的方法捕获IndexOutOfBoundsException,则ArrayIndexOutOfBoundsException和StringIndexOutOfBoundsException都可以被捕获。
C、如果方法X声明抛弃IndexOutOfBoundsException,则调用X的方法必须用try-catch语句捕获。
D、方法X不能声明抛弃异常。

5、请问所有的异常(Exception)和错误(Error)类皆继承哪一个类?( )
A、java.lang.Throwable
B、java.lang.Exception
C、java.lang.Error
D、java.io.Exception

6、pubic void test () { try { oneMethod (); System.out.print ( "condition 1"); } catch ( Exception e ) { System.out.print ( "condition 3"); } catch ( ArithmeticException e ) { System.out.print ( "condition 2" ); } finally { System.out.println ("condition 4" ); } } Which will display if oneMethod throw NullPointerException?
A、condition 1 Condition4
B、condition 2 Condition4
C、condition 3 Condition4
D、error in compilation

7、Given: import?java.io.*; class?Master?{ String?doFileStuff()?throws?FileNotFoundException?{ ? return?"a"; ?} } class?Slave?extends?Master?{ public?static?void?main(String[]?args){ String?s?=?null; try?{ ? s?=?new?Slave().doFileStuff();}catch?(?Exception?x){ s?=?"b";? } System.out.println(s); } //?insert?code?here } Which,?inserted?independently?at?//?insert?code?here,?will?compile,?and?produce?the?output b??(Choose?all?that?apply.)
A、String doFileStuff() { return "b"; }
B、String doFileStuff() throws IOException { return "b"; }
C、String doFileStuff(int x) throws IOException { return "b"; }
D、String doFileStuff() throws Exception { return "b"; }

8、Given import java.io.*; class Main{ public void f1() throws ArithmeticException{ } public void f2() throws FileNotFoundException{ } public static void main(){ new Main().f1(); //line1 new Main().f2(); //line2 } } Which is correct?
A、No error in compilation
B、error at //line1 in compilation
C、error at //line2 in compilation
D、two errors at //line1 and //line2 in compilation

9、class Emu{ static String s = "-"; public static void main(String[] args){ try{ throw new Exception(); }catch(Exception e){ try{ try{ throw new Exception(); }catch (Exception ex){ s += "ic "; } throw new Exception(); } catch(Exception x){ s += "mc "; } finally{ s += "mf "; } }finally{ s += "of "; } System.out.println(s); } } What is the result?
A、-ic mf of
B、-ic mc mf of
C、-ic mc of mf
D、Compilation fails

10、Given: class Mineral{ } class Gem extends Mineral{ } class Miner{ static int x = 7; static String s = null; public static void getWeight(Mineral m){ int y = 0 / x; System.out.print(s + " "); } public static void main(String[] args){ Mineral[] ma = { new Mineral(), new Gem()}; for(Object o : ma) getWeight((Mineral) o); } } And the command-line invocation: java Miner What is the result?
A、null
B、null null
C、A ClassCastException is thrown.
D、A NullPointerException is thrown.

第十章 Java数据结构

第十章 Java数据结构 作业

1、请完成汇率和金额排序程序。

第十一章 Java文件读写

第十一章 Java文件读写 作业

1、读取以下的a.txt,统计相同单词的次数,最后按照次数的大小降序排列,输出到b.txt中。 a.txt (===算分隔符,不算单词,不考虑单词大小写) ============== hello world hello java world hello ============== b.txt ============ hello,3 world,2 java,1 ============ 提示:需要用到的内容有文本文件读和写操作。还要加上数据结构,有ArrayList, HashMap,Comparable等。

学习通Java语言程序设计C

Java语言是一种高级语言,具有跨平台、面向对象等特点,广泛应用于各种领域。

学习Java语言的必要性

Java语言是一门现代化的编程语言,具有较高的可读性和可维护性,容易学习,同时也具有广泛的应用领域,如互联网、移动设备、人工智能等。对于程序员而言,学习Java语言是非常重要的。

学习Java语言的基础知识

学习Java语言需要掌握一些基础知识,如数据类型、运算符、流程控制、数组等。其中,数据类型是Java语言的基础,包括整型、浮点型、字符型、布尔型等。运算符是用于进行各种计算的符号,如算术运算符、逻辑运算符等。流程控制可以控制程序的执行顺序,如if语句、while语句、for语句等。数组是用于存储一组相同类型的数据的集合,可以进行遍历、排序等操作。

学习Java语言的面向对象特性

Java语言是一门面向对象的编程语言,可以使用类、对象、继承、多态等面向对象特性进行编程。类是Java语言的基本组成单位,可以用来描述对象的属性和方法。对象是类的实例化,可以调用类的方法进行操作。继承是一种类与类之间的关系,可以使一个类继承另一个类的属性和方法。多态是一种同样的方法在不同的对象上有着不同的行为,可以提高程序的灵活性和扩展性。

学习Java语言的应用

Java语言的应用非常广泛,如Web应用、移动应用、桌面应用、云计算等。其中,Web应用是Java语言的主要应用领域之一,如Java Web框架、Java服务器、Java EE等。移动应用也是Java语言的重要应用领域,如Android开发、J2ME开发等。桌面应用则主要是使用Swing、AWT等GUI库进行开发,云计算则可以使用Java语言进行云计算平台的开发。

学习Java语言的实践方法

学习Java语言需要不断地进行实践,可以通过一些开发工具进行实践。常用的Java开发工具有Eclipse、IntelliJ IDEA、NetBeans等。其中,Eclipse是比较流行的Java开发工具,具有丰富的插件和功能,支持多种Java开发,包括Web开发、移动开发等。

结语

Java语言是现代化的编程语言,具有广泛的应用领域和丰富的特性。学习Java语言需要掌握一定的基础知识和面向对象特性,并进行不断的实践。