下列哪项是String的字面量?()
第1题:
下列哪个程序段可能导致错误?
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"
第2题:
写出程序运行的结果
Public class Base
Public virtual string Hello() {return “Base”;}
Public class Sub:Base
Public override string Hello() {return “Sub”;}
1. Base b = new Base(); b.Hello;
2. Sub s = new Sub(); s.Hello;
3. Base b = new Sub (); b.Hello;
4. Sub s = new Base(); s.Hello;
第3题:
下列代码段的执行结果是( )。 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
第4题:
下列代码的执行结果是______。 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
第5题:
下列的哪个程序段可能导致错误?
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";
第6题:
Which expressions will evaluate to true if preceded by the following code?() String a = "hello"; String b = new String(a); String c = a; char[] d = { ’h’, ’e’, ’l’, ’l’, ’o’ };
第7题:
String s= "hello"; String t = "hello"; char c[] = {’h’,’e’,’l’,’l’,’o’} ; Which return true?()
第8题:
现有: class Pencil { public void write (String content){ System.out.println ("Write"+content); } } class RubberPencil extends Pencil{ public void erase (String content){ System.out.println ("Erase"+content); } } 执行下列代码的结果是哪项?() Pencil pen=new RubberPencil(); pen.write ("Hello"); pen.erase ("Hello");
第9题:
Which SELECT statement will the result ‘ello World’ from the string ‘Hello World’?()
第10题:
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.
第11题:
“Hello”
‘world’
/u2345
new String(“good”)
第12题:
<% String s = “hello world ” ;%>
<% = “hello world ” ;%>
<% = “hello world ” %>
<% ! “hello world ” %>
第13题:
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。
第14题:
下列程序段的输出结果是 String MyStr = "Hello,"; MyStr = MyStr + "World!"; System.out.println(MyStr);
A.Hello,World!
B.Hello,
C.World!
D.该程序段有语法错误
第15题:
下列程序段的输出是( )。 public class Test { public static void main (String args[ ]) { String ss1 = new String("hello"); String ss2 = new String("hello"); System.out.println(ssl == ss2); System.out.println (ssequals(ss2)); } }
A.true, false
B.true, true
C.false, true
D.false, false
第16题:
下列的( )程序段可能导致错误。
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";
第17题:
下列选项中,()是正确的表达式。
第18题:
有语句String s=”hello world”; ,以下操作哪个是不合法的()
第19题:
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?()
第20题:
public class X { public static void main (Stringargs) { string s = new string (“Hello”); modify(s); System.out.printIn(s); } public static void modify (String s) { s += “world!”; } } What is the result?()
第21题:
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.
第22题:
(a == Hello)
(a == b)
(a == c)
a.equals(b)
a.equals(d)
第23题:
s.equals(t);
t.equals(c);
s==t;
t.equals(new String(hello));
t==c;