下面这段代码会产生( )个String对象。
Strings1="hello";
Strings2=s1.substring(2,3);
Strings3=s1.toString();
Strings4=newStringBuffer(s1).toString();
A、1
B、2
C、3
D、4
第1题:
下列代码的执行结果是()。publicclasstest5{publicstaticvoidmain(Stringargs[]){Strings1=newString("hello");Strings2=newString("hello");System.out.prim(s1==s2);System.out.print(",");System.out.println(s1.equals(s2));}
A.true,false
B.true,true
C.false,true
D.false,false
第2题:
下列哪个程序段可能导致错误?
A.String s="hello"; String t= "good"; String k=s+ t;
B.String s="hello"; String t; t=s[3]+"one";
C.String s="hello"; String standard=s. toUpperCase
D.String s="hello"; String t =s+ "good"
第3题:
下列语句输出结果为( )。 public class test { public static void main (String args[]) { Strings1=newString("HOW"); Strings2=newString("How"); System.out.println(!(s1.equals(s2))): } }
A.假
B.真
C.0
D.1
第4题:
阅读下面程序 publicclass Operators AndExpressions{ voidequalsMethodl(){ Strings1=newString("howareyou"); Strings2=newString("howareyou"); System.out.println(s1==s2); } publicstaticvoidmain(Stringargs[]){ OperatorsAndExpressionsOperAndExp=newOperatorsAndExpressions (); //用于复合类型数据的“==”运算符 OperAndExp.equalsMethodl(); } } 程序运行结果是( )。
A.==
B.true
C.false
D.equal
第5题:
下列代码的执行结果是______。 public class ex55 { public static void main(String args[] ) { String s1=new String("hello"); String s2=new String("hello"); System.out.print (s1==s2); System.out.print (","); System.out.println (s1.equals (s2)); } }
A.true, false
B.true, true
C.false, true
D.false, false
第6题:
下列程序的执行结果是 public class Testhh { public static void main(String args [] ) { Strings1=new String("I am boy"); Strings2=new String("I am boy"); System.out.println(s1.equals(s2)); } }
A.真
B.假
C.I am boy
D.都不正确
第7题:
下列的( )程序段可能导致错误。
A.String s="hello": Sting t="good"; String k=s+t;
B.Sting s="hello"; String t; t=s [3] + "one";
C.Sting s="hello"; String standard=s.toUpperCase( );
D.String s="hello": Stringt s +"good";
第8题:
执行以下代码会输出什么结果?() public class Test { StringgetStr(String s){ return s + “hello”; } public static void main(String arg[]) { Test t= new Test(); System.out.println(t.getStr(“LiLei/n”)); } }
第9题:
设String对象s="H",运行语句System.out.println(s.concat("ello!"));后String对象s的内容为"Hello!",所以语句输出为"Hello!"。
第10题:
public class X { public static void main (String[]args) { string s = new string (“Hello”); modify(s); System.out.printIn(s); } public static void modify (String s) { s += “world!”; } } What is the result?()
第11题:
ArithmeticException
NullPointerException
IOException
ClassNotFoundException
第12题:
The program runs and prints “Hello”
An error causes compilation to fail.
The program runs and prints “Hello world!”
The program runs but aborts with an exception.
第13题:
下列的哪个程序段可能导致错误? ( )
A.String s="hello"; String t="good"; String k=s+t;
B.String s="hello"; String t; t=s[3]+"one";
C.String s="hello"; String standard=s.toUpperCase();
D.String s="hello"; String t=s+"good";
第14题:
给出下列的代码,则以下哪个选项返回true? String s="hello"; String t="hello"; char c []= {'h','e','1','1','o'};A)s.equals(t);
A.t. equals(
B.;
C.s==t;
D.t==c;
第15题:
String s = "Hello";s = s + " world!";这两行代码执行后,
原始的String 对象中的内容到底变了没有?
没有。因为String 被设计成不可变(immutable)类,所以它的所有对象都是不可变对象。
在这段代码中,s 原先指向一个String 对象,内容是 "Hello",然后我们对s 进行了+
操作,那么s 所指向的那个对象是否发生了改变呢?答案是没有。这时,s 不指向原来那个对象了,而指向了另一个 String 对象,内容为"Hello world!",原来那个对象还
存在于内存之中,只是s 这个引用变量不再指向它了。
通过上面的说明,我们很容易导出另一个结论,如果经常对字符串进行各种各样的修
改,或者说,不可预见的修改,那么使用String 来代表字符串的话会引起很大的内存
开销。因为 String 对象建立之后不能再改变,所以对于每一个不同的字符串,都需要
一个String 对象来表示。这时,应该考虑使用StringBuffer 类,它允许修改,而不是每
个不同的字符串都要生成一个新的对象。并且,这两种类的对象转换十分容易。
同时,我们还可以知道,如果要使用内容相同的字符串,不必每次都new 一个String。
例如我们要在构造器中对一个名叫s 的String 引用变量进行初始化,把它设置为初始
值,应当这样做:
public class Demo {
private String s;
...
public Demo {
s = "Initial Value";
}
...
}
而非
s = new String("Initial Value");
后者每次都会调用构造器,生成新对象,性能低下且内存开销大,并且没有意义,因
为String 对象不可改变,所以对于内容相同的字符串,只要一个String 对象来表示就
可以了。也就说,多次调用上面的构造器创建多个对象,他们的String 类型属性s 都
指向同一个对象。
上面的结论还基于这样一个事实:对于字符串常量,如果内容相同,Java 认为它们代
表同一个String 对象。而用关键字new 调用构造器,总是会创建一个新的对象,无论
内容是否相同。
至于为什么要把String 类设计成不可变类,是它的用途决定的。其实不只String,很
多Java 标准类库中的类都是不可变的。在开发一个系统的时候,我们有时候也需要设
计不可变类,来传递一组相关的值,这也是面向对象思想的体现。不可变类有一些优
点,比如因为它的对象是只读的,所以多线程并发访问也不会有任何问题。当然也有
一些缺点,比如每个不同的状态都要一个对象来代表,可能会造成性能上的问题。所
以Java 标准类库还提供了一个可变版本,即 StringBuffer。
第16题:
下列代码段的执行结果是( )。 public class Test { public static void main(String args[ ]) { String s1= new String("hello"); String s2= new String("hello"); System.out.println(s1==s2); System.out.println(s1.equal(s2)); } }
A.true false
B.true true
C.false true
D.false false
第17题:
下面的程序执行后,屏幕上显示的应是( )。 publicclassExam{ publicstaticvoidmain(String[]args){ charcharl[]={'t','e','s','t'}; charchar2[]={'t','e,'s','t','1'}; Strings1=newString(char1); Strings2=newString(char2,0,4); System.out.println(s1.equals(S2)); } }
A.true
B.false
C.test
D.编译错误
第18题:
给出下列的代码则以下哪个选项返回true? String s = "hello" ; String s = "hello" ; char c[] = { 'h' ,'e','l','o'};
A.s.equals(t);
B.t.equals(c);
C.s = =t
D.t = = c;
第19题:
下面的代码实现一个简单的Applet: import java.applet.Applet; import java.awt.*; public class Sample extends Applet { private String text="Hello World"; public void init() { add(new Label(text)); } public Sample(String string) { text=string; } } 通过下面的HTML文件访问: <html> <title>Sample Applet</title> <body> <applet code="Sample.class"width=200 height=200></applet> </body> </html> 当编译和运行该小程序时会出现什么结果,请选择正确的答案。( )
A.将会出现“Hello World"
B.将会产生一个运行时错误
C.什么都没有
D.产生一个编译时错误
第20题:
下面哪些代码中声明的变量可以存放10个String对象()
第21题:
String s= "hello"; String t = "hello"; char c[] = {’h’,’e’,’l’,’l’,’o’} ; Which return true?()
第22题:
对
错
第23题:
编译报错
LiLei hello
LiLeihello
无任何输出