public class AppletDemo extends Applet { StringBuffer buffer; public void init() {
buffer = new StringBuffer(); addItem(\初始化?\ }
public void start() {
addItem(\启动... \ }
public void stop() {
addItem(\停止运行... \ }
public void destroy() { addItem(\准备卸载...\ }
void addItem(String newWord) { System.out.println(newWord); buffer.append(newWord); repaint(); }
public void paint(Graphics g) {
g.drawString(buffer.toString(), 5, 15); } }
编译AppletDemo.java后,将AppletDemo.class嵌入HTML文件中,然后用AppletViewer命令打开该HTML文件,问:
(1)小应用程序在加载阶段,在屏幕上打印结果是什么? (2)关闭这个小应用程序时,在屏幕上打印结果是什么?
21、阅读下面的程序:
1) abstract class Base{
2) abstract public void myfunc(); 3) public void another(){
4) System.out.println(\5) } 6) }
7) public class Abs extends Base{
8) public static void main(String argv[]){ 9) Base b = new Abs(); 10) b.another(); 11) }
12) public void myfunc(){
13) System.out.println(\14) }
第 26 页 共 48 页
15) public void another(){ 16) myfunc(); 17) } 18) }
以上程序经编译后,运行结果是什么?
22、阅读下面程序代码,写出程序运行的输出结果。
1) class TestException{ 2) public static void main(String []args){ 3) try{ 4) callMethod(); 5) }catch(Exception e){ 6) System.out.print('a'); 7) } 8) System.out.println('b'); 9) } 10) static void createException(){ 11) throw new ArrayIndexOutOfBoundsException(); 12) } 13) static void callMethod(){ 14) try{ 15) createException(); 16) System.out.print('c'); 17) }catch(ArrayIndexOutOfBoundsException e){ 18) System.out.print('d'); 19) } 20) finally{ 21) System.out.print('e'); 22) } 23) System.out.print('f'); 24) } 25) }
23、阅读下面的程序:
1) class Super{
2) public int i=0; 3) public Super(){ 4) i=1; 5) } 6) }
7) public class Sub extends Super{ 8) public Sub(){ 9) i=2; 10) }
第 27 页 共 48 页
11) public static void main(String args[]){ 12) Sub s=new Sub();
13) System.out.println(s.i); 14) } 15) }
上面的程序经编译后,运行结果是什么?
24、阅读下面的程序,程序保存为Test.java:
1) public class Test {
2) public static void main(String[ ] args) { 3) int index=0;
4) while (index<=100) { 5) index+=10; 6) if (index==40) 7) break;
8) System.out.println(\9) } 10) } 11) }
上面的程序经编译,运行后输出的结果是什么?
25、阅读下面的程序:
1) class ValHold{
2) public int i = 10; 3) }
4) public class ObParm{
5) public static void main(String argv[]){ 6) ObParm o = new ObParm(); 7) o.amethod(); 8) }
9) public void amethod(){ 10) int i = 99;
11) ValHold v = new ValHold(); 12) v.i = 30; 13) another(v,i);
14) System.out.print(v.i); 15) }
16) public void another(ValHold v, int i){ 17) i = 0; 18) v.i = 20;
19) ValHold vh = new ValHold(); 20) v = vh;
21) System.out.print(v.i); 22) System.out.print(i);
第 28 页 共 48 页
23) } 24) }
上面的程序编译是否成功?如果编译出错,指出哪行出错,并说明理由;如果编译正确,运行结果是什么?
26、仔细阅读下面的程序代码,若经编译和运行后,请写出打印结果。 class StaticTest {
static int x = 1; int y;
StaticTest() {
y++; }
public static void main(String args[]) { StaticTest st = new StaticTest(); System.out.println(\
System.out.println(\ st = new StaticTest();
System.out.println(\
System.out.println(\}
static { x++; } }
27、阅读下面的程序Test.java:
1) import java.io.*; 2) public class Test{
3) public static void main(String argv[]){ 4) Test t = new Test();
5) System.out.println(t.fliton()); 6) }
7) public int fliton(){ 8) try{
9) FileInputStream din = new FileInputStream(\10) din.read();
11) }catch(IOException ioe){
12) System.out.println(\13) return -1; 14) }
15) finally{
16) System.out.println(\17) }
18) return 0;
第 29 页 共 48 页
19) } 20) }
如果文件test.txt与Test.java在同一个目录下,test.txt中仅有一行字符串“hello world!”,上面的程序编译是否成功?如果编译出错,指出哪行出错,并说明理由;如果编译正确,运行结果是什么?
28、仔细阅读下面的程序代码,写出程序运行的输出结果。 class Test1 {
private int i = 1; public class Test11{ private int i = 2;
public void methodI(int i) {
i++;
this.i++;
Test1.this.i++;
System.out.println(\ System.out.println(\
System.out.println(\ } }
Test11 ic=new Test11(); public void increaseI(int k) {
ic.methodI(k); }
public static void main(String [] args) {
Test1 oc=new Test1(); oc.increaseI(20); } }
29、仔细阅读下面的程序代码,若经编译和运行后,请写出打印结果。 public class Test {
public static void main(String args[]){ int [ ] a = {10, 20, 30, 40, 50}; int s =0;
for (int c: a) s +=c;
System.out.print(s ); }
第 30 页 共 48 页