蓝莓题库

中国大学moocjava核心技术慕课答案大全-k8凯发

欢迎来访!

k8凯发-凯发官网入口物理题库 正文

作者2023-10-22 23:21:51物理题库 78 ℃0 评论
第二章 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、请实现程序输出以下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)运行结果截图。

第四章 面向对象和类

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

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核心技术》期末考试

单项选择题

1、给定: public class test { public static void main(string [] args) { int x = 5; boolean b1 = true; boolean b2 = false; if((x==4) && !b2) system.out.print(“l “); system.out.print(“2 “); if ((b2 = true) && b1) system.out.print(“3 “); } } 输出结果为?
    a、2
    b、3
    c、1 2
    d、2 3

2、给定: 31. // some code here 32. try { 33. // some code here 34. // some code here 35. } catch (someexception se) { 36. // some code here 37. // some code here 38. } finally { 39. // some code here 40. // some code here 41. } 如下哪些情况出现时第39行中代码不会被执行?
    a、第31行代码抛出异常
    b、第34行代码抛出异常
    c、第37行代码抛出异常
    d、第40行代码抛出异常

3、给定: 10. interface foo {} 11. class alpha implements foo { } 12. class beta extends alpha {} 13. class delta extends beta { 14. public static void main( string[] args) { 15. beta x = new beta(); 16. // insert code here 17. } 18. } 哪一选项中代码插入到第16行, 会抛出 java.lang.classcastexception?
    a、alpha a = x;
    b、delta d = (delta)x
    c、foo f= (alpha)x;
    d、beta b = (beta)(alpha)x;

4、给定: 1. class testa { 2. public void start() { system.out.println(“testa”); } 3. } 4. public class testb extends testa { 5. public void start() { system.out.println(“testb”); } 6. public static void main(string[] args) { 7. ((testa)new testb()).start(); 8. } 9. } 结果为?
    a、testa
    b、testb
    c、编译失败
    d、运行时抛出异常

5、给定: 11. public abstract class shape { 12. int x; 13. int y; 14. public abstract void draw(); 15. public void setanchor(int x, int y) { 16. this.x = x; 17. this.y = y; 18. } 19. } 并且类circle继承并实现了shape 如下哪项是正确的?
    a、shape s = new shape(); s.setanchor(10,10); s.draw();
    b、circle c = new shape(); c.setanchor(10,10); c.draw();
    c、shape s = new circle(); s.setanchor(10,10); s.draw();
    d、circle c = new circle(); c.shape.setanchor(10,10); c.shape.draw();

6、给定: 11. public static void parse(string str) { 12. try { 13. float f= float.parsefloat(str); 14. } catch (numberformatexception nfe) { 15. f= 0; 16. } finally { 17. system.out.println(f); 18. } 19. } 20. public static void main(string[] args) { 21. parse(“invalid”); 22. } 结果为?
    a、0.0
    b、编译失败
    c、运行时抛出异常:parseexception
    d、运行时抛出异常: numberformatexception

7、给定类定义: 1. public class test { 2. int x= 12; 3. public void method(int x) { 4. x =x; 5. system.out.println(x); 6. } 7. } 给定: 34. test t = new test(); 35. t.method(5); 则testclass中第5行输出结果为:
    a、24
    b、10
    c、12
    d、17

8、给定: 55. int []x= {1, 2,3,4, 5}; 56.int y[] =x; 57. system.out.println(y[2]); 如下哪项是正确的?
    a、第57行输出2
    b、第57行输出3
    c、第55行编译失败
    d、第56行编译失败

9、给定: 10. class foo { 11. foo() { /* more code here */ } 12. static void alpha() { /* more code here */ } 13. void beta() { /* more code here */ } 14. } 如下哪些选项说法错误的是?
    a、foo() 中可以直接调用beta().
    b、foo() 中可以直接调用alpha().
    c、beta() 中可以直接调用alpha().
    d、alpha()中可以直接调用beta().

10、java.util.hashmap判断key的唯一性的方法是
    a、只判断hashcode
    b、只判断equals
    c、先判断hashcode再判断equals
    d、先判断equals再判断hashcode

11、下列说法中正确的一项是
    a、当类x是abstract时,x中的final方法可以定义为abstract
    b、类x中的protected方法可以被x的任何子类重写
    c、被private static修饰的方法只能被该类中其它static方法调用
    d、类x中非static且被public final修饰的方法能被类x的任何子类重写

12、给定 1. public class simplecalc { 2. public int value; 3. public void calculate() { value = 7; } 4. } 和: 1. public class multicalc extends simplecalc { 2. public void calculate() { value -= 3; } 3. public void calculate(int multiplier) { 4. calculate(); 5. super.calculate(); 6. value *=multiplier; 7. } 8. public static void main(string[] args) { 9. multicalc calculator = new multicalc(); 10. calculator.calculate(2); 11. system.out.println("value is: " calculator.value); 12. } 13. } 结果为:
    a、value is: 8
    b、编译失败
    c、value is: 12
    d、value is: -12

13、给定: 11. public static void main(string[] args) { 12. object obj =new int[] { 1,2,3 }; 13. int[] somearray = (int[])obj; 14. for (int i: somearray) system.out.print(i ” “) 15. } 结果为:
    a、1 2 3
    b、第12行无法通过编译
    c、第13行无法通过编译
    d、运行时抛出异常:classcastexception

14、给定: 1. public class a { 2. public void doit() { 3. } 4. public string doit() { 5. return “a”; 6. } 7. public double doit(int x) { 8. return 1.0; 9. } 10.} 结果为:
    a、运行时抛出异常
    b、第7行无法通过编译
    c、第4行无法通过编译
    d、编译成功,并且a在运行时也不会有任何异常抛出

15、给定: 11. public static void main(string[] args) { 12. string str = “null’; 13. if (str == null) { 14. system.out.println(“null”); 15. } else if (str.length() == 0) { 16. system.out.println(“zero”); 17. } else { 18. system.out.println(“some”); 19. } 20. } 结果为:
    a、null
    b、zero
    c、some
    d、compilation fails.

16、请阅读以下程序,并写出结果 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

17、请阅读以下程序,并写成结果。 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

18、如下关于jdk和jre的说法,错误的是?
    a、jdk全称java development kit,意即java开发工具包
    b、jre全程java runtime environment,意即java运行环境
    c、jre中包含了jdk
    d、若只需要运行编译好的java程序,则只有jre就可以

19、下列代码执行结果是 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、编译报错

20、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

程序题

1、完成动物的年龄排序。

猜你喜欢

  • 2023-10-22 23:59
  • 2023-10-22 23:48
  • 2023-10-22 23:40
  • 2023-10-22 23:38
  • 2023-10-22 23:29
  • 2023-10-22 23:28
  • 2023-10-22 23:21
  • 2023-10-22 22:20
  • 2023-10-22 22:19
  • 2023-10-22 22:12
网站分类
最新发表
标签列表
网站地图