《Java程序设计》练习题 下载本文

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 页

}

30、仔细阅读下面的程序代码,若经编译和运行后,请写出打印结果。 class myException extends Exception{} public class Sample{ public void foo(){ try{

System.out.print(1); bar();

System.out.print(2); }catch(myException e){ System.out.print(3); } finally{

System.out.print(4); } }

public void bar() throws myException{ throw new myException(); }

public static void main(String args[]){ Sample s=new Sample(); s.foo(); } }

31、请简要画出编译运行下面程序的界面效果图。 import java.awt.*; import javax.swing.*;

public class ColorSelect extends JFrame { private JButton ok, cancel;

private JCheckBox background, foreground; private JComboBox colorList; private JPanel panel, panel2; private Container c; public ColorSelect(){

super( \ c=getContentPane();

c.setLayout(new BorderLayout());

colorList = new JComboBox(); colorList.addItem( \

c.add( colorList, BorderLayout.NORTH ); panel = new JPanel();

第 31 页 共 48 页

background = new JCheckBox( \ foreground = new JCheckBox( \ panel.add( background ); panel.add( foreground );

c.add( panel, BorderLayout.CENTER ); ok = new JButton( \

cancel = new JButton( \ panel2 = new JPanel(); panel2.add( ok ); panel2.add( cancel );

c.add( panel2, BorderLayout.SOUTH ); setSize( 300, 125 ); setVisible(true); }

public static void main ( String args[] ){ ColorSelect app = new ColorSelect();

app.setDefaultCloseOperation( EXIT_ON_CLOSE ); } }

32、仔细阅读下面的程序,简单的画出GUI的界面 import java.awt.*;

public class Test extends Frame { public Test() { super(\

setLayout(new BorderLayout());

add(new Button(\ add(new Button(\ add(new Button(\ add(new Button(\ add(new Button(\ }

public static void main(String args[]){ Test t=new Test(); t.pack(); t.show(); } }

33、仔细阅读下面的程序,写出程序的执行顺序(写出编号): public class UsingExceptions {

public static void main( String args[] ){ try{

method1(); // 1

第 32 页 共 48 页