Java程序设计基础 - 中国大学mooc

已完结

 301

第1周 类与对象

单元测试一

1、下列哪个是Java应用程序主类中正确的main方法?

A、public void main (String args[ ])

B、static void main (String args[ ])

C、public static void Main (String args[])

D、public static void main (String args[ ])

2、下列哪个叙述是正确的?

A、Java应用程序由若干个类所构成,这些类必须在一个源文件中。

B、Java应用程序由若干个类所构成,这些类可以在一个源文件中,也可以分布在若干个源文件中,其中必须含有public类。

C、Java源文件必须含有主类。

D、Java源文件如果含有主类,主类可以是public类也可以不是public类。

3、下列哪个叙述是正确的?

A、成员变量的名字不可以和局部变量的相同。

B、方法的参数的名字可以和方法中声明的局部变量的名字相同。

C、成员变量没有默认值。

D、局部变量没有默认值。

4、对于下列Dog类,哪个叙述是错误的? class Dog { Dog(int m){ } Dog(double m){ } int Dog(int m){ return 23; } void Dog(double m){ } }

A、Dog(int m)与Dog(double m)互为重载的构造方法。

B、int Dog(int m)与void Dog(double m)互为重载的非构造方法。

C、Dog类只有两个构造方法,而且没有无参数的构造方法。

D、Dog类有3个构造方法。

