Java常用类习题(附部分答案) 联系客服

发布时间 : 星期六 文章Java常用类习题(附部分答案)更新完毕开始阅读

17.下面的表达式哪个是正确的? ( )

A) String s=\你好\

B) String s=\你好\C) String s=\你好\D) String s=\你好\

E) String s=null; int i=(s!=null)&&(s.length>0)?s.length():0; 18.给定下列程序:

public class Test {

public static void main(String args[]){

String str=new String(\ change(str,ch);

System.out.println(str+\ char ch[]={'C','e','l','l','o'};

}

public static void change(String str,char ch[]){ str=\ } };

B) World and Hello

D) Changed and Cello B) \

D) \

B) String s=\

String t; t=s[3]+\

String t=s-\

上述程序的运行结果应该是?( ) A) World and Cello C) Changed and Hello A) \ C) \

19.在java语言中,下列表达式返回true的是哪些项目。( )

20.下面的哪些程序片断可能导致错误?( )

A) String s=\ String t=\ String k=s+t;

C) String s=\

D) String s=\

String standard = s.toUpperCase(); A) String temp[]=new String{\B) String temp[]={\C) String temp={\D) String temp[]={\22.下面的哪些叙述为真? ( )

21.在java语言中,下列语句中正确的是哪个?( )

A) equals()方法判定引用值是否指向同一对象。

B) == 操作符判定两个分立的对象的内容和类型是否一致。 C) equals()方法只有在两个对象的内容一致时返回true。

D) 类File重写方法equals()在两个分立的对象的内容和类型一致时返回true。 23.关于以下程序段,正确的说法是

1. String s1=\

5

2. String s2=\3. if(s1= =s2)

4. System.out.println(\5. if (s1.equals(s2))

6. System.out.println(\A. 行4与行6都将执行 B. 行4执行,行6不执行 C. 行6执行,行4不执行 D. 行4、行6都不执行

24.要产生[20,999]之间的随机整数使用哪个表达式? A.(int)(20+Math.random()*97) B. 20+(int)(Math.random()*980) C. (int)Math.random()*999 D. 20+(int)Math.random()*980 25.下列程序运行的结果为: public class Example{

String str=new String(\ char[] ch={'a','b','c'};

public static void main(String args[]){ Example ex=new Example(); ex.change(ex.str,ex.ch);

System.out.print(ex.str+\ Sytem.out.print(ex.ch); }

public void change(String str,char ch[]){ str=\ ch[0]='g'; } }

A. good and abc B. good and gbc C. test ok and abc D. test ok and gbc 26.设有如下程序

public class test {

public static void main(String args[]) {

Integer intObj=Integer.valueOf(args[args.length-1]); int i = intObj.intValue(); if(args.length > 1) System.out.println(i); if(args.length > 0)

System.out.println(i - 1); else

6

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

运行程序,输入如下命令: java test 2 则输出为:

A. test B. test -1 C. 0 D. 1 E. 2 27.下列程序运行的结果为:

public class test {

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

float f = 2.3f; double d = 2.7;

i = ((int)Math.ceil(f)) * ((int)Math.round(d)); System.out.println(i); } }

A. 4 B. 5 C. 6 D. 6.1 E. 9 28.如果以下条件成立,则用到java.lang.Math 类中哪个方法? method( -4.4 ) == -4;

A. round() B. min() C. trunc() D. abs() E. floor() F. ceil() 29.set集合如何处理重复元素

A.如果加入一个重复元素将抛出异常

B.如果加入一个重复元素add方法将返回false

C. 集合通过调用equals方法可以返回包含重复值的元素。 D. 重复值将导致编译出错。

30.以下哪个方法是Vector类中增加一个新元素的方法。

A.addElement B. insert C. append D. addItem 31. 以下哪些方法是Collection 接口的方法?

A. iterator B. isEmpty C. toArray D. setText 32.对于以下声明:

String s1=\

String s2=\ String s3;

下面的操作合法的是( )

A)s3=s1+s2; B)s3=s1-s2; C)s3=s1&s2; D)s3=s1&&s2; 33.下面的程序段执行后输出的结果是( )。

StringBuffer buf=new StringBuffer(\ buf.insert(7,\

System.out.println(buf.toString()); A、 Beijing@2008 B、 @Beijing2008

7

C、 Beijing2008@ D、 Beijing#2008 二、程序阅读题 1、阅读下面的程序:

① public class Test{

② public static void main(String[] a){ ③ int i = Integer.parseInt(a[0]); ④ switch (i) {

⑤ case 1:System.out.println(\⑥ case 2:System.out.println(\⑦ case 3:System.out.println(\⑧ case 4:System.out.println(\⑨ } ⑩ } ? }

上面的程序编译是否成功?如果编译出错,指出哪行出错,并说明理由;如果编译正确,用java Test 2 运行得到的输出结果是什么? 2、阅读下面的程序:

1) public class Test{

2) public static void main(String[ ] args) { 3) int x,y=2,i=0,j=0;

4) if(args.length<2) System.exit(-1); 5) x = Integer.parseInt(args[1]); 6) switch(x){

7) case 2:switch(y){

8) case 1:i++;break; 9) case 2:j++;break; 10) default:i++;j++; 11) }

12) case 3:i++;j++;break; 13) default:i++;j++; 14) }

15) System.out.println(\16) System.out.println(\17) } 18) }

上面的程序编译是否成功?如果编译出错,指出哪行出错,并说明理由;如果编译正确,用java Test 1 2 3 运行得到的运行结果是什么? 3、阅读下面的程序TestMonth.java: public class TestMonth{

public static void main(String []args){

try{

int month=Integer.parseInt(args[0]); if(month<0||month>12){

8