class Sock2 {   String color;   public boolean equals(Object o) {   return color.equals(((Sock2)o).color);   } }   class TestSocks {   public static void main(String [] args) {   Sock2 s1 = new Sock2(); s1.color = "blue";   Sock2 s2 = new Sock2(); s2.col

题目

class Sock2 {   String color;   public boolean equals(Object o) {   return color.equals(((Sock2)o).color);   } }   class TestSocks {   public static void main(String [] args) {   Sock2 s1 = new Sock2(); s1.color = "blue";   Sock2 s2 = new Sock2(); s2.color = "blue";   if (s1.equals(s2)) System.out.print("equals ");   if (s1 == s2) System.out.print("== ");   }   }   结果为:()      

  • A、==
  • B、equals
  • C、equals ==
  • D、无结果输出

相似考题
参考答案和解析
正确答案:B
更多“class Sock2 {   String color;   public boolean equals(Object o) {   return color.equals(((Sock2)o).color);   } }   class TestSocks {   public static void main(String [] args) {   Sock2 s1 = new Sock2(); s1.color = "blue";   Sock2 s2 = new Sock2(); s2.colo”相关问题
  • 第1题:

    下列程序的执行结果是______。 public class Test9 { public static void main(String[] args) { String s1 = new String("I am a girl"); String s2 = new String("I am a girl"); System.out.println (s1.equal (s2)); } }

    A.true

    B.假

    C.I amgirl

    D.都不正确


    正确答案:A
    解析:如果需要比较两个对象的值是否相同,则可以调用equal()方法,如果被比较的两个对象相等,则返回true,否则返回false。

  • 第2题:

    阅读下面程序 public class My Val{ public static void main(String args[]){ My Val m=new My Val(); m. amethod(); } public void amethod(){ boolean b[]=new Boolean[5]; } } 程序编译或运行结果是

    A.1

    B.null

    C.

    D.编译不能过


    正确答案:C
    解析:编译能通过,但不在控制台输出任何信息。程序只是实例化了一个布尔类型的数组,且由于此数组为局部变量,不会自动初始化,故其中值都为null。

  • 第3题:

    下列代码的执行结果是( )。 public class Test{ public static void main String args[]){ String s1=new String("welcome"); String s2=new String("welcome"); System.out.println(s1==s2); System.out.println(s1.equals(s2)); } }

    A.false,false

    B.false,true

    C.true,true

    D.true,false


    正确答案:B

  • 第4题:

    public class Something {

    public static void main(String[] args) {

    Something s = new Something();

    System.out.println("s.doSomething() returns " + doSomething());

    }

    public String doSomething() {

    return "Do something ...";

    }

    }

    看上去很完美。


    正确答案:

     

    错。看上去在main 里call doSomething 没有什么问题,毕竟两个methods 都在同一个

    class 里。但仔细看,main 是static 的。static method 不能直接call non-static methods。可改

    成"System.out.println("s.doSomething() returns " + s.doSomething());"。同理,static method 不

    能访问non-static instant variable。

  • 第5题:

    下列语句输出结果为( )。 public class test { public static void main(StringArgsl[]) { String s1=new String("How"); String s2=new String("How"); System.out.println(!(s1==s2)); } }

    A.false

    B.true

    C.1

    D.0


    正确答案:B

  • 第6题:

    下列语句输出结果为( )。public class test{public static void main(String args []){String s1=new String("How");String s2=new String("How");System.out.println(!(s1 ==s2));}

    A.false

    B.true

    C.1

    D.0


    正确答案:B

  • 第7题:

    阅读下面程序 public class Operators And Expressions( void equals Methodl{ Stringsl一new String("how are you"); Strings2=new String("how are you"); System.out.println(s1= =s2): } public static void main(String args[]){ Operators And Expressions Oper And Exp=new Oper- ators And Expressions ; //用于复合类型数据的”= =”运算符 OperAndExp.equalsMethodl; } } 程序运行结果是( )。

    A.= =

    B.true

    C.false

    D.equal


    正确答案:C
    C。【解析】本题考查的是Java语言中运算符的特殊用法。相等比较运算符"==",不仅可用于基本类型的数据之间的比较,还可以用于复合类型数据之间的比较。但是,对于复合类型数据的"=="运算,其比较的目标是两个操作数是否是同一个对象。如果需要比较两个对象的值是否相同,则可以调用equals方法。题目中函数equalsMethodl的代码非常简单,首先生成两个字符串对象sl和s2,然后输出表达式"sl==s2"的结果。根据上面介绍的相等比较运算符的用法,字符串对象sl和s2是复合类型数据,因此表达式"sl==s2"比较的是sl和s2是否同一个对象。显然,sl和s2虽然具有相同的值,即"howareyou",但它们是两个不同的对象。因此,本题的正确答案是C。

  • 第8题:

    程序员正在进行一个项目,必须实现equals方法与所给的hashCode方法协调运行:  public int hashCode() {    return (size.hashCode() + color.hashCode()) * 17;    }    哪一个equals方法支持此目标?() 

    • A、 无法确定
    • B、 public boolean equals(Object o) {  Sock s = (Sock) o; return size.equals(s.size);}
    • C、 public boolean equals(Object o) {  Sock s = (Sock) o; return color.equals(s.color);}
    • D、 public boolean equals(Object o) {  Sock s = (Sock) o; return size.equals(s.size) &&color.equals(s.color);  }

    正确答案:D

  • 第9题:

    public class Test {  public static void main(String args[]) {  class Foo {  public int i = 3; }  Object o = (Object)new Foo();   Foo foo = (Foo)o;  System.out.println(“i = “ + foo.i); }  }  What is the result?()  

    • A、 i = 3
    • B、 Compilation fails.
    • C、 A ClassCastException is thrown at line 6.
    • D、 A ClassCastException is thrown at line 7.

    正确答案:A

  • 第10题:

    单选题
    public class X {  public static void main (String[]args)  {  String s1 = new String (“true”);  Boolean b1 = new Boolean (true);  if (s2.equals(b1))   {  System.out.printIn(“Equal”);  }  }  }      What is the result?()
    A

     The program runs and prints nothing.

    B

     The program runs and prints “Equal”

    C

     An error at line 5 causes compilation to fail.

    D

     The program runs but aborts with an exception.


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

  • 第11题:

    单选题
    class Sock2 {   String color;   public boolean equals(Object o) {   return color.equals(((Sock2)o).color);   } }   class TestSocks {   public static void main(String [] args) {   Sock2 s1 = new Sock2(); s1.color = "blue";   Sock2 s2 = new Sock2(); s2.color = "blue";   if (s1.equals(s2)) System.out.print("equals ");   if (s1 == s2) System.out.print("== ");   }   }   结果为:()
    A

    ==

    B

    equals

    C

    equals ==

    D

    无结果输出


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

  • 第12题:

    单选题
    程序员正在进行一个项目,必须实现equals方法与所给的hashCode方法协调运行:  public int hashCode() {    return (size.hashCode() + color.hashCode()) * 17;    }    哪一个equals方法支持此目标?()
    A

     无法确定

    B

     public boolean equals(Object o) {  Sock s = (Sock) o; return size.equals(s.size);}

    C

     public boolean equals(Object o) {  Sock s = (Sock) o; return color.equals(s.color);}

    D

     public boolean equals(Object o) {  Sock s = (Sock) o; return size.equals(s.size) &&color.equals(s.color);  }


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

  • 第13题:

    阅读下面程序 public class OperatorsAndExpressions{ void equalsMethodl(){ String s1=new String("how are you"); String s2=new String("how are you"); System.out.println(s1==s2); } public static void main(String args[]){ OperatorsAndExpressionsOperAndExp=new OperatorsAndExpressions(); //用于复合类型数据的“==”运算符 OperAndExp.equalsMethod1(); } } 程序运行结果是

    A.=

    B.true

    C.false

    D.equal


    正确答案:C
    解析:本题考查的是Java语言中运算符的特殊用法。相等比较运算符“=”,不仅可用于基本类型的数据之间的比较,还可以用于复合数据类型之间的比较。但是,对于复合类型数据的“=”运算,其比较的目标是两个操作数是否是同一个对象。如果需要比较两个对象的值是否相同,.则可以调用equals()方法。题目中函数equalsMethodl()的代码非常简单,首先生成两个字符串对象s1和s2,然后输出表达式“s1=s2”的结果。根据上面介绍的相等比较运算符的用法,字符串对象s1和s2是复合数据类型,因此表达式“s1=s2”比较的是s1和s2是否同一个对象。显然,s1和s2虽然具有相同的值,即“how are you”,但它们是两个不同的对象。因此,本题的正确答案是C。

  • 第14题:

    下列语句输出结果为( )。 public class test { public static void main(String args[]) String s1 =newString("HOW"); String s2=newString("How"): System.out.pnntln(!(s1.equals(s2))); } }

    A.假

    B.真

    C.0

    D.1


    正确答案:A

  • 第15题:

    public class Something {

    public static void main(String[] args) {

    Other o = new Other();

    new Something().addOne(o);

    }

    public void addOne(final Other o) {

    o.i++;

    }

    }

    class Other {

    public int i;

    }

    和上面的很相似,都是关于final 的问题,这有错吗?


    正确答案:

     

    正确。在addOne method 中,参数o 被修饰成final。如果在addOne method 里我们修

    改了o 的reference

    (比如: o = new Other();),那么如同上例这题也是错的。但这里修改的是o 的member vairable

    (成员变量),而o 的reference 并没有改变。

  • 第16题:

    阅读下面程序 public class OperatorsAndExpressions { void equalsMethodl(){ String s1=new String("how are you"); String s2=new String("how are you"); System.out.println(s1==s2); } public static void main(String args[]){ OperatorsAndExpressions perAndExp=new OperatorsAndExpressions(); OperAndExp.equalsMethod1(); } } 程序运行结果是( )。

    A. ==

    B.true

    C.假

    D.equal


    正确答案:C

  • 第17题:

    下列代码段的执行结果是( )。 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。

  • 第18题:

    下列代码的执行结果是______。 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

  • 第19题:

    Which declarations will allow a class to be started as a standalone program?()  

    • A、public void main(String args[])
    • B、public void static main(String args[])
    • C、public static main(String[] argv)
    • D、final public static void main(String [] array)
    • E、public static void main(String args[])

    正确答案:D,E

  • 第20题:

    class Sock {  String size;  String color;  public boolean equals(Object o) {  Sock s = (Sock) o;  return color.equals(s.color);   }  // insert code here  }  哪两个满足 hashCode 的约定?()

    • A、public int hashCode() { return 343; }
    • B、public int hashCode() { return size.hashCode (); }
    • C、public int hashCode() { return color.hashCode (); }
    • D、public int hashCode() { return (int) (Math.random() * 1000);

    正确答案:B,C

  • 第21题:

    public class X {  public static void main (String[]args)  {  String s1 = new String (“true”);  Boolean b1 = new Boolean (true);  if (s2.equals(b1))   {  System.out.printIn(“Equal”);  }  }  }      What is the result?()  

    • A、 The program runs and prints nothing.
    • B、 The program runs and prints “Equal”
    • C、 An error at line 5 causes compilation to fail.
    • D、 The program runs but aborts with an exception.

    正确答案:A

  • 第22题:

    多选题
    class Sock {  String size;  String color;  public boolean equals(Object o) {  Sock s = (Sock) o;  return color.equals(s.color);   }  // insert code here  }  哪两个满足 hashCode 的约定?()
    A

    public int hashCode() { return 343; }

    B

    public int hashCode() { return size.hashCode (); }

    C

    public int hashCode() { return color.hashCode (); }

    D

    public int hashCode() { return (int) (Math.random() * 1000);


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

  • 第23题:

    单选题
    public class Test {  public static void main(String args[]) {  class Foo {  public int i = 3; }  Object o = (Object)new Foo();   Foo foo = (Foo)o;  System.out.println(“i = “ + foo.i); }  }  What is the result?()
    A

     i = 3

    B

     Compilation fails.

    C

     A ClassCastException is thrown at line 6.

    D

     A ClassCastException is thrown at line 7.


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