java,string类练习题
精品文档
java,string类练习题
一、选择题
1. 下列String类的方法返回指定字符串的一部分
A. extractstring B. substring C. Substring D.
Middlestring
2(下列哪一个是正确的
A) HashTable的关键字和值都允许为null
B) HashTable的关键字和值都不允许为null
C) HashTable的关键字允许为null,值不允许为null D) HashTable的关键字不允许为null,值允许为null(对
于下列代码:
String str1=“java”; String str2=“java”;
String str3=new String;
StringBuffer str4=new StringBuffer; 以下
表
关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf
达式
的值为true的是 A) str1= =str2; C) str2= =str3;
B) str1= =str4; D) str3= =str4;
4(以下程序段输出结果的是
public class Test {
public static void main{String str=“ABCDE”;str.substring;str.concat;
System.out.print; }
}
1 / 20
精品文档
A) DE B) DEXYZ C) ABCDED) CDEXYZ(对于下
列代码:
public class Example{
String str=new String; char ch[]={‘d’,’b’,’c’};
public static void main{ }
public void change{ str=“world”;ch[0]= ‘a’;
}
Example ex=new Example; ex.change;
System.out.println;
}
B) hello and a
输出结果是: A) hello and d
C) world and d D) world and a(以下代码的运行
结果是哪一项。
public class StringTest { } A)A.A C)A.B
B)B.A D)B.B
public static void mb_operate {}
public static void main { }
String a=“A”; String b=“B”; mb_operate;
System.out.println; x.concat; y=x;
7(以下代码的运行结果是哪一项。
2 / 20
精品文档
public class StringArray Test {}
public static void mb_swap {}
public static void main { }
B)21 D)23
String [] s={“1”,”2”}; mb_swap;
System.out.print; if return; String t=s[0];
s[0]=s[1]; s[1]=t;
A)20 C)22
8(以下代码的运行结果是哪一项。
publicclassTestGetChars{
publicstaticvoidmain{
String myString = new String; char [] yourString
= new char[16];
myString.getChars;
System.out.println;
System.out.println;
} }
A)Hello World! B)Hello World!
Hello Hello! C)Hello World! D)World
World
World
9(以下代码的运行结果是哪一项。
3 / 20
精品文档
publicclassTestStringContructor{
publicstaticvoidmain{byte [] b = {49,50,51,52,53};} A)45555
B)12345
C)901D)49505152510(以下代码的运行结果是哪一
项。
public class J_Intern {
public static void main {
String s1 = “123456”; String s= “123456”; String s= “123” + “456”; String a0 = “123”;
String s= a0 + “456”;
String s= new String;
String s= s5.intern;
System.out.println ? “==“ : “!=“) +”s1”);System.out.println ? “==“ : “!=“) +”s1”);System.out.println ? “==“ : “!=“) +”s1”);System.out.println ? “==“ : “!=“) +”s1”);System.out.println ? “==“ : “!=“) +”s1”); }
String myString = new String; System.out.println;
}
4 / 20
精品文档
}
A)s2==s1
s3==s1s4!=s1s5!=s1 s6==s1C)s2==s1
s3==s1s4!=s1s5!=s1s6!=s1
B)s2==s1
s3==s1 s4==s1 s5!=s1 s6==s1 s3==s1 s4!=s1 s5!=s1
s6==s1
D)s2!=s1
11(下面程序段的输出结果是
StringBuffer buf1=new StringBuffer;
System.out.println+ ‘,’+buf1,capacity); A)
0,20
B) 0,null
C) 0,20
D) 0,0
12(下面的语句序列的输出结果为
String s= “ABCD”; s.concat;
s.replace; A)ABCDEF B)ABFDE
C)ABCDE
D)ABCD
13(下面的哪些程序片断可能导致错误?
A) String s = “Gone with the wind”;
5 / 20
精品文档
String t = “ good “; String k = s + t;
B) String s = “Gone with the wind”;String t;
D) String s = “home directory”; String t = s
- “directory”;
t = s[3] + “one”;
C) String s = “Gone with the wind”; String
standard = s.toUpperCase; 14(给出下面的代码片断
1) public void create {
2} Vector myVect;
3} myVect = new Vector; } }
下面的哪些陈述为true?
A) 第二行的声明不会为变量myVect分配内存空间。 B) 第二行的声明分配一个到Vector对象的引用的内存空
间。 C) 第二行语句创建一个Vector类对象。 D) 第三行语句创建一个Vector类对象。
E) 第三行语句为一个Vector类对象分配内存空间。 15(欲构造ArrayList类的一个实例,此类继承了List接口,下列哪个方法是正确的
A ) ArrayList myList=new Object; B) List myList=new ArrayList;
C) ArrayList myList=new List; D) List
myList=new List; 16(执行下列代码后,哪个结论是正确
6 / 20
精品文档
的 String[] s=new String[10];
A) s[10] 为 ““;
B) s[9] 为 null;
C) s[0] 为 未定义
D) s.length 为10
17(下面的表达式哪个是正确的?
A) String s=“你好”;int i=3; s+=i;
B) String s=“你好”;int i=3; if{ 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.length:0; 18(给定下列程序:
public class Test
{
public static void main{
String str=new String; change;
System.out.println; char ch[]={‘C’,’e’,’l’,’l’,’o’};
}
public static void
change{ str=“Changed”;ch[0]=‘H’; } };
B) World and Hello
7 / 20
精品文档
D) Changed and Cello B) “word”.equals
上述程序的运行结果应该是, A) World and Cello C) Changed and Hello A) “hello”==“hello”
19(在java语言中,下列表达式返回true的是哪些
项目。
C) “john”=“john” D) “john”.equals)0(下面的哪些程序片断可能导致错误,
A) String s=“Hello”; String t=“World”; String k=s+t;
B) String s=“Hello”; String t;
t=s[3]+”one”;
C) String s=“Hello”; D) String s=“Hello World”;
String standard = s.toUpperCase; String t=s-”World”;1(在java语言中,下列语句中正确的是哪
个,
A) String temp[]=new String{“a” “b” “c”}; B) String temp[]={“a” “b” “c”}; C) String temp={“a” “b” “c”}; D) String
temp[]={“a”,”b”,”c”};
22(下面的哪些叙述为真?
A) equals方法判定引用值是否指向同一对象。
8 / 20
精品文档
B) == 操作符判定两个分立的对象的内容和类型是否一致。 C) equals方法只有在两个对象的内容一致时返回true。
D) 类File重写方法equals在两个分立的对象的内容和类型一致时返回true。 3(关于以下程序段,正确的说法是
1( String s1=“Hello”;
Java常用类习题
一、选择题
1. 下列String类的方法返回指定字符串的一部分
A. extractstring B. substring C. Substring D.
Middlestring
2(下列哪一个是正确的
A) HashTable的关键字和值都允许为null B) HashTable的关键字和值都不允许为null
C) HashTable的关键字允许为null,值不允许为null D) HashTable的关键字不允许为null,值允许为null(对于下列代码:
String str1=“java”; String str2=“java”;
String str3=new String;
StringBuffer str4=new StringBuffer; 以下表达式的值为true的是 A) str1= =str2; C) str2= =str3;
9 / 20
精品文档
public class Test {
public static void main{String str=“ABCDE”;str.substring;str.concat;System.out.p
rint; } }
A) DE B) DEXYZ C) ABCDED) CDEXYZ(对于下
列代码:
B) str1= =str4; D) str3= =str4;
4(以下程序段输出结果的是
public class Example{
String str=new String; char ch[]={‘d’,’b’,’c’};
public static void main{ }
public void change{
str=“world”;ch[0]= ‘a’; }
Example ex=new Example; ex.change;
System.out.println;
}
B) hello and a
输出结果是: A) hello and d C) world and d
D) world and a
6(以下代码的运行结果是哪一项。
public class StringTest { } A)A.A C)A.B
10 / 20
精品文档
B)B.A D)B.B
public static void mb_operate {}
public static void main { }
String a=“A”; String b=“B”; mb_operate;
System.out.println; x.concat; y=x;
7(以下代码的运行结果是哪一项。
public class StringArray Test {} A)20 C)22
B)21 D)23
public static void mb_swap {}
public static void main { }
String [] s={“1”,”2”}; mb_swap;
System.out.print; if return; String t=s[0];
s[0]=s[1]; s[1]=t;
8(以下代码的运行结果是哪一项。
publicclassTestGetChars{
publicstaticvoidmain{}
A)Hello World! C)Hello World! World
B)Hello World! D)World World
Hello Hello!
String myString = new String; char [] yourString
= new char[16];
myString.getChars;
11 / 20
精品文档
System.out.println; System.out.println;
}
9(以下代码的运行结果是哪一项。
publicclassTestStringContructor{
publicstaticvoidmain{ } A)4555C)90123
B)1234D)4950515253
byte [] b = {49,50,51,52,53}; String myString
= new String; System.out.println;
}
10(以下代码的运行结果是哪一项。
public class J_Intern {
public static void main {
String s1 = “123456”; String s= “123456”; String s= “123” + “456”; String a0 = “123”;
String s= a0 + “456”; 会新建一个字符串String
s= new String; new
String s= s5.intern; 返回字符序列
System.out.println ? “==“ : “!=“) +”s1”);System.out.println ? “==“ : “!=“) +”s1”);System.out.println ? “==“ : “!=“) +”s1”);System.out.println ? “==“ : “!=“) +”s1”);System.out.println ? “==“ : “!=“)
12 / 20
精品文档
+”s1”); }
}
A)s2==s1s3==s1s4!=s1s5!=s1 s6==s1C)
s2==s1s3==s1s4!=s1s5!=s1s6!=s1
B)s2==s1
s3==s1 s4==s1 s5!=s1 s6==s1 s3==s1 s4!=s1 s5!=s1
s6==s1
D)s2!=s1
11(下面程序段的输出结果是
StringBuffer buf1=new StringBuffer; pacacity总
容量 System.out.println+ ‘,’+buf1,pacacity); A) 0,20
B) 0,null
C) 0,20
D) 0,0
12(下面的语句序列的输出结果为
String s= “ABCD”; s.concat; s.replace; A)
ABCDEF B)ABFDE
C)ABCDE
D)ABCD
13(下面的哪些程序片断可能导致错误?
A) String s = “Gone with the wind”; String t
13 / 20
精品文档
= “ good “; String k = s + t;
B) String s = “Gone with the wind”; String t;
D) String s = “home directory”; String t = s
- “directory”;
t = s[3] + “one”;
C) String s = “Gone with the wind”; String
standard = s.toUpperCase; 14(给出下面的代码片断
1) public void create { } Vector myVect;
3} myVect = new Vector; } }
下面的哪些陈述为true?
A) 第二行的声明不会为变量myVect分配内存空间。
B) 第二行的声明分配一个到Vector对象的引用的内存空
间。 C) 第二行语句创建一个Vector类对象。 D) 第三行
语句创建一个Vector类对象。
E) 第三行语句为一个Vector类对象分配内存空间。
15(欲构造ArrayList类的一个实例,此类继承了List
接口,下列哪个方法是正确的
A ) ArrayList myList=new Object; B) List myList=new ArrayList; C) ArrayList myList=new List; 口不能被实例化
16(执行下列代码后,哪个结论是正确的 String[] s=new String[10];
14 / 20
精品文档
D) List myList=new List;借
A) s[10] 为 ““;
B) s[9] 为 null;
C) s[0] 为 未定义 D) s.length 为10
17(下面的表达式哪个是正确的?
A) String s=“你好”;int i=3; s+=i; 字符串可以+= B) String s=“你好”;int i=3; if{ 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=&&>0)?s.length:0;S.length 空指针异常 18(给定下列
程序:
public class Test {
public static void main{
String str=new String; change;
System.out.println; char ch[]={‘C’,’e’,’l’,’l’,’o’};
}
public static void
change{ str=“Changed”;ch[0]=‘H’; } };
B) World and Hello
D) Changed and Cello
15 / 20
精品文档
B) “word”.equals
上述程序的运行结果应该是, A) World and Cello C) Changed and Hello
19(在java语言中,下列表达式返回true的是哪些
项目。
A) “hello”==“hello” ==内存地址都要一样 C)
“john”=“john”
20(下面的哪些程序片断可能导致错误,
A) String s=“Hello”; String t=“World”; String k=s+t;
C) String s=“Hello”;
B) String s=“Hello”;
String t; t=s[3]+”one”;
String t=s-”World”;
D) “john”.equals)
D) String s=“Hello World”;
String standard = s.toUpperCase; A) String
temp[]=new String{“a” “b” “c”}; B) String temp[]={“a” ,”b” ,”c”}; C) String temp={“a” “b” “c”}; D) String
temp[]={“a”,”b”,”c”};2(下面的哪些叙述为真?
21(在java语言中,下列语句中正确的是哪个,
16 / 20
精品文档
A) equals方法判定引用值是否指向同一对象。
B) == 操作符判定两个分立的对象的内容和类型是否一致。 C) equals方法只有在两个对象的内容一致时返回true。
问答:
1( 写出String类的特点,构造函数及其他
成员函数。并对各函数的功能进行详细说明。
答:
String类的特点:为不可变对象,一旦被创建,就不能修改它的值.对于已经存在的String对象的修改都是重新创建一个新的对象,然后把新的值保存进去.
String类的构造函数和其他函数
2( 写出StringBuffer类的特点,构造函数
及其他成员函数。并对各函数的功能进行详细说明。
答:
StringBuffer类的特点:
是一个可变对象,当对他进行修改的时候不会像String那样重新建立对象 它只能通过构造函数来建立。
StringBuffer的构造函数
StringBuffer类除了具有String类的函数外,新增的函数:
3( 写出java中数组的概念和定义,并说明
17 / 20
精品文档
java中数组类型与C++中的区别。 答:数组的概念:
数组是一种复合数据类型,也是有序数据的集合,数组中的
每个元素具有相同的数据类型,可以用统一的数组名和相应
的下标来唯一地确定数组中的元素。
数组的定义:一维数组的定义:type ArrayName[];
二维数组:type ArrayName[][].
与C++中的区别:在Java中定义数组时并不为数组元
素分配内存空间,定义之后经过初始化之后才能引用数组中
的元素。Java中还提供另外一种定义方式:type[]ArrayName.
4( 写出一维数组的定义
格式
pdf格式笔记格式下载页码格式下载公文格式下载简报格式下载
和实例化方法。 答:
定义格式:type arrayName[]或是type []arrayName.type可以是Java中任意的数据类型,包括复合数据类型。
实例化方法:1、静态初始化:type arrayName[]={elements1[,elements2.....]}
2、动态初始化:简单数据类型arrayName=new type[arraySize]; 例如: int intArray[];
intArray=new int[5]
复合数据类型 首先为数组开辟每个元素的引用空
间:arrayName=new type[arraySize]然后再为每个元素开
辟内在空间:arrayName[0]=new type...arrayName[arraySize-1]=new type.
5( 写出二维数组的定义格式和实例化方法。 答:
18 / 20
精品文档
定义格式:type arrayName[][]或是type[][]arrayname
实例化方法:1、静态初始化 例:int intArray[][]={{1,2},{2,3}}
2、动态初始化 直接为每一维分配内存空间 例:int a[][]=new int[2][3];
从最高维开始,分别为每一维分配内存空间,格式如下:arrayName=new type[arrayLenth1][];
arrayName[0]=new type[arrayLenth20]....
arrayName[arraylenth-1]=new type[arrayenth2]
实验:
第一题:
用数组实现一个char队列类,该类的构造函数传递一个int类型的参数,确定队列的大小。成员方法包括put和get,队列为満时再put要提示“队列满,无法put”,队列为空时再get要提示“队列空,无法get”。
另写一个演示类测试该队列类,分别构造大小两个队列,大的容量为100个char, 小的容量为4个char。用put操作将26个字母加入大队列,再用get操作取出并输出到屏幕; 将ZYXWV五个字母加入到小队列,再从队列中取出并输出。 class CharList{
int arraySize,i=0;//arraySize表示数组的大小,i表示数组下标 char array[];//定义一个一维数组当作队列
19 / 20
精品文档
CharList//构造函数动态为数组初始化 { arraySize=sz; array=new char[arraySize]; } boolean put//把字符存入
数组中 {if { } System.out.println; return false; } array[i]=element; i++; return true;
20 / 20