1、如何更改break语句使退出inner和middle循环,继续外循环的下一轮? outer: for (int x = 0; x < 3; x++) { middle: for (int y = 0; y < 3; y++) { inner: for (int z = 0; z < 3; z++) { if (arr(x, y, z) == targetValue) break; } } } A、break inner; B、break middle; C、break outer; D、continue;
2、以下程序的输出结果为? public class Test { public static void main(String args[]) { for ( int k = 0; k < 3; k++) System.out.print("k"); } } A、012 B、k C、0123 D、kkk
3、以下代码的调试结果为? 1: public class Q10 2: { 3: public static void main(String[] args) 4: { 5: int i = 10; 6: int j = 10; 7: boolean b = false; 8: 9: if( b = i == j) 10: System.out.println("True"); 11: else 12: System.out.println("False"); 13: } 14: } A、在第9行出现编译错误 B、在第9行出现运行异常 C、输出 :True D、输出 :False
4、以下代码的调试结果为?以下程序的运行结果为 public class test { public static void main(String args[]) { int i = 1; do { i--; } while (i > 2); System.out.println(i); } } A、0 B、1 C、2 D、-1
5、下面的代码段执行之后count的值是什么? int count = 0; for (int i = 1; i < 4; i++) { count += i; } System.out.println(count); A、4 B、1 C、6 D、10
6、以下程序的运行结果为: 1. public class Conditional { 2. public static void main(String args [] ) { 3. int x = 4; 4. System.out.println( "value is " + 5. ((x > 4) ? 99.99 : 9)); 6. } 7. } A、输出:value is 99.99 B、输出: value is 9 C、输出: value is 9.0 D、在第5行出现编译错误
7、下列程序的运行结果? public class Test { public static void main(String a[]) { int x=3,y=4,z=5; if (x>3) { if (y<2) System.out.println("show one"); else System.out.println("show two"); } else { if (z>4) System.out.println("show three"); else System.out.println("show four"); } } } A、show one B、show two C、show three D、show four
8、以下程序调试结果 public class test { public static void main(String args[]) { int i=1, j=3; while (j>0) { j--; i++; } System.out.println(i); } } A、4 B、2 C、3 D、0
1、以下程序运行时输入: java Cycle hello two me 2 public class Cycle{ public static void main(String args[]){ System.out.println(args[1]); } } 则运行结果为? A、me B、hello C、two D、2
2、public class test { public static void main(String args[]) { int m=0; for ( int k=0;k<2;k++) method(m++); System.out.println(m); } public static void method(int m) { System.out.print(m); } } A、000 B、012 C、123 D、111
3、以下程序运行结果为: public class Q { public static void main(String argv[]) { int anar[]= new int[5]; System.out.println(anar[0]); } } A、出错: anar在未初始化前被引用 B、"null" C、0 D、5
4、下列程序的运行结果是: public class Test { public static void main(String args[]) { int m[]={1,2,3,4,5,6,7,8}; int sum = 0; for (int i=0;i<8;i++){ sum = sum + m[i]; if (i==3) break; } System.out.println(sum); } } A、3 B、6 C、36 D、10
2、以下程序编译和运行会发生什么 public class Q8 { int i = 20; static { int i = 10; } public static void main(String[] args) { Q8 a = new Q8(); System.out.println(a.i); } } A、编译错误,变量 'i' 定义2次. B、编译错误,静态初始化只能用于初始化目的 C、输出 10. D、输出 20.
3、给出如下类定义: public class test { test(int k) { } } 如果要创建一个该类的对象,正确的语句是: A、test obj1 = new test(); B、test obj1 = new test(5); C、test obj1 = new test('5 '); D、test obj1 = new test(3.4);
4、有如下代码: public class Person { … } 下列哪个符合该类的构造方法定义 A、public void Person() {…} B、public static void Person() {…} C、public Person() {…} D、public int Person() {…}
5、以下代码的输出结果? public class Test{ static int x=5; public static void main(String argv[]){ change(x); x++; System.out.println(x); } static void change(int m){ m+=2; } } A、7 B、6 C、5 D、8
6、设有如下程序: public class Test5 { public static void main (String args []) { /* This is the start of a comment if (true) { Test5 = new test5(); System.out.println("Done the test"); } /* This is another comment */ System.out.println ("The end"); } } 结果为? A、输出 "Done the test". B、程序输出"The end" C、程序编译错误. D、程序输出"Done the test"和 "The end"
7、给出下面的不完整的类代码: 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; } } 下面的哪些表达式可以加到构造方法中的"doing the same as..."处? A、Person(n,a); B、name=n;age=a; C、this(n,a); D、this(name,age);
8、考虑如下类: public class Test { int j,k; public Test(int j ) { this(j,0); } public Test(int j, int k) { this.j=j; this.k=k; } } 以下哪些可正确创建Test对象? A、Test t = new Test(); B、Test t = new Test(l); C、Test t = new Test(l, 2); D、Test t = new Test(l, 2, 3);
3、以下代码调试结果 class Base {} class Sub extends Base {} public class CEx{ public static void main(String argv[]){ Base b = new Base(); Sub s = (Sub) b; } } A、调试通过 B、编译异常 C、运行异常 D、运行没输出
4、如何定义一个不能有子类的类Key? A、class Key { } B、abstract final class Key { } C、final class Key { } D、native class Key { }
5、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 } } 在 // point x安排哪个语句合法? A、i = m; B、i = b; C、i = p.a; D、i = p.change(30);
6、如何能使程序调用Base类的构造方法输出"base constructor"; class Base{ Base(int i){ System.out.println("base constructor"); } Base(){ } } public class Sup extends Base{ public static void main(String argv[]){ Sup s= new Sup(); //One } Sup() { //Two } public void derived() { //Three } } A、在//One行之后放置Base(10); B、在//One行之后放置super(10); C、在//Two行之后放置super(10); D、在//Three行之后放置super(10);
7、以下程序的输出为? 1: class MyClass 2: { 3: static int maxElements; 4: 5: MyClass(int maxElements) 6: { 7: this.maxElements = maxElements; 8: } 9: 10: } 11: 12: public class Q19 13: { 14: public static void main(String[] args) 15: { 16: 17: MyClass a = new MyClass(100); 18: MyClass b = new MyClass(100); 19: 20: if(a.equals(b)) 21: System.out.println("Objects have the same values"); 22: else 23: System.out.println("Objects have different values"); 24: } 25: } A、在第20行出错. equals()方法未定义. B、编译通过,在运行时20行出现异常 C、输出 "Objects have the same values". D、输出 "Objects have different values"
10、设有如下代码: class Base{} public class MyCast extends Base{ static boolean b1=false; static int i = -1; static double d = 10.1; public static void main(String argv[]){ MyCast m = new MyCast(); Base b = new Base(); //Here } } 则在 //Here处插入哪个代码将不出现编译和运行错误。 A、b=m; B、m=b; C、d =i; D、b1 =i;
第七章测试
1、测试如下代码: public class Ref{ public static void main(String[] args) { StringBuffer sbl = new StringBuffer("Hello"); StringBuffer sb2 = new StringBuffer("Hello"); boolean result = sbl.equals(sb2); System.out.println(result); } } 下述哪条语句正确描述了程序编译和运行的行为? A、编译成功,输出为 true B、编译成功,输出为 1 C、编译成功,输出为 false D、编译成功,输出为 0
3、以下程序的调试结果为? 1. public class EqualsTest{ 2. public static void main(String args[]){ 3. Long LA = new Long(7); 4. Long LB = new Long(7); 5. if(LA==LB) System.out.println("Equal"); 6. else System.out.println("Not Equal"); 7. } 8. } A、程序在执行到第5行时出现异常 B、输出"Not Equal" C、输出"Equal" D、编译错误
4、有如下代码: public class Test{ public static void main(String args[]) { String str = new String("World"); char ch[] = {'H','e','l','l','o'}; change(str,ch); System.out.println(str + "and" + ch); } public static void change(String str, char ch[]) { str = "Changed"; ch[0] = 'C'; } } 运行后输出的结果是: A、World and Hello B、World and Cello C、World and CelloChanged and Hello D、Changed and Cello
5、已知代码: String s = "story";下列语句中合法的是: A、s += "books"; B、char c = s[1]; C、int len = s.length; D、String t = s.toLowerCase();
第八章测试
1、以下程序的编译和运行结果为? abstract class Base{ abstract public void myfunc(); public void another(){ System.out.println("Another method"); } } public class Abs extends Base{ public static void main(String argv[]){ Abs a = new Abs(); a.amethod(); } public void myfunc(){ System.out.println("My Func"); } public void amethod(){ myfunc(); } } A、输出结果为 My Func B、编译指示 Base 类中无抽象方法 C、编译通过,但运行时指示Base 类中无抽象方法 D、编译指示Base 类中的myfunc方法无方法体,没谁会喜欢该方法
2、以下代码的调试结果为? abstract class MineBase { abstract void amethod(); static int i; } public class Mine extends MineBase{ public static void main(String argv[]){ int[] ar = new int[5]; for(i = 0;i < ar.length;i++) System.out.println(ar[i]); } } A、输出5个0 B、错误:ar 未初始化就使用 C、错误:Mine 必须定义为抽象的 D、错误,i超出数组下标范围
5、考虑如下代码,其中包括一个内嵌类: public final class Test4 { class Inner { void test() { if (Test4.this.flag) { sample(); } } } private boolean flag = false; public void sample() { System.out.println("Sample"); } public Test4() { (new Inner()).test(); } public static void main(String args []) { new Test4(); } } 结果为? A、输出 "Sample" B、程序无输出,但正确终止 C、程序不能终止 D、程序编译失败
7、设有类定义如下: class InOut{ String s= new String("Between"); public void amethod(final int iArgs){ int iam=5; iam++; class Bicycle{ public void sayHello(){ //Here } } } public void another(){ int iOther; } } 以下哪些语句可以安排在//Here处 ? A、System.out.println(s); B、System.out.println(iOther); C、System.out.println(iam); D、System.out.println(iArgs);
9、设有如下代码: interface IFace{ } class CFace implements IFace{ } class Base{ } public class ObRef extends Base{ public static void main(String argv[]){ ObRef obj = new ObRef(); Base b = new Base(); Object obj1 = new Object(); IFace obj2 = new CFace(); //Here } } 则在 //Here处插入哪个代码将不出现编译和运行错误。 A、obj1=obj2; B、b=obj; C、obj=b; D、obj1=b;
5、检查下面的代码: class E1 extends Exception{} class E2 extends E1{} public class Quiz6_l{ public static void f(boolean flag) throws E1,E2{ if(flag) { throw new E1(); } else { throw new E2(); } } public static void main(String[] args) { try{ f(true); } catch(E2 e2) { System.out.println("Caught E2"); }catch(E1 e1) { System.out.println("Caught El"); } } } 对上面的程序进行编译、运行,下面的叙述哪个是正确的: A、由于Qoiz6_1.main方法中没有声明抛出异常E1、E2,所以编译会失败 B、由于针对E2的catch程序块是无法执行到的,所以编译会失败 C、编译成功,输出为: Caught El Caught E2 D、编译成功,输出为: Caught E1
6、设有如下代码段 1 String s = null; 2 if ( s != null & s.length() > 0) 3 System.out.println("s != null & s.length() > 0"); 4 if ( s != null && s.length() > 0) 5 System.out.println("s != null & s.length() > 0"); 6 if ( s != null || s.length() > 0) 7 System.out.println("s != null & s.length() > 0"); 8 if ( s != null | s.length() > 0) 9 System.out.println("s != null | s.length() > 0"); 哪些行将抛出空指针异常? A、2,4 B、6,8 C、2,4,6,8 D、2,6,8
7、当2个实际参数分别为4和0时,以下方法调用的执行结果为: public void divide(int a, int b) { try { int c = a / b; } catch (Exception e) { System.out.print("Exception "); } finally { System.out.println("Finally"); } } A、输出 Exception Finally B、输出 Finally C、输出 Exception D、无输出
8、检查下面的代码: class E1 extends Exception{ } class E2 extends E1 { } public class Quiz6_5{ public static void main(String[] args){ try{ throw new E1(); } // --X-- } } 下列语句,哪一个可以放到--X--位置,而且保证编译成功。 A、catch(Exception x){} B、catch(final Exceptionx){ } C、catch(El x){} D、catch(E2 x){}
9、检查下面的代码: class E1 extends Exception{ }; class E2 extends E1{ } class SuperQuiz6_2 { } public class Quiz6_3 extends SuperQuiz6_2{ public void f(Boolean flag) throws E1{ //一一X一一 } } 下列的语句,哪—个可以放到--X--位置,而且保证编译成功。 A、throw new Exception(); B、throw new El(); C、throw new E2(); D、throw new object();