搜索
您的当前位置:首页JAVA面试题(多个公司)

JAVA面试题(多个公司)

来源:飒榕旅游知识分享网
sunJava习题

一、 单选题(40道)

1. 事件监听接口中的方法的返回值是? A. int B. String C. void D. Object

E. AWTEvent

2. 设 x=1 , y=2 , z = 3,则表达式 y+=z--/++x-x的值是?(选择1项) A. 0 B. 1 C. 2

D. 3

3. 在类MyClass中定义了方法getResult(),其中包含语句: double myresult=Math.sqrt(1000);

为了使此程序可用,需要导入什么包?(选择1项) A. java.lang.* B. java.Math.*

C. java.util.Math.*; D. 以上皆非

4. 在java中,哪一个关键字使类不能派生出子类。(选择1项) A. final B. public

C. private D. volatile 5. 在Java中,要处理Button类对象的事件,以下各项中,哪个是可以处理这个事件的接口。(选择一项) A. FocusListener B. ComponentListener C. WindowListener

D. ActionListener

6. 编译,运行下列Java代码后的结果是()。(选择一项) public class Test{

public static void main(String args []){ int age;

age=age+1;

System.out.println(“The age is”+age); } }

A. 编译,运行后没有输出

B.编译,运行后输出:The age is 1

C.能通过编译,但运行时产生错误 D. 不能通过编译

7. 在Java语言中,类Worker是类Person的子类,Worker的构造方法中有一句“super()”,该语句()。 (选择一项)

A.调用类Worker中定义的super()方法 B.调用类Person中定义的super()方法 C.调用类Person的构造方法 D.语法错误

8. 下列整型的最终属性 i 的定义中,正确的是。(选择1项) A. final i;

B. static int i;

C. static final int i=234; D. final float i=3.14f;

9. 算术表达式1+2/3-4*5的值为。(选择1项) A. 1 B. -19 C. -5

D. 0

10. 下面属于电子商务产生的条件的是哪个? A、 计算机的广泛应用,网络的普及和成熟

B、 信用卡的普及和应用,电子交易安全协议的确定 C、 政府的支持与推动 D、 以上都是

11. 电子商务有许多分类,其中 B to C 是指( )。

A、企业与企业 B、企业与消费者 C、消费者与消费者 D、企业与政府 12. 电子商务有许多分类,其中 B to B是指( )。

A、企业与企业 B、企业与消费者 C、消费者与消费者 D、企业与政府 13. 按照使用网络类型分类,以下不属于当前电子商务的主要形式是( )。 A、EDI商务 B、Internet商务 C、Intranet商务 D、WWW商务 14. Intranet一般称之为( )。

A、外联网 B、 广域网 C、局域网 D、企业内部网

15. 在一个由客户端,协议防火墙、中间层(DMZ)、域防火墙及企业内部网构成的电子商务系统中,一

般情况下,协议防火墙会允许哪个/些协议通过?

A. IIOP B. FTP C. HTTP/HTTPS D. JAVA

16. 下面哪一个关于IBM电子商务应用框架的阐述是正确的? A. 它是一种开放的、不安全的体系结构, B.系统的当机时间不会影响雇员的生产率

C.该框架中的后台数据库只支持DB2

D. 它是基于工业标准的,能够提供好的灵活性和可扩展性

17. 一个电子商务解决方案由客户端、协议防火墙、应用服务器、域防火墙和企业内部网几 部分共同组成。企业认为这种解决方案比较昂贵。为了节省花费,企业管理者希望能够除去第二层防火

墙(即域防火墙)。在需求分析阶段,企业相关人员指出安全性是非常重要的。 在这种需求下,第二

层防火墙需要删除么?

A. 不能删除。需要第二层防火墙保障安全性。防火墙必须成对安装以保证它的功能。 B.不能删除。使用第二层防火墙可以改善安全。第二层防火墙能够防止外部 INTERNET用户直接访问企

