参考答案和解析
正确答案:A
更多“下列哪项是String的字面量?() A、“Hello”B、‘world’C、/u2345D、new String(“good”)”相关问题
  • 第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"


    正确答案:B
    解析:选项A)String类型可以直接使用“+”运算符进行连接运算。选项B)String是一种Object,而不是简单的字符数组,不能使用下标运算符取其值的某个元素,错误。选项C)toUpperCase()方法是String对象的一个方法,作用是将字符串的内容全部转换为大写并返回转换后的结果(String类型)。选项D)同选项A)。

  • 第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


    正确答案:C
    解析:本题考查比较运算符(==)的使用。比较运算符不仅可以用于基本数据类型的数据之间的比较,还可以用于复合数据类型的数据之间的比较。题中s1和s2的值虽然都是hello,但是由于它们是不同的对象,因此运算后的结果为false。如果需要比较两个对象的值是否相同,则可以调用equals()方法。所以程序最后输出false和true。

  • 第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


    正确答案:C

  • 第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";


    正确答案:B
    解析:选项A)String类型可以直接使用“+”运算符进行连接运算。选项B)String是一种Object,而不是简单的字符数组,不能使用下标运算符取其值的某个元素,错误。选项C)toUpperCase()方法是String对象的一个方法,作用是将字符串的内容全部转换为大写并返回转换后的结果(String类型)。选项D)同选项A)。

  • 第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’ };  

    • A、(a == "Hello")
    • B、(a == b)
    • C、(a == c)
    • D、a.equals(b)
    • E、a.equals(d)

    正确答案:C,D

  • 第7题:

    String s= "hello";     String t = "hello";  char c[] = {’h’,’e’,’l’,’l’,’o’} ;     Which return true?()   

    • A、 s.equals(t);
    • B、 t.equals(c);
    • C、 s==t;
    • D、 t.equals(new String("hello"));
    • E、 t==c;

    正确答案:A,C,D

  • 第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");    

    • A、 Write Hello        Erase Hello
    • B、 Erase Hello        Write Hello
    • C、编译错误
    • D、运行时抛出异常

    正确答案:C

  • 第9题:

    Which SELECT statement will the result ‘ello World’ from the string ‘Hello World’?()

    • A、SELECT SUBSTR( ‘Hello World’,1) FROM dual;
    • B、SELECT INITCAP(TRIM (‘Hello World’, 1,1)) FROM dual;
    • C、SELECT LOWER(SUBSTR(‘Hello World’, 1, 1) FROM dual;
    • D、SELECT LOWER(SUBSTR(‘Hello World’, 2, 1) FROM dual;
    • E、SELECT LOWER(TRIM (‘H’ FROM ‘Hello World’)) FROM dual;

    正确答案:E

  • 第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?()
    A

     The program runs and prints “Hello”

    B

     An error causes compilation to fail.

    C

     The program runs and prints “Hello world!”

    D

     The program runs but aborts with an exception.


    正确答案: B
    解析: 暂无解析

  • 第11题:

    单选题
    下列哪项是String的字面量?()
    A

    “Hello”

    B

    ‘world’

    C

    /u2345

    D

    new String(“good”)


    正确答案: B
    解析: 暂无解析

  • 第12题:

    单选题
    下列选项中,()是正确的表达式。
    A

    <% String s = “hello world ” ;%>  

    B

    <% = “hello world ” ;%> 

    C

    <% = “hello world ” %>  

    D

    <% ! “hello world ” %>


    正确答案: D
    解析: 暂无解析

  • 第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.该程序段有语法错误


    正确答案:A
    解析:String类型可以直接使用“+”进行连接运算。

  • 第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";


    正确答案:B

  • 第17题:

    下列选项中,()是正确的表达式。 

    • A、<% String s = “hello world ” ;%>  
    • B、<% = “hello world ” ;%> 
    • C、<% = “hello world ” %>  
    • D、<% ! “hello world ” %>

    正确答案:C

  • 第18题:

    有语句String s=”hello world”; ,以下操作哪个是不合法的()

    • A、int i=s.length();
    • B、s>>>=3;
    • C、String ts=s.trim();
    • D、String t=s+”!”

    正确答案:B

  • 第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?()    

    • A、 The program runs and prints “Hello”
    • B、 An error causes compilation to fail.
    • C、 The program runs and prints “Hello world!”
    • D、 The program runs but aborts with an exception.

    正确答案:A

  • 第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?() 

    • A、The program runs and prints “Hello”
    • B、An error causes compilation to fail.
    • C、The program runs and prints “Hello world!”
    • D、The program runs but aborts with an exception.

    正确答案:A

  • 第21题:

    单选题
    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?()
    A

    The program runs and prints “Hello”

    B

    An error causes compilation to fail.

    C

    The program runs and prints “Hello world!”

    D

    The program runs but aborts with an exception.


    正确答案: D
    解析: 暂无解析

  • 第22题:

    多选题
    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’ };
    A

    (a == Hello)

    B

    (a == b)

    C

    (a == c)

    D

    a.equals(b)

    E

    a.equals(d)


    正确答案: C,D
    解析: 暂无解析

  • 第23题:

    多选题
    String s= "hello";     String t = "hello";  char c[] = {’h’,’e’,’l’,’l’,’o’} ;     Which return true?()
    A

    s.equals(t);

    B

    t.equals(c);

    C

    s==t;

    D

    t.equals(new String(hello));

    E

    t==c;


    正确答案: E,A
    解析: 这个在前面第10题的equals()方法和==操作符的讨论中论述过。==操作符比较的是操作符两端的操作数是否是同一个对象,而String的equals()方法比较的是两个String对象的内容是否一样,其参数是一个String对象时才有可能返回true,其它对象都返回假。需要指出的是由于s和t并非使用new创建的,他们指向内存池中的同一个字符串常量,因此其地址实际上是相同的(这个可以从反编译一个简单的测试程序的结果得到,限于篇幅不列出测试代码和反编译的分析),因此答案c也是正确的。