单选题import java.util.*;  class KeyMaster {  public int i;  public KeyMaster(int i) { this.i = i; }  public boolean equals(Object o) { return i == ((KeyMaster)o).i; }  public int hashCode() { return i; }  }  public class MapIt {  public static void main(Str

题目
单选题
import java.util.*;  class KeyMaster {  public int i;  public KeyMaster(int i) { this.i = i; }  public boolean equals(Object o) { return i == ((KeyMaster)o).i; }  public int hashCode() { return i; }  }  public class MapIt {  public static void main(String[] args) {  Set set = new HashSet();  KeyMaster k1 = new KeyMaster(1);  KeyMaster k2 = new KeyMaster(2);  set.add(k1); set.add(k1);  set.add(k2); set.add(k2);  System.out.print(set.size() + “:”);  k2.i = 1;  System.out.print(set.size() + “:”);  set.remove(k1);  System.out.print(set.size() + “:”);  set.remove(k2);  System.out.print(set.size()); }  }  What is the result?()
A

 4:4:2:2

B

 4:4:3:2

C

 2:2:1:0

D

 2:2:0:0

E

 2:1:0:0

F

 2:2:1:1

G

 4:3:2:1


相似考题
更多“单选题import java.util.*;  class KeyMaster {  public int i;  public KeyMaster(int i) { this.i = i; }  public boolean equals(Object o) { return i == ((KeyMaster)o).i; }  public int hashCode() { return i; }  }  public class MapIt {  public static void main(Str”相关问题
  • 第1题:

    若类A和类B的定义如下

    class A

    {

    int i,j;

    public:

    int geti( )

    {

    return i;

    }

    };

    class B:public A

    {

    int k:

    public:

    void make( )

    {

    k=i*j;

    }

    };

    则上述定义中非法的语句是

    A.k=i*j

    B.int k;

    C.return i;

    D.void make()


    正确答案:A
    解析:本题考核派生类的定义和访问权限。变量i和j都是基类A的私有变量,它们是隐蔽的,在派生类中不能直接访问。

  • 第2题:

    若类A和类B的定义如下: class A { int i,j; public: int geti () { return i; } }; class B : public A { int k; public: void make () { k=i*j; } }; 则上述定义中非法的语句是

    A.k=i*j;

    B.int k;

    C.return i;

    D.void make()


    正确答案:A
    解析:本题考核派生类的定义和访问权限。变量i和j都是基类A的私有变量,它们是隐蔽的,在派生类中不能直接访问。

  • 第3题:

    若类A和类B的定义如下:includeclass A{int i*j;public:int geti(){return i;}};class

    若类A和类B的定义如下: #include<malloc.h> class A { int i*j; public: int geti() { return i; } }; class B: public A { int k; public: void make() { k=i*j; } ); 则上述定义中非法的表达式是( )。

    A.k=i*j;

    B.int k;

    C.return i;

    D.void make();


    正确答案:A
    解析:因为派生类不能访问基类的私有成员i和j(默认情况下,成员的属性为私有),所以表达式k=i*j是非法的。其余的访问权限都是许可的。

  • 第4题:

    若类A和类B的定义如下: class A { int i,j; public: int geti() { return i; } }; class B: public A { int k; public: void make() { k=i*j } }; 则上述定义中

    A.k=i*j;

    B.int k;

    C.return i;

    D.void make()


    正确答案:A
    解析:本题考核派生类的定义和访问权限。变量i和j都是基类A的私有变量,它们是隐蔽的,在派生类中不能直接访问。

  • 第5题:

    以下程序的运行结果为?

    class ValHold{

    public int i = 10;

    }

    public class ObParm{

    public static void main(String argv[]){

    ObParm o = new ObParm();

    o.amethod();

    }

    public void amethod(){

    int i = 99;

    ValHold v = new ValHold();

    v.i=30;

    another(v,i);

    System.out.print( v.i );

    }

    public void another(ValHold v, int i){

    i=0;

    v.i = 20;

    ValHold vh = new ValHold();

    v = vh;

    System.out.print(v.i);

    System.out.print(i);

    }

    }

    A.10030

    B. 20030

    C. 209930

    D. 10020


    正确答案:D

  • 第6题:

    Which are syntactically valid statement at// point x?()     class Person {     private int a;  public int change(int m){  return m;  }     }  public class Teacher extends Person {     public int b;  public static void main(String arg[]){     Person p = new Person();     Teacher t = new Teacher();    int i;  // point x     }    } 

    • A、 i = m;
    • B、 i = b;
    • C、 i = p.a;
    • D、 i = p.change(30);
    • E、 i = t.b.

    正确答案:D,E

  • 第7题:

    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

  • 第8题:

    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

  • 第9题:

    public class test (      private static int j = 0;  private static boolean methodB(int k) (  j += k;  return true;  )  public static void methodA(int  i)(  boolean b:     b = i < 10 | methodB (4);  b = i < 10 || methodB (8);  )  public static void main (String args[])(   methodA (0);  system.out.printIn(j);  )  )   What is the result?()  

    • A、 The program prints “0”
    • B、 The program prints “4”
    • C、 The program prints “8”
    • D、 The program prints “12”
    • E、 The code does not complete.

    正确答案:B

  • 第10题:

    单选题
    public class Test {  public int aMethod() {  static int i = 0;  i++;  return i;  }  public static void main (String args[]) {  Test test = new Test();  test.aMethod();  int j = test.aMethod();  System.out.println(j);  }  }  What is the result?()
    A

     0

    B

     1

    C

     2

    D

     Compilation fails.


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

  • 第11题:

    单选题
    public class SyncTest {  private int x;  private int y;  private synchronized void setX( int i ) { x = i; }  private synchronized void setY( int i ) { y = i; }  public void setXY( int i ) { setX(i); setY(i); }  public synchronized boolean check() { return x != y; }  }   Under which condition will check return true when called from a different class? ()
    A

     check can never return true.

    B

     check can return true when setXY is called by multiple threads.

    C

     check can return true when multiple threads call setX and setY separately.

    D

     check can return true only if SyncTest is changed to allow x and y to be set separately.


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

  • 第12题:

    单选题
    public class test (      private static int j = 0;  private static boolean methodB(int k) (  j += k;  return true;  )  public static void methodA(int  i)(  boolean b:     b = i < 10 | methodB (4);  b = i < 10 || methodB (8);  )  public static void main (String args[])(   methodA (0);  system.out.printIn(j);  )  )   What is the result?()
    A

     The program prints “0”

    B

     The program prints “4”

    C

     The program prints “8”

    D

     The program prints “12”

    E

     The code does not complete.


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

  • 第13题:

    下列程序的功能是为变量赋值,程序运行后,输出i=51。请改动main方法中的错误,使程序能够正确编译、运行并输出正确的结果。

    注意:不改动程序结构。

    class A

    {

    private int a;

    public void setA (int x)

    {

    a=x;

    }

    public int getA()

    {

    return a;

    }

    }

    public class MethodTest

    {

    public static void main(String args[])

    {

    A a=A();

    a.getA(51);

    int i=a.getA();

    System.out.println ("i="+i);

    }

    }


    正确答案:改正后的main方法如下: public static void main(String args[]) { A a=new A(); a.getA(51); int i=a.getA(); System.out.println("i="+i); }
    改正后的main方法如下: public static void main(String args[]) { A a=new A(); a.getA(51); int i=a.getA(); System.out.println("i="+i); } 解析:本题综合考查类及类成员的定义与使用方面的知识。该程序中定义了两个类:A和MethodTest,类A中封装了一个私有的成员变量a和两个公有的方法setA和getA。在类MethodTest中包含了main方法。创建对象应使用new操作符来实例化对象,程序在创建对象a时未使用new,故存在错误。由于a是对象a的私有变量,在main方法中不能直接访问,只能通过对象a的公有方法setA和getA来访问。公有方法setA的功能是将传递回来的参数值赋给a,所以应当调用setA方法来为变量a赋值。

  • 第14题:

    有以下程序inclube class ClassOne{public: ClassOne(int v=O) { i=v;cout<

    有以下程序 #inclube <iostream.h> class ClassOne { public: ClassOne(int v=O) { i=v;cout<<i;} void print(){ cout<<i<<end1;} }; class ClassTwo { public: ClassTwo(int v=O) { i=v;cout<<i;} void print(){ cout<<i<<end1;} private: ClassOne myObj; int i; }; void main() { ClassTwo obj(1); obj.print(); }

    A.11

    B.111

    C.110

    D.101


    正确答案:A
    解析:类ClassTwo中定义了成员对象myObj,所以在构造类ClassTwo的对象obj时会先调用类ClassOne的构造函数输出0,然后调用ClassTwo的构造函数输出1。主函数最后调用obj的成员函数print()输出1。所以最后结果为011。

  • 第15题:

    若类A和类B的定义如下: class A [ int i,j; public: int geti() { return i; } }; class B:public A { int k; public: void make() { k=i*j; } }; 则上述定义中非法的表达式是

    A.k=i*j

    B.int k;

    C.retum i;

    D.void make()


    正确答案:A
    解析:本题考核派生类的定义和访问权限。变量i和j都是基类A的私有变量,它们是隐蔽的,在派生类中不能直接访问。

  • 第16题:

    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 并没有改变。

  • 第17题:

    下列程序的输出结果是______。 include class base { int x,y; public: base(int i,i

    下列程序的输出结果是______。

    include<iostream.h>

    class base

    {

    int x,y;

    public:

    base(int i,int j){x=i;y=j;}

    virtual int add( ){return x+y;}

    };

    class three:public base

    {

    int z;

    public:

    three(int i,int j,int k):base(i,j){z=k;)

    int add( ){return(base::add( )+z);}

    };

    void main( )

    {

    three*q=new three(10,20,30);

    cout<<q->add( )<<endl;

    }


    正确答案:60
    60 解析:本题考察继承中子类对父类的继承方式,注意子类的add成员函数,它直接使用了父类的成员函数进行运算。

  • 第18题:

    import java.util.*;  class KeyMaster {  public int i;  public KeyMaster(int i) { this.i = i; }  public boolean equals(Object o) { return i == ((KeyMaster)o).i; }  public int hashCode() { return i; }  }  public class MapIt {  public static void main(String[] args) {  Set set = new HashSet();  KeyMaster k1 = new KeyMaster(1);  KeyMaster k2 = new KeyMaster(2);  set.add(k1); set.add(k1);  set.add(k2); set.add(k2);  System.out.print(set.size() + “:”);  k2.i = 1;  System.out.print(set.size() + “:”);  set.remove(k1);  System.out.print(set.size() + “:”);  set.remove(k2);  System.out.print(set.size()); }  }  What is the result?() 

    • A、 4:4:2:2
    • B、 4:4:3:2
    • C、 2:2:1:0
    • D、 2:2:0:0
    • E、 2:1:0:0
    • F、 2:2:1:1
    • G、 4:3:2:1

    正确答案:F

  • 第19题:

    public class Test {  public static void leftshift(int i, int j) {  i<<=j;  }  public static void main(String args[])  {  int i = 4, j = 2;  leftshift(i, j);   System.out.printIn(i); }  }     What is the result?()  

    • A、 2
    • B、 4
    • C、 8
    • D、 16
    • E、 The code will not compile.

    正确答案:B

  • 第20题:

    public class Test {  public int aMethod() {  static int i = 0;  i++;  return i;  }  public static void main (String args[]) {  Test test = new Test();  test.aMethod();  int j = test.aMethod();  System.out.println(j);  }  }  What is the result?()  

    • A、 0
    • B、 1
    • C、 2
    • D、 Compilation fails.

    正确答案:D

  • 第21题:

    public class SyncTest {  private int x;  private int y;  public synchronized void setX (int i) (x=1;)  public synchronized void setY (int i) (y=1;)  public synchronized void setXY(int 1)(set X(i); setY(i);)  public synchronized Boolean check() (return x !=y;)  }  Under which conditions will check () return true when called from a different class?

    • A、 Check() can never return true.
    • B、 Check() can return true when setXY is called by multiple threads.
    • C、 Check() can return true when multiple threads call setX and setY separately.
    • D、 Check() can only return true if SyncTest is changed to allow x and y to be set separately.

    正确答案:A

  • 第22题:

    单选题
    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
    解析: 暂无解析

  • 第23题:

    单选题
    public class SyncTest {  private int x;   private int y;   public synchronized void setX (int i) (x=1;)   public synchronized void setY (int i) (y=1;)   public synchronized void setXY(int 1)(set X(i); setY(i);)   public synchronized Boolean check() (return x !=y;)   }   Under which conditions will check () return true when called from a different class?
    A

     Check() can never return true.

    B

     Check() can return true when setXY is called by multiple threads.

    C

     Check() can return true when multiple threads call setX and setY separately.

    D

     Check() can only return true if SyncTest is changed to allow x and y to be set separately.


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