5、说出下列E类中【代码1】~【代码2】的输出结果。(输入的答案要求两个数值间用逗号隔开) class A { double f(int x,double y) { return x+y; } int f(int x,int y) { return x%y; } } public class E { public static void main(String args[]) { A a=new A(); System.out.println(a.f(10,3)); //【代码1】 System.out.println(a.f(10,10.0)); //【代码2】 } }

第2周 对象交互

单元测试二

1、下列哪个叙述是正确的?

A、Java应用程序由若干个类所构成,这些类必须在一个源文件中。

B、Java应用程序由若干个类所构成,这些类可以在一个源文件中,也可以分布在若干个源文件中,其中必须有一个源文件含有主类。

C、Java源文件必须含有主类。

D、Java源文件如果含有主类,主类必须是public类。

2、对于下列Hello类,哪个叙述是正确的? class Hello { Hello(int m){ } int Hello() { return 20; } hello() { } }

A、Hello无法通过编译,因为其中的hello方法的方法头是错误的(没有类型)。

B、Hello类没有构造方法。

C、Hello类的int Hello()方法是错误的方法。

D、Hello类有2个构造方法。

3、对于下列Dog类,哪个叙述是错误的? class Dog { Dog(int m){ } Dog(double m){ } int Dog(int m){ return 23; } void Dog(double m){ } }

A、Dog类有3个构造方法。

B、Dog(int m)与Dog(double m)互为重载的构造方法。

C、int Dog(int m)与void Dog(double m)互为重载的非构造方法。

D、Dog类只有两个构造方法,而且没有无参数的构造方法。

4、下列E类的类体中哪些【代码】是错误的。 class E { int x; //【代码1】 long y = x; //【代码2】 public void f(int n) { int m; //【代码3】 int t = n+m; //【代码4】 } }

A、代码4

B、代码3

C、代码2

D、代码1

5、请说出A类中System.out.println的输出结果。 class B { int x = 100,y = 200; public void setX(int x) { x = x; } public void setY(int y) { this.y = y; } public int getXYSum() { return x+y; } } public class A { public static void main(String args[]) { B b = new B(); b.setX(-100); b.setY(-200); System.out.println("sum="+b.getXYSum()); } }

第3周 对象容器

单元测试三

1、1.在下列E类中System.out.println的输出结果是什么? import java.util.*; public class A { public static void main(String args[]) { LinkedList< Integer> list=new LinkedList< Integer>(); for(int k=3;k<=10;k++) { list.add(new Integer(k)); } list.remove(3); list.remove(3); Integer m=list.get(3); System.out.println(m.intValue()); } }

2、在下列E类中System.out.println的输出结果是什么? import java.util.*; public class E { public static void main(String args[]) { Stack mystack1=new Stack(), mystack2=new Stack(); StringBuffer bu=new StringBuffer(); for(char c='D';c>='A';c--) { mystack1.push(new Character(c)); } while(!(mystack1.empty())) { Character temp=mystack1.pop(); mystack2.push(temp); } while(!(mystack2.empty())) { Character temp=mystack2.pop(); bu.append(temp.charValue()); } System.out.println(bu); } }

第4周 继承与多态

单元测试四

1、下列哪个叙述是正确的?

A、子类重写或新增的方法也能直接操作被子类隐藏的成员变量。

B、子类继承的方法只能操作子类继承和隐藏的成员变量。

C、abstract类的子类必须是非abstract类。

D、子类继承父类的构造方法。

2、下列哪个叙述是正确的?

A、不可以同时用final和abstract修饰同一个方法。

B、abstract类中可以有非abstract方法,但该方法不可以用final修饰。

C、abstract类中只可以有abstract方法。

D、final 类可以有子类。

3、假设C是B的子类,B是A的子类,cat是C类的一个对象,bird是B类的一个对象,下列哪个叙述是错误的?

A、bird instanceof C的值是true。

B、cat instanceof A的值是true。

C、bird instanceof A的值是true。

D、cat instanceof B的值是true

4、下列程序中注释的哪个代码(A,B,C,D)是错误的(无法通过编译)? class A { static int m; static void f(){ m = 20 ; //A } } class B extends A { void f() //B { m = 222 ; //C } } class E { public static void main(String args[]) { A.f(); // D } }

A、B

B、A

C、C

D、D

5、请说出E类中【代码1】,【代码2】的输出结果。 class A { double f(double x,double y) { return x-y; } } class B extends A { double f(int x,int y) { return x*y; } } public class E { public static void main(String args[]) { B b=new B(); System.out.println(b.f(3,5)); //【代码1】 System.out.println(b.f(3.0,5.0)); //【代码2】 } }

第6周 抽象与接口

单元测试六

1、下列哪个叙述是正确的

A、允许接口中只有一个抽象方法。

B、如果一个非抽象类实现某个接口,那么它可以只重写接口中的部分方法。

C、如果一个抽象类实现某个接口,那么它必须要重写接口中的全部方法。

D、一个类最多可以实现两个接口。

2、下列接口中标注的(A,B,C,D)中,哪个是错误的? interface Takecare { public void speakHello(); //A public abstract static void cry(); //B int f(); //C abstract float g(); //D }

A、B

B、A

C、C

D、D

3、将下列(A,B,C,D)哪个代码替换下列程序中的【代码】不会导致编译错误。 A.public int f(){return 100+M;} B.int f(){return 100;} C.public double f(){return 2.6;}。 D.public abstract int f(); interface Com { int M = 200; int f(); } class ImpCom implements Com { 【代码】 }

A、A

B、B

C、C

D、D

4、请说出E类中【代码1】,【代码2】的输出结果。 interface A { double f(double x,double y); } class B implements A { public double f(double x,double y) { return x-y; } int g(int a,int b) { return a*b; } } public class E { public static void main(String args[]) { A a = new B(); System.out.println(a.f(3,5)); //【代码1】 B b = (B)a; System.out.println(b.g(3,5)); //【代码2】 } }

5、请说出E类中【代码1】,【代码2】的输出结果。 interface Com { int add(int a,int b); } abstract class A { abstract int add(int a,int b); } class B extends A implements Com{ public int add(int a,int b) { return a*b; } } public class E { public static void main(String args[]) { B b = new B(); Com com = b; System.out.println(com.add(12,6)); //【代码1】 A a = b; System.out.println(a.add(10,5)); //【代码2】 } }

第8周 异常处理与输入输出

单元测试八

1、1.下列代码标注的(A,B,C,D)中哪一个是错误的? class OutClass { int m = 1; static float x; //A class InnerClass { int m =12; //B static float n =20.89f; //C InnerClass(){ } void f() { m = 100; } } void cry() { InnerClass tom = new InnerClass(); //D } }

A、C

B、A

C、B

D、D

2、下列哪一个叙述是正确的?

A、和接口有关的匿名类可以是抽象类。

B、和类有关的匿名类还可以额外地实现某个指定的接口。

C、和类有关的匿名类一定是该类的一个非抽象子类。

D、和接口有关的匿名类的类体中可以有static成员变量。

3、下列哪个叙述是正确的?

A、创建File对象可能发生异常。

B、BufferedRead流可以指向FileInputStream流。

C、BufferedWrite流可以指向FileWrite流。

D、RandomAccessFile流一旦指向文件,就会刷新该文件。

4、为了向文件hello.txt尾加数据,下列哪个是正确创建指向hello.txt的流? A.try { OutputStream out = new FileOutputStream ("hello.txt"); } catch(IOException e){} B.try { OutputStream out = new FileOutputStream ("hello.txt",true); } catch(IOException e){} C.try { OutputStream out = new FileOutputStream ("hello.txt",false); } catch(IOException e){} D.try { OutputStream out = new OutputStream ("hello.txt",true); } catch(IOException e){}

A、A

B、B

C、C

D、D

5、请说出下列程序的输出结果。 import java.io.IOException; public class E { public static void main(String args[]){ try { methodA(); } catch(IOException e){ System.out.print("Hello"); return; } finally { System.out.println(" World!"); } } public static void methodA() throws IOException{ throw new IOException(); } }