JAVA复习题三卷合一版(陈玲修改)1(5)

2019-08-26 17:32

3. 下列程序中构造了一个SET并且调用其方法add(),输出结果是_________ import java.util.*; public class Example{

public int hashCode(){

return 1; }

public boolean equals(Object b){

return true; }

public static void main(String args[]){

Set set=new HashSet(); set.add(new Example()); set.add(new Example()); set.add(new Example()); System.out.println(set.size()); } } 答案:1

4. 下列程序的运行结果是_____________ class Example{

class Dog{

private String name; private int age;

public int step; Dog(String s,int a){

name=s; age=a; step=0; }

public void run(Dog fast){

fast.step++; } }

public static void main (String args[]){

Example a=new Example(); Dog d=a.new Dog(\d.step=25; d.run(d);

21

System.out.println(d.step); } }

答案:26

5. 下面语句段的输出结果是什么? _____________ int i = 9; switch (i) {

default: System.out.println(\case 0: System.out.println(\case 1: System.out.println(\case 2: System.out.println(\}

答案:default, zero

6. 给出程序片段,空格处应该添入什么句子设置框架背景颜色为pink import java.awt.*;

public class SetF extends Frame{ public static void main(String argv[]){ SetF s=new SetF(); s.setSize(300,200); s.setVisible(true); ______________ }

答案:s.setBackground(Color.pink);

7. 执行下面程序,将输出什么结果______________ public class Pass{

static int j=20;

public static void main(String argv[]){

int i=10;

Pass p = new Pass(); p.amethod(i);

System.out.println(\System.out.println(\}

public void amethod(int x){

x=x*2; j=j*2; } }

22

答案:i=10

j=40

8. 完成程序中循环,实现把变量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++)

9. 下面程序运行的结果是_______________________ 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

10. 下列程序段运行结果是_________

public class Example{

public static void main(String args[]){

Integer b= new Integer(10); Add(b);

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

23

static void Add(Integer b){

int I= b.intValue(); I+=3;

b= new Integer(I); } }

答案:10 五、 编程题(2道)

1. 试编写程序解下面的问题。猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,

又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。 答案:总共1534个 参考程序如下: public class MyClass {

public static void main(String[] args) { int day,x1,x2; day=9; x1=0; x2=1; while(day>0) { }

x1=(x2+1)*2;/*第一天的桃子数是第2天桃子数加1后的2倍*/ x2=x1; day=day-1; }

System.out.println(\} }

2. 水仙花数是一个三位数,三位数各位的立方之和等于三位数本身。请编写程序,求出所有的水仙

花数。

水仙花数为:153,370,371,407 参考程序如下: public class Test {

public static void main(String[] args) { int x,y,z;

for(int i=100;i<1000;i++)

24

{ x=i; y=i/100; z=(i-y*100)/10;

if(i==(x*x*x+y*y*y+z*z*z)) System.out.println(i); } } }

25


JAVA复习题三卷合一版(陈玲修改)1(5).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2019年全国各地高考模拟作文题汇总分析

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: