1、java复习题答案复习题1一、 选择题答案 选择第1题B选择第2题A选择第3题C选择第4题B选择第5题A选择第6题A选择第7题B选择第8题C选择第9题B选择第10题B选择第11题C 二、多项选择题答案 多项选择第1题BD多项选择第2题AE多项选择第3题AC多项选择第4题AE多项选择第5题AE多项选择第6题DEF多项选择第7题AC多项选择第8题BC多项选择第9题AB多项选择第10题CD多项选择第11题CD多项选择第12题ACD多项选择第13题ABD 三、 填空题答案 填空第1题x=10,a=3,b=4,c=5填空第2题java.util填空第3题(public )(static )(void)(
2、main)(String args)填空第4题1填空第5题26 四、 编程题答案1public class HelloWorldpublic static void main(String args)System.out.println(Hello,World!);import java.awt.Graphics;import java.applet.Applet;public class HelloWorld extends AppletString s;public void init()s=Hello World!;public void paint(Graphics g)g.drawS
3、tring(s,25,25);2class yanghuipublic static void main (String args)int i,j;int yhlevel=10;int yanghui;System.out.println(杨晖三角形:);yanghui=new intyhlevel;for(i=0;iyanghui.length;i+)yanghuii=new inti+1;yanghui00=1;for (i=1; iyanghui.length;i+)yanghuii0=1;for(j=1;jyanghuii.length-1;j+)yanghuiij=yanghuii-
4、1j-1+yanghuii-1j;yanghuiiyanghuii.length-1=1;for (i=0; iyanghui.length;i+)for(j=0;jyanghuii.length;j+)System.out.print(yanghuiij+ );System.out.println(); 输出结果是:杨晖三角形:1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 13im
5、port java.io.*;public class MemoControllerFileOutputStream fos;OutputStreamWriter osw;BufferedWriter bw;public MemoController()tryfos=new FileOutputStream(memo.txt,true); osw=new OutputStreamWriter(fos);bw=new BufferedWriter(osw);catch(FileNotFoundException e);public synchronized void append(String
6、s)trybw.write(s,0,s.length(); bw.flush();bw.close();osw.close();fos.close();catch(IOException e)public static void main(String args)MemoController mmc=new MemoController();mmc.append(I am xubin );4import java.io.*; class Phones static FileOutputStream fos;public static final int lineLength = 81;publ
7、ic static void main(String args) throws IOExceptionbyte phone = new bytelineLength;byte name = new bytelineLength;int I;try fos = new FileOutputStream(phone.numbers);catch(FileNotFoundException e) while (true)System.err.println(Enter a name (enter done to quit);readLine(name);if (done.equalsIgnoreCa
8、se(new String(name,0,0,4)break; System.err.println(Enter the phone number); readLine(phone);for (int i=0;phonei!= 0;i+) fos.write(phonei);fos.write(,);for (int i=0;namei!= 0;i+)fos.write(namei); fos.write(n);fos.close(); private static void readLine(byte line) throws IOException int i=0,b=0; while (
9、ilineLength-1)&(b=System.in.read()!=n)linei+ = (byte)b; linei=(byte) 0; 复习题2四、 编程题答案1public class HelloWorldpublic static void main(String args)System.out.println(Hello,World!);import java.awt.Graphics;import java.applet.Applet;public class HelloWorld extends AppletString s;public void init()s=Hello
10、 World!;public void paint(Graphics g)g.drawString(s,25,25);2class yanghuipublic static void main (String args)int i,j;int yhlevel=10;int yanghui;System.out.println(杨晖三角形:);yanghui=new intyhlevel;for(i=0;iyanghui.length;i+)yanghuii=new inti+1;yanghui00=1;for (i=1; iyanghui.length;i+)yanghuii0=1;for(j
11、=1;jyanghuii.length-1;j+)yanghuiij=yanghuii-1j-1+yanghuii-1j;yanghuiiyanghuii.length-1=1;for (i=0; iyanghui.length;i+)for(j=0;jyanghuii.length;j+)System.out.print(yanghuiij+ );System.out.println(); 输出结果是:杨晖三角形:1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56
12、 70 56 28 8 1 1 9 36 84 126 126 84 36 9 13import java.io.*;public class MemoControllerFileOutputStream fos;OutputStreamWriter osw;BufferedWriter bw;public MemoController()tryfos=new FileOutputStream(memo.txt,true); osw=new OutputStreamWriter(fos);bw=new BufferedWriter(osw);catch(FileNotFoundExceptio
13、n e);public synchronized void append(String s)trybw.write(s,0,s.length(); bw.flush();bw.close();osw.close();fos.close();catch(IOException e)public static void main(String args)MemoController mmc=new MemoController();mmc.append(I am xubin );4import java.io.*; class Phones static FileOutputStream fos;
14、public static final int lineLength = 81;public static void main(String args) throws IOExceptionbyte phone = new bytelineLength;byte name = new bytelineLength;int I;try fos = new FileOutputStream(phone.numbers);catch(FileNotFoundException e) while (true)System.err.println(Enter a name (enter done to
15、quit);readLine(name);if (done.equalsIgnoreCase(new String(name,0,0,4)break; System.err.println(Enter the phone number); readLine(phone);for (int i=0;phonei!= 0;i+) fos.write(phonei);fos.write(,);for (int i=0;namei!= 0;i+)fos.write(namei); fos.write(n);fos.close(); private static void readLine(byte l
16、ine) throws IOException int i=0,b=0; while (ilineLength-1)&(b=System.in.read()!=n)linei+ = (byte)b; linei=(byte) 0; 二、多项选择题答案 多项选择第1题ACD多项选择第2题AB多项选择第3题AD多项选择第4题CD多项选择第5题AD多项选择第6题AC多项选择第7题AD多项选择第8题BC多项选择第9题BD多项选择第10题BCD多项选择第11题BD多项选择第12题ABD多项选择第13题BD多项选择第14题ABD 三、 填空题答案 填空第1题Collection,List,Map,Set填
17、空第2题7填空第3题因为在A的main方法中,还没有A的实例就要试图生成内部类的实例 四、 编程题答案编程第1题import java.io.*;public class Pointint x,y;public Point(int x,int y)this.x = x;this.y = y;public Point getPoint()Point tempPoint = new Point(x,y);return tempPoint;public void setPoint(Point point)this.x = point.x;this.y = point.y;public static
18、void main(String args)Point Point1 = new Point(3,4);System.out.println(Point1:+(+Point1.x+,+Point1.y+);Point Point2 = Point1.getPoint();System.out.println(Point2:+(+Point2.x+,+Point2.y+);Point Point3 = new Point(5,6);Point1.setPoint(Point3);System.out.println(Point1:+(+Point1.x+,+Point1.y+); 编程第2题im
19、port java.io.*;class FileCopypublic static void main(String args)FileInputStream in;FileOutputStream out;if (args.length2)System.out.println(Usage: java copy srcfile destfile);System.exit(-1); try in = new FileInputStream(args0);out = new FileOutputStream(args1);copyFile(in,out);catch (Exception e)S
20、ystem.out.println(e);private static void copyFile(FileInputStream in, FileOutputStream out)int length;byte buf = new byte1024;trywhile (length=in.read(buf,0,1024)!=-1)out.write(buf, 0, length); catch (Exception e)System.out.println(Error:+e);System.exit(-1);编程第3题import java.awt.*;import java.awt.eve
21、nt.*;import java.applet.*;import java.util.*;public class TimeViewer extends Applet implements ActionListener, Runnable Thread timer;TextField in, out;Button bb;Panel p1, p2, p3;boolean state;public void init() in = new TextField(20); out = new TextField(20);bb = new Button(Current Time:);p1 = new P
22、anel();p2 = new Panel();p3 = new Panel();setLayout(new GridLayout(3, 1);setSize(200,100);p1.add(in);p2.add(bb);p3.add(out);add(p1);add(p2);add(p3);bb.addActionListener(this); timer = new Thread(this);state = true;timer.start(); public void actionPerformed(ActionEvent e) /out.setText(in.getText();out
23、.setText(currentTime(); public void run() while(true) try timer.sleep(1000); catch (InterruptedException e) in.setText(currentTime();String currentTime() Date now = new Date();String str = now.getHours() + : + now.getMinutes() + : + now.getSeconds();return str;复习题3一、 选择题答案 选择第1题C选择第2题D选择第3题D选择第4题C选择第5题B选择第6题B选择第7题C选择第8题D选择第9题A选择第10题A选择第11题B选择第12题B 二、多项选择题答案 多项选择第1题AB多项选择第2题BD多项选择第3题ADE多项选择第4题AD多项选择第5题BCD多项选择第6题AB多项选择第7题ABCD多项选择第8题ABCD多项选择第9题BC多项选择第10题BC多项选择第11题AC 三、 填空题答案 填空第1题protected;default;public填空第2题Object填空第3题java.lang.Character;java.lang.Boolean填空第4题What a pleasure! I
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1