业内部服务器。

C.可以删除。使用两层防火墙可以增加整个系统的吞吐量,尽管如此,但是一层防火墙就足够了。

D. 可以删除。使用两层防火墙可以实现高可用性,尽管如此,但是一层防火墙就足够了。 18. 下面的技术中,哪一个是客户端脚本语言?

A. ASP B. PHP C. Perl D. VBScript

19.有一个电子商务应用,它需要使两个公司间的信息进行交互。这两个公司的数据分别存放在 DB2数据

库中和Lotus 中。在IBM电子商务框架中,会推荐使用下面哪一种标准来展现数据? A. EDI B. IIOP C. HTML D. XML

20. 下面那种技术可以与基于浏览器的WEB应用共同协作,使得某些错误能够直接在终端用户的客户端处

理而无需在WEB服务器端处理? A. XML B. CGI C. SGML D. JavaScript

21. 作为电子商务解决方案的一部分,客户需要使用自己定义的标记集来标记数据。下面哪一种技术是基

于SGML的,并能满足客户的这一需求? A. XML B. HTML C. DHTML D. JavaScript 22. 哪个关键字可以抛出异常?

A transient B finally C throw D static

23. Main()方法的返回类型是: A int B void C boolean

D static

24. 哪个关键字可以对对象加互斥锁? A transient B synchronized C serialize

D static

25. 下列哪些语句关于内存回收的说明是正确的? A 程序员必须创建一个线程来释放内存; B 内存回收程序负责释放无用内存

C 内存回收程序允许程序员直接释放内存

D 内存回收程序可以在指定的时间释放内存对象

26. 欲构造ArrayList类的一个实例,此类继承了List接口,下列哪个方法是正确的 ? A ArrayList myList=new Object(); B List myList=new ArrayList();

C ArrayList myList=new List(); D List myList=new List();

27. paint()方法使用哪种类型的参数? A Graphics B Graphics2D C String D Color

28. 指出正确的表达式 A byte=128; B Boolean=null; C long l=0xfffL; D double=0.9239d;

29. 要从文件\" file.dat\"文件中读出第10个字节到变量C中,下列哪个方法适合? A FileInputStream in=new FileInputStream(\"file.dat\"); in.skip(9); int c=in.read(); B FileInputStream in=new FileInputStream(\"file.dat\"); in.skip(10); int c=in.read(); C FileInputStream in=new FileInputStream(\"file.dat\"); int c=in.read();

D RandomAccessFile in=new RandomAccessFile(\"file.dat\"); in.skip(9); int c=in.readByte();

30. 容器被重新设置大小后,哪种布局管理器的容器中的组件大小不随容器大小的变化而改变? A CardLayout B FlowLayout C BorderLayout

D GridLayout

31. 下列哪些不属于软件维护阶段?

A. 诊断和改正在使用过程中发现的软件错误 B.诊断和改正在开发过程中发现的软件错误

C.根据用户需求进行软件升级

D. 修改软件为将来的维护活动预先做准备 32. 下列那种java组件为容器组件 A. List列表框

B. Choice下拉式列表框 C. Panel面板

D. MenuItem命令式菜单项

33. 如下那种Java的控件将不会引发动作事件(ActionEvent)? A. Button B. MenuItem C. Panel

D. CheckboxMenuItem

34. 编译java Applet源程序文件将产生相应的结果文件,这些结果文件的扩展名是什么? A. .java B. .class

C. .html D. .exe

35. 将一个十六进制数赋值给一个long类型的变量,正确的表达式是: A. long number = 345L; B. long number = 0345;

C. long number = 0345L; D. long number = 0x345L.

36. JPanel组件的默认布局管理器是哪一项? A. GridLayout B. CardLayout C. BorderLayout

D. FlowLayour

37. 下列哪一项不属于面向对象程序设计的基本要素? A. 类 B.对象 C.方法

D. 安全

38. 下面程序的输出结果是。 public class ex2 {

public static void main(String[] args) {

for(int cnt=0;cnt<10;cnt++) { if(cnt==5) break;

System.out.print(cnt); } }

}

A. 0 1 2 3 4 B. 6 7 8 9

C. 0 1 2 3 4 6 7 8 9 D. 5

39. 给定下面的代码片段: 1) String str = null;

