以下哪些选项使得运行―java Sup‖时打印―base constructor‖? a) 在―//One‖的地方加入一行―Base(10);‖ b) 在―//One‖的地方加入一行―super(10);‖ c) 在“//Two”的地方加入一行“super(10);” d) 在―//Three‖的地方加入一行―super(10);‖
7.java.lang.Class类没有public类型的构造方法,这句话对吗 [答案] 对 8.以下代码能否编译通过? class A { public A() {}
public A(int i) { this(); } } class B extends A {
public boolean B(String msg) { return false; } } class C extends B { private C() { super(); }
public C(String msg) { this(); } public C(int i) {} } [答案] 可以编译通过。 第 12章 内部类
1.在类的方法内部定义的内部类可以访问外部类的所有成员变量,这句话对吗?对。 2.内部类和外部类一样,可以实现接口,继承其他的类或被其他的类继承。这句话对吗? 对。
3.以下代码能否编译通过,假如能编译通过,运行―java Outer‖时得到什么打印结果? public class Outer{
public String name = \
public static void main(String argv[]){ Inner i = new Inner(); i.showName(); }//End of main private class Inner{
String name =new String(\ void showName(){
System.out.println(name); } }//End of Inner class }
[答案] 编译出错。应该把―Inner i = new Inner();‖改为―Inner i = new Outer().new Inner();‖ 4.外部类不能被private、protected修饰,有些内部类可以被private、protected修饰。这句话对吗? [答案] 对
5.匿名类也需要声明构造方法,这句话对吗?[答案] 错。 6.以下哪些是合法的代码? a)public class Outer { String a;
public class Inner { String b;
public void innerMethod() {
System.out.println(\
21
System.out.println(\ } public void createInner() { Inner i=new Inner(); i.innerMethod(); }} b)public class Outer { String a;
public class Inner { String b;
public void innerMethod() {
System.out.println(\ System.out.println(\ public void createInner() {
Outer.Inner i=new Outer.Inner(); i.innerMethod(); }} c)public class Outer { String a; int k=1;
public static class Inner { String b;
public void innerMethod() {
System.out.println(\ System.out.println(\ } } public void createInner() {
Outer.Inner i=new Outer.Inner(); i.innerMethod();
System.out.println(\d)public class Outer { static String a; static int k; public Outer() { k++; }
public class Inner { String b;
public void innerMethod() {
System.out.println(\ System.out.println(\ } public void createInner() {
Outer.Inner i=new Outer.Inner(); i.innerMethod();
System.out.println(\
7.对于以下代码,method2()方法可否访问变量x、y或z? class A { public int x;
22
private int y; class B {
protected void method1() {} class C {
private void method2() {}} } class D extends A { public float z; } }
[答案] 可以直接访问变量x和y,但不能直接访问变量z。
8.对于以下类,在Inner类的method()方法中,哪些语句是合法的? public class Outer { private int a;
public static void main(String[] args){} public void go(int w,final int z) { int p=w-z;
final int q=w+z; class Inner {
public void method (){
System.out.println(\ //line1 System.out.println(\ //line2 System.out.println(\ //line3 System.out.println(\ //line4 System.out.println(\ //line5 }} Inner that=new Inner(); that.method(); } } [答案] line2 line4 line5 第 13章 多线程与并发
1.以下代码能否编译通过,假如能编译通过,运行时得到什么打印结果? public class WhatHappens implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); }
public void run() {
System.out.println(\ } }
[答案] 编译出错。在main()方法中不能直接引用―this‖。
2.以下代码能否编译通过,假如能编译通过,运行时得到什么打印结果? public class Bground extends Thread{ public static void main(String argv[]){ Bground b = new Bground(); b.run(); }
public void start(){
for (int i = 0; i <10; i++){
System.out.println(\
23
[答案] 编译和运行都通过。运行时没有任何打印结果。 3.以下哪些属于java.lang.Thread类的方法? a) yield()
b) sleep(long msec) c) go() d) stop() e) suspend()
4.在哪些情况线程会被阻塞? [答案]
在以下情况,线程被阻塞:
(1)当线程处于运行状态,如果执行了某个对象的 wait()方法,Java虚拟机就会把线 程放到这个对象的等待池中,线程被阻塞。
(2)当线程处于运行状态,试图获得某个对象的同步锁时,如果该对象的同步锁已经 被其它线程占用,Java虚拟机就会把这个线程放到这个对象的锁池中,线程被阻塞。。 (3)线程执行了sleep()方法,或者调用了其他线程的join()方法,或者发出了I/O请求, 就会被阻塞。
5.有哪些原因会导致线程死亡?
[答案] 当线程退出run()方法,就进入死亡状态。线程有可能是正常执行完run()方法而退出,也有可能是遇到异常而退出,只要退出run()方法,线程就死亡。 此外,线程的stop()方法也会让线程死亡。 6.对于以下代码:
public class RunTest implements Runnable { public static void main(String[] args) { RunTest rt = new RunTest(); Thread t =new Thread(rt); //此处插入一行} public void run() {
System.out.println(\ } void go() { start(1);}
void start(int i) {} }
下面哪些语句放到以上插入行,将使程序打印―running‖? a) System.out.println(\ b) t.start(); c) rt.go(); d) rt.start(1);
7.以下代码能否编译通过,假如能编译通过,运行―java Test‖时得到什么打印结果? class RunHandler{ public void run(){
System.out.println(\ public class Test {
public static void main(String[] args) {
Thread t = new Thread(new RunHandler()); t.start(); } }
24
[答案] 编译出错。RunHandler类没有实现Runnable接口,因此不能作为Thread构造方法的参数。
第14章 数组
1.以下哪段代码能显示最后一个命令行参数,并且当不存在命令行参数时,不会抛出异常
a) public static void main(String args[]) { if (args.length != 0)
System.out.println(args[args.length-1]);} b) public static void main(String args[]) { try { System.out.println(args[args.length]); }
catch (ArrayIndexOutOfBoundsException e) {} } c)public static void main(String args[]) { int ix = args.length; String last = args[ix];
if (ix != 0) System.out.println(last); } d)public static void main(String args[]) { int ix = args.length-1;
if (ix > 0) System.out.println(args[ix]);} e)public static void main(String args[]) {
try { System.out.println(args[args.length-1]); } catch (NullPointerException e) {}}
2.执行完以下代码后,数组arr的各个元素的取值是什么? int[] arr = {1, 2, 3}; for (int i=0; i < 2; i++) arr[i] = 0;
[答案] arr[0]=0, arr[1]=0, arr[2]=3
3.以下代码能否编译通过,假如能编译通过,运行时出现什么情况? public class MyAr{
public static void main(String argv[]){ int[] i = new int[5];
System.out.println(i[5]); } }
[答案] 运行时抛出ArrayIndexOutOfBoundsException。 4.如何获得数组myarray的长度? a) myarray.length() b) myarray.length c) myarray.size d) myarray.size()
5.以下哪些是合法的数组声明和初始化? a) int x[] = {1,2,3};
b) int []x[] = {{1,2,3},{1,2,3}}; c) int x[3] = {1,2,3}; d) int []x = {0,0,0}; e) char c[] = {'a', 'b'}; f) int x[]=new int[]{1,2,3};
25