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
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
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、编译报错
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、编译错误
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
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
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
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运行结果。
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();
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
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
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