2) if ((str != null) && (str.length() > 10)) { 3) System.out.println(\"more than 10\"); 4) }

5) else if ((str != null) & (str.length() < 5)) {

6) System.out.println(\"less than 5\"); 7) }

8) else { System.out.println(\"end\"); }

哪些行会导致错误?

A line 1 B line 2 C line 5 D line 8

40. 下面哪种注释方法能够支持javadoc命令: A /**...**/ B /*...*/ C //

D /**...*/

二、 多选题(40道)

1.下面代码输出的结果是? public class MyFor{

public static void main(String argv[]){ int i;

int j;

outer:for (i=1;i <3;i++)

inner: for(j=1; j<3; j++) { if (j==2)

continue outer;

System.out.println(\"Value for i=\" + i + \" Value for j=\" +j); } } }

A Value for i=1 value for j=1 B Value for i=2 value for j=1 C Value for i=2 value for j=2 D Value for i=3 value for j=1

2.那些方法可以插入到程序中注释片段位置? class Base{

public void amethod(int i) { } }

public class Scope extends Base{

public static void main(String argv[]){} //Method Here(选择项插入位置) }

A void amethod(int i) throws Exception {} B void amethod(long i)throws Exception {} C void amethod(long i){}

D public void amethod(int i) throws Exception {}

3.在下面代码中那些修饰符可以放在XX位置 public class MyClass1 {

public static void main(String argv[]){ } /*修饰符位置 XX */ class MyInner {} }

A public B private C static D friend

4.根据程序代码选择所有返回为true的表达式

public static void main(String args[]){ Float f=new Float(4.2f); Float c;

Double d=new Double(4.2); float fl=4.2f; c=f; }

A f.equls(d) B c==f

C c==d D c.equls(f)

5.哪两种方法不能直接引起一个线程中断执行 A sleep(); B stop(); C yield(); D wait(); E notify(); F notifyAll()

G synchronized()

6.重载类hai的构造方法,下面哪些是正确的 A public void hai(int a) B hai(int a,int b)

C public hai(int a) D int hai(int c, int d) E int hai() F int hai(String s)

7.覆盖(重写)方法void hai(int a,int b),下面那些是正确的 A public void hai(int a,int b) B protected void hai(int a,int b) C public hai(int a) D int hai(int c, int d) E int Hai() F int hai(String s)

8.如果在try代码块中没有异常发生,则会输出那些结果 public static void main(String args[]){ try{

System.out.println(\"1\"); /////AA

}catch (RuntimeException x){ System.out.println(\"2\"); return;

}catch(Exception x){ System.out.println(\"3\"); return;

}finally{

System.out.println(\"4\"); }

System.out.println(\"5\");

}

A 1 B 2 C 3 D 4 E 5

9.下面表达式中结果相同的是 A 3/2 B 3<2 C 3*4 D 3<<2

10.下面表达式哪两个的值相等 A 16<<2 B 16/3*2 C 16>>2 D 16<<<2 E 16>>>2 F 16/2

11.下面那些线程状态事务是正确的? A 从ready到running

B 从running到ready C 从running到waiting D 从waiting到running E 从waiting到ready F 从ready到waiting

12.下面代码中s不能是哪些数据类型 switch(s) {

default: System.out.println(\"Best Wishes\"); }

A byte B long C float D double

13.下面选项中那些是有效的申明? A char c= ' \\' ' B char c= \"cafe\" C char c= '\쫾' D char c= '\Ā1' E char c= '0x001'

14.如果定义String s=”example”,下面语句哪些会编译? A s>>=2; B int i = s.length(); C s+=3; D char c= s[3]; E s= s +\"abcd\";

15.下面什么条件下一个正在运行的线程会中断 A 当一个中断异常发生

B 当一个高优先级线程准备好(runnable状态) C 当创建一个新的线程 D 当stop方法被调用

16.下面对数组申明那些是正确的? A int i[][]=new int[10,10]; B int i[10][10]=new int[][]; C int i [][]=new int[10][]; D int[]i[]=new int[10][10];

E int [][]i= new int[10][10];

17.下面那些是正确的为50个字符申明的数组? A char c[][]; B String []s; C String s[]; D String s[50]; E Object s[50];

18.下面那些不能添加到一个容器内?

A applet B container C menu item D panel E component

19.下面哪些是合法的标识符: A.

$persons

B.TwoUsers C. *point D. this

E. _endline

20.下面关于使用\"<<\"和 \">>\"操作符的哪些结果是对的? A. 1010 0000 0000 0000 0000 0000 0000 0000 >>

4的结果是 0000 1010 0000 0000 0000 0000 0000 0000 B. 1010 0000 0000 0000 0000 0000 0000 0000 >>

4的结果是

1111 1010 0000 0000 0000 0000 0000 0000

C. 1010 0000 0000 0000 0000 0000 0000 0000 >>>

4的结果是 0000 1010 0000 0000 0000 0000 0000 0000 D. 1010 0000 0000 0000 0000 0000 0000 0000 >>>

4的结果是 1111 1010 0000 0000 0000

0000

0000 0000

21.执行下列代码后,哪个结论是正确的 String[] s=new String[10]; A s[10]为 \"\"; B s[9]为 null; C s[0]为 未定义 D s.length 为10

22.下面哪些不是java的基本数据类型? A. short B. Boolean C. Int

D. float

23.下面有关java代码安全性的叙述哪些是对的。

A. 字节码校验器加载查询执行需要的所有类。 B.运行时解释器执行代码。

C.在运行时,字节码被加载,验证然后在解释器里面运行。

D. 类加载器通过分离本机文件系统的类和从网络导入的类增加安全性。 24. \"result\"是一个boolean型的变量,下面的哪些表达式是合法的? A. result = true;

B. if ( result ) { // do something... } C. if ( result!= 0 ) { // so something... } D. result = 1

25.下面的表达式哪个是正确的? A String s=\"你好\";int i=3; s+=i;

B String s=\"你好\";int i=3; if(i==s){ s+=i}; C String s=\"你好\";int i=3; s=i+s; D String s=\"你好\";int i=3; s=i+;

E. String s=null; int i=(s!=null)&&(s.length>0)?s.length():0; 26.选出合理的标识符 A _sys1_lll B 2mail C $change

D class

27.哪个布局管理器使用的是组件的最佳尺寸( preferred size) A FlowLayout B BorderLayout C GridLayout D CardLayout

E.GridBagLayout

28.下列哪个方法可用于创建一个可运行的类?

A public class X implements Runable{ public void run(){ ......} } B public class X implements Thread{ public void run(){ ......} } C public class X implements Thread{ public int run(){ ......} } D public class X implements Runable{ protected void run(){ ......} } E.public class X implements Thread{ public void run(){ ......} }

29.下面哪个方法可以在任何时候被任何线程调用? A notify() B wait()

C notifyAll() D sleep() E.yield()

F.synchronized(this)

30.给出下列代码: switch(x){

case 1:System.out.println(“Test 1”);break; case 2:

case 3:System.out.println(“Test 2”);break; default:System.out.println(“end”); }

x为什么值时,会输出Test 2?

A. 0 B. 1 C. 2 D. 3

31.下面关于变量及其范围的陈述哪些是对的? A. 实例变量是类的成员变量。

B.实例变量用关键字static声明。

C.在方法中定义的局部变量在该方法被执行时创建 D. 局部变量在使用前必须被初始化。 32.下面关于继承的哪些叙述是正确的? A.在java中只允许单一继承。

B.在java中一个类只能实现一个接口。

C.在java中一个类不能同时继承一个类和实现一个接口。 D.

java的单一继承使代码更可靠。 33.给定下列代码: public class Parent {

public int addValue( int a, int b) { int s; s = a+b; return s;

} }

class Child extends Parent { }

下列哪些方法可以作为Child类的方法?

A. int addValue( int a, int b ){// do something...} B. public void addValue (){// do something...} C. public int addValue( int a ){// do something...}

D. public int addValue( int a, int b )throws MyException {//do something...} 34.下面的哪些程序片断可能导致错误? A. String s = \"Gone with the wind\"; String t = \" good \";

String k = s+ t;

B. String s = \"Gone with the wind\"; String t; t = s[3] + \"one\";

C. String s = \"Gone with the wind\"; String standard = s.toUpperCase(); D. String s = \"home directory\";

String t = s - \"directory\";

35.下列哪些接口在Java中没有定义相对应的Adapter类? A. MouseListener B. KeyListener C. ActionListener

D. ItemListener E. WindowListener

36.如果下列的方法能够正常运行,在控制台上将显示什么? public void example(){ try{

unsafe();

System.out.println(\"Test1\"); }

catch(SafeException e)

{System.out.println(\"Test 2\");}

finally{System.out.println(\"Test 3\");} System.out.println(\"Test 4\"); } A Test 1 B Test 2 C Test 3 D Test 4

37.下列哪些情况可以终止当前线程的运行? A 抛出一个例外时。

B 当该线程调用sleep()方法时。 C 当创建一个新线程时。

D 当一个优先级高的线程进入就绪状态时。

38.下列那些项目不是java的主要优点。 A. 直接操作内存,功能强大 B.一次编写,到处运行 C.纯面向对象的语言

D. 可以通过拖拽的方式快速开发程序界面

39.下面有几个定义的标识符,找出合乎java规范的标识符。 A. IDoLoveThisGame B. const C. 3Person

D. _People5

40. public class MyClass{

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

_____class MyInnerClass {}

}

在以上Java代码中的横线上,可放置哪些修饰符。

A. public B. private

C. static D. friend

三、 程序解析题(10道)

1.编译如下代码可能出现的结果是 public class Holt extends Thread{ private String sThreadName;

public static void main(String argv[]){

Holt h = new Holt();

h.go(); }

Holt(){}

Holt(String s){

sThreadName = s; }

public String getThreadName(){

return sThreadName; }

public void go(){

Holt first = new Holt(\"first\"); first.start();

Holt second = new Holt(\"second\"); second.start();

}

public void start(){

for(int i = 0; i < 2; i ++){

System.out.println(getThreadName() +i); try{

Thread.sleep(100);

}catch(InterruptedExceptione){ System.out.println(e.getMessage()); } }

}

}

A 编译时出错

B 输出 first0, second0, first0, second1 C 输出 first0, first1, second0, second1

D 运行时出错

2.当你试图编译运行如下代码时会出现什么结果 class Background implements Runnable{ int i=0;

public int run(){

while(true){

i++;

System.out.println(\"i=\"+i);

} //End while return 1;

}//End run }//End class

A 编译通过执行run方法会输出i递增值 B 编译通过当调用开始输出i递增值 C 在编译时代码引起错误.

D 由于没有正确的参数编译时出错.

3.编译下面代码会出现什么结果 public class Conv{

public static void main(String argv[]){ Conv c=new Conv();

String s=new String(\"ello\"); c.amethod(s); }

public void amethod(String s){ char c='H'; c+=s;

System.out.println(c);

} }

A 编译通过并输出字符串\"Hello\" B 编译通过并输出字符串\"ello\" C 编译通过并输出字符串\"elloH\" D 编译出错

4.下面哪一项编译不会有错? A.

package testpackage;

public class Test{//do something…} class MyClass{} B.

import java.io.*; package testpackage;

public class Test{//do something…} C.

import java.io.*;

class Person{//do something…} public class Test{//do something…} D.

import java.io.*;

import java.awt.*;

public class Test{//do something…} 5.给出下面代码: 1) class Parent {

2) private String name; 3) public Parent(){} 4) }

5) public class Child extends Parent { 6) private String department; 7) public Child() {}

8) public String getValue(){ return name; } 9) public static void main(String arg[]) { 10) Parent p = new Parent(); 11) } 12) }

那些行将引起错误? A 第3行

B 第6行 C 第7行

D 第8行

6.编译下列代码的结果是?

public class SiteInfo{ String webSite=\"http://www.lanw.com\"+'/'+\"default.htm\"; public String getSite(){

return webSite; } }

A. 没有任何问题

B. 编译器会报错误:':' expected for the statement in line 2. C.缺少main方法. D.

getSite()方法中的return webSite有问题

7.给出下面代码段 1) public class Test { 2) int m, n;

3) public Test() {}

4) public Test(int a) { m=a; }

5) public static void main(String arg[]) { 6) Test t1,t2; 7) int j,k; 8) j=0; k=0; 9) t1=new Test(); 10) t2=new Test(j,k); 11) } 12) }

哪行将引起一个编译时错误?

A line 3 B line 5 C line 6

D line 10

8.对于下列代码: 1) class Person {

2) public void printValue(int i, int j) {//... } 3) public void printValue(int i){//... } 4) }

5) public class Teacher extends Person { 6) public void printValue() {//... } 7) public void printValue(int i) {//...} 8) public static void main(String args[]){ 9) Person t = new Teacher(); 10) t.printValue(10);

11) }

第10行语句将调用哪行语句? A line 2 B line 3 C line 6 D line 7

9.给定下面的Java程序段:

1. 1) StringBuffer sb = new StringBuffer(\"abc\"); 2. 2) String s = new String(\"abc\"); 3. 3) sb.append(\"def\"); 4. 4) s.append(\"def\"); 5. 5) sb.insert(1, \"zzz\"); 6. 6) s.concat(sb); 7. 7) s.trim();

下面那些描述是正确的 :

A. 编译器在 line 1产生错误.

B. 编译器在 line 2产生错误. C. 编译器在 line 3产生错误. D. 编译器在 line 4产生错误. E. 编译器在 line 5产生错误. F.编译器在 line 6产生错误. G. 编译器在 line 7产生错误.

10.给定如下的代码:

1) public class Test { 2) int m, n; 3) public Test() {} 4) public Test(int a) { m=a; } 5) public static void

main(String arg[]) { 6) Test t1,t2; 7) int j,k; 8) j=0; k=0; 9) t1=new Test();

10) t2=new Test(j,k); 11) } 12) }下列哪一行会在编译时产生错误? A. line 3 B. line 5

