java习题Ver2.0

发布时间 : 星期六 文章java习题Ver2.0更新完毕开始阅读

System.out.ptintln(\} }

Public static void main(String []args){ Aclass a=new Aclass(); Aclass a1=new Bclass(); a.go(); a.go(); }

}

以上程序运行结果是()

A.Aclass B.Bclass Aclass Bclass C.Aclass D.Bclass Bclass Aclass 15. 分析以下程序 Public class Test{

static void leftshift(int i,int j){

I<<=j; }

Public static void main(String []args){

int i=4,j=2; Leftshift(i,j);

System.out.println(i); } }

改程序的执行结果是() A.2 B.4. C.8. D.16

二、填空题

1、(16-33)、下面的程序的功能是从1~36的整数中随机选取7个数:第1次随机选择1~36中的一个数,第2次从剩下的35个数(第1次选中的数不包括其中)中随机选择一个数,…,重复这个过程,直到选中7个整数。请完成程序填空。

import java.util.*;

public class Select7From36 {

public static void main(String[] args) { Random rd= new Random();

ArrayList allNum=new ArrayList(); //创建有7个元素的Integer数组 Integer result[]=____(1)____; int x;

for(int i=1;i<=36;i++){

allNum.add(new Integer(i)); }

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

result[i]=(Integer)allNum.get(x); allNum.remove(x); }

for(int i=0;i

System.out.print(result[i]+\ \ } } }

三、程序阅读题

1、(16-36)阅读下面的程序代码,并回答问题。 String s1=new String(\

String s2=new String(\ boolean b1=s1.equals(s2);

boolean b2=s1==s2;

System.out.print(b1+\ \

?程序段执行后,在命令行的输出结果如何? ?解释输出(1)的结果的原因?

四、编程题

1、编写两个类,其中一个类声明私有成员变量,并同时声明公共方法访问私有变量;另一个类中使用这些公共方法。

2、编写一个程序,将数组作为参数传给一个方法,该方法将这个数组的元素全部显示出来。

3、现在输入n个数字,以逗号分开,放入一个字符串中,然后按升序和降序排序,并显示排序结果。

练习四

一.程序阅读题

1、(16-37)、阅读下面的程序,并回答问题。

import java.io.*; public class Test {

public static void main(String args[]) throws IOException { BufferedReader buf=new BufferedReader(

new InputStreamReader(System.in)); while(true) {

String str=buf.readLine(); if(str.equals(\ break;

int x=Integer.parseInt(str); System.out.println(x*x); } } }

编译运行上面的程序:

?从键盘输入10,回车后输出的结果如何? 输出200

?从键盘输入exit,回车后程序能正确执行吗?为什么?

NumberFormatException 2、(16-38)、写出下面的程序编译、运行后的结果。

public class Test{

public static void main(String args[]) { new Student(\ new Student(\ new Student(\

System.out.println(\ Student.print(); } }

class Student {

protected String name; protected char sex;

protected int chinese; protected int english; protected Student next; static Student list;

Student(String name, char sex, int chinese,int english) { this.name=name; this.sex=sex;

this.chinese=chinese; this.english=english; this.next=list; list=this; }

static void print() { Student friend=list; if(friend==null)

联系合同范文客服:xxxxx#qq.com(#替换为@)