C. line 6 D. line 10

四、 程序填空题(10道)

1.完成程序中循环,实现把变量i赋值给数组ia[]的元素 public class Lin{

public static void main(String argv[]){ Lin l = new Lin(); l.amethod(); }

public void amethod(){

int ia[] = new int[4];

_____________________//Start For loop {

ia[i]=i;

System.out.println(ia[i]); }

} }

答案:for(int i=0; i< ia.length;i++)

2.下面程序运行的结果是_______________________ public class Oct{

public static void main(String argv[]){ Oct o = new Oct(); o.amethod(); }

public void amethod(){ int oi= 012;

System.out.println(oi); } }

答案:10

3.下列程序段运行结果是_________ public class Example{

public static void main(String args[]){ Integer b= new Integer(10); Add(b);

System.out.println(b.intValue());

}

static void Add(Integer b){ int I= b.intValue(); I+=3;

b= new Integer(I); } }

答案:10

4.写出下列代码执行的结果: public class Test1 {

public static void main(String[] args) { for (int i=-2; i<2; i++) { if (i==0) continue;

System.out.println(\"i=\" + i); } } }

答案:

i=-2

i=-1 i=1

5.给定如下代码,程序输出的结果是: public class Test {

void printValue(int m){

do { System.out.println(\"The_value_is_\"+m); }

while( --m > 10 ) }

public static void main(String arg[]) {

int i=10;

Test t= new Test();

t.printValue(i); } }

答案:

The_value_is_10

6.写出下列程序的结果 public class Test{

public static void main(String args[]){ String s1=”Henry Lee”; String s2=”Java Applet”; String s3=”Java”; String st;

if(s.compareTo(s2)<0) st=s2; else st=s1;

if(st.compareTo(s3)<0) st=s3;

System.out.println(“big=”+st); } }

答案:

Java Applet

7.写出下面程序运行的结果: import java.util.Arrays; public class SortArray {

public static void main(String args[]) { String[] str = {\"size\ Arrays.sort(str);

for (int i=0; i答案:

abs_class_length_size

8.定义A,B如下: class A { int a=1;

double d=2.0; void show( ) {

System.out.println(\"Class A:a=\"+a +\" d=\"+d); }

}

class B extends A {

float a=3.0f;

String d=\"Java program.\"; void show( ) {

super.show( );

System.out.println(\"Class B:a=\"+a +\" d=\"+d); } }

(1) 若在应用程序的main方法中有以下语句: A a=new A(); a.show();

则输出的结果如何?

(2) 若在应用程序的main方法中定义类B的对象b: A b=new B(); b.show();

则输出的结果如何? 答案:

(1)Class A:a=1 d=2.0 (2)Class A:a=1 d=2.0

Class B:a=3.0 d=Java program.

9.以下程序段的输出结果为 。 int j=2; switch ( j ) { case 2:

System.out.print(“Value is two.”); case 2+1 :

System.out.println(“Value is three.”); break; default:

System.out.println(“value is “+j);

break; }

答案:

Value is two.Value is three

10.阅读以下程序段: class Parent {

void printMe()

{

System.out.println(“parent”); } }

class Child extends Parent {

void printMe()

{

System.out.println(“child”); }

void printAll() {

super.printMe(); this.printMe(); printMe(); }

}

public class Test_this

{

public static void main(String args[ ]) {

Child myC=new Child(); myC.printAll(); } }

输出结果为:

答案:

parent child child

五、 编程题:(2道)

1.编写一个程序计算下列问题:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,

求它在第10次落地时,共经过多少米?第10次反弹多高? 答案:the total of road is 299.609375 m the tenth is 0.09765625 m

参考程序如下:

public class Example {

public static void main(String[] args) {

double sn=100.0,hn=sn/2; int n;

for(n=2;n<=10;n++){

sn=sn+2*hn;/*第n次落地时共经过的米数*/ hn=hn/2; /*第n次反跳高度*/

}

System.out.println(\"the total of road is \"+sn+\" m\"); System.out.println(\"the tenth is \"+hn+\" m\"); } }

2. 编写程序计算结果:一个 1000以内的整数,它加上100后是一个完全平方数,再加上168又是一个完

全平方数,请问该数是否存在,如果存在又是多少? 答案:21,261 参考程序如下:

public class Example

{

public static void main(String[] args){

long i,x,y,z;

for (i=1;i<1000;i++){

x=(long)Math.sqrt(i+100);/*x为加上100后开方后的结果*/

y=(long)Math.sqrt(i+268);/*y为再加上168后开方后的结果*/

if(x*x==i+100&&y*y==i+268)/*如果一个数的平方根的平方等于该数,这说明此数是完全平方数*/

System.out.println(i); } } }

因篇幅问题不能全部显示,请点此查看更多更全内容

Top