public class Something {void doSomething () {private String s = "";int l = s.length();}}有错吗?

题目

public class Something {

void doSomething () {

private String s = "";

int l = s.length();

}

}

有错吗?


相似考题
更多“public class Something {void doSomething () {private String s = "";int l = s.length();}}有错吗?”相关问题
  • 第1题:

    下面程序的执行结果为 ‘ #include"iostream" using namespace std; class A { int a; public: void Sera(int x){a=x;} void Display_a(){cout<<a<<endl;} }; class B { int b; public: void Setb(int x){ b=x;} void Dispaly_b() {cout<<b<<endl;} }; class C:public A,private B { private: int c; public: void Setc(int x,int y,int z) { c=z;Sera(x);Serb(y);} void Display_c(){cout<<c<<endl;} }; ① void main() ② { ③ C cc; ④ cc.Seta(1); ⑤ cc.Display_a(); ⑥ cc.Setc(2,2,3); ⑦ cc.Dispaly_b(); ⑧ cc.Display_c(); }

    A.输出为2 2 3

    B.有错误在第5行

    C.输出为1 2 3

    D.有错误在第7行


    正确答案:D
    解析:private继承不能调用Display_b函数,public继承可以调用基类中非private成员。注意:类的继承方式public,proctected和private的区别。

  • 第2题:

    下列程序段运行的结果为 public class Test{ static void print(String s,int i){ System.out.println("String:"+s+",int:"+i); } static void print(int i, String s){ System.out.println("int:"+i+",String:"+s); } public static void main(String [] args){ print(99,"Int first"); } }

    A.String:Stringfirst,int:11

    B.int:11,String:Int first

    C.String:String first,int:99

    D.int:99,String:int first


    正确答案:D
    解析:本题考查考生阅读程序的能力。JavaApplication都是以main()方法作为入口,首先执行的是print(99,“Intfirst”),根据构造方法的参数类型选择调用方法,这里调用的是print(inti,Strings)方法,因此输出的是int:99,String:Intfirst。

  • 第3题:

    abstract class Something {

    private abstract String doSomething ();

    }

    这好像没什么错吧?


    正确答案:

     

    错。abstract 的methods 不能以private 修饰。abstract 的methods 就是让子类implement(实

    现)具体细节的,怎么可以用private 把abstract

    method 封锁起来呢? (同理,abstract method 前不能加final)。

  • 第4题:

    class Something {

    int i;

    public void doSomething() {

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

    }

    }

    有什么错呢? 看不出来啊。


    正确答案:

     

    正确。输出的是"i = 0"。int i 属於instant variable (实例变量,或叫成员变量)。instant

    variable 有default value。int 的default value 是0。

  • 第5题:

    若有以下程序:includeusing namespace std;class A{private:int a; public:void seta

    若有以下程序: #include<iostream> using namespace std; class A { private: int a; public: void seta(int x) { a=x; } void showa() { cout<<a<<","; } }; class B { private: int b; public: void setb(int x) { b=x; } void showb() { cout<<b<<",”; } }; class C:pUblic A,private B { private: int c; public: void setc(int x,int y,int z) { c=z; seta(x); setb(y); } void showc() { showa(); showb(); cout<<c<<end1; } }; int main() { Cc; c.setc(1,2,3); c.showc(); retrun 0; } 程序执行后的输出结果是

    A.1,2,3

    B.1,1,1

    C.2,2,2

    D.3,3,3


    正确答案:A
    解析:本题考核派生类的应用。本题中类A和类B都是基类。而类C从类A公有派生,从类B处私有派生。所以类C中的函数成员可以访问类A和类B中的公有成员。在类C的函数成员setc中,调用基类A的函数成员seta对A的数据成员a赋值,还调用了基类B的函数成员setb对类B的数据成员b赋值,然后对类C自己的数据成员c赋值。在类C的函数成员showc中,调用基类A的函数成员showa显示数据成员a的值,还调用基类B的函数成员showb显示数据成员b的值,然后输出类C自己的数据成员c的值。在主函数main中,先定义派生类的对象c,然后调用setc对c中的数据成员赋值,然后输出赋值结果。所以程序最后输出应该为:1,2,3。

  • 第6题:

    设有类定义如下:

    class InOut{

    String s= new String("Between");

    public void amethod(final int iArgs){

    int iam;

    class Bicycle{

    public void sayHello(){

    //Here

    }

    }

    }

    public void another(){

    int iOther;

    }

    }

    以下哪些语句可以安排在//Here处 ?

    A. System.out.println(s);

    B.System.out.println(iOther);

    C. System.out.println(iam);

    D. System.out.println(iArgs);


    正确答案:AD

  • 第7题:

    public class Parent{     public void change(int x){} }  public class Child extends Parent{     //覆盖父类change方法  }  下列哪个声明是正确的覆盖了父类的change方法?() 

    • A、 protected void change(int x){}
    • B、 public void change(int x, int y){}
    • C、 public void change(String s){}
    • D、 public void change(int x){}

    正确答案:D

  • 第8题:

    现有:  interface Data {public void load();}  abstract class Info {public abstract void load();}      下列类定义中正确使用Data和Info的是哪项?() 

    • A、 public class Employee implements Info extends Data { public void load(){/*dosomething*/}     }
    • B、public class Employee extends Inf.implements Data{ public void load() {/*do something*/}     }
    • C、public class Empl.yee implements Inf extends Data{ public void Data.1oad(){* do something*/}     public void load(){/*do something*/}     }
    • D、public class Employee extends Inf implements Data  {  public void Data.1oad()  {/*do something*/)     public void info.1oad(){/*do something*/}    }

    正确答案:B

  • 第9题:

    public class NamedCounter {  private final String name;  private int count;  public NamedCounter(String name) { this.name = name; }  public String getName() { return name; }  public void increment() { coount++; }  public int getCount() { return count; } public void reset() { count = 0; } }  Which three changes should be made to adapt this class to be used safely by multiple threads? ()

    • A、 declare reset() using the synchronized keyword
    • B、 declare getName() using the synchronized keyword
    • C、 declare getCount() using the synchronized keyword
    • D、 declare the constructor using the synchronized keyword
    • E、 declare increment() using the synchronized keyword

    正确答案:A,C,E

  • 第10题:

    多选题
    public class Parent {  public int addValue( int a, int b) {     int s;     s = a+b;     return s;     }     }  class Child extends Parent {  }  Which methods can be added into class Child?()
    A

    int addValue( int a, int b ){// do something...}

    B

    public void addValue (){// do something...}

    C

    public int addValue( int a ){// do something...}

    D

    public int addValue( int a, int b )throws MyException {//do something...}


    正确答案: B,D
    解析: 此题涉及方法重载(overload),方法重写(override)以及类派生时方法重写的规则。方法重载的规则是:
    一、参数列表必须不同,个数的不同完全可以,如果个数相同则参数类型的不同不能引起歧意,例如int 和long,float和double就不能作为唯一的类型不同;
    二、返回值可以不同,但是不能是重载时唯一的不同点(这点和c++中不同,c++中返回类型必须一致)。
    方法重写发生在类继承时,子类可以重写一个父类中已有的方法,必须在返回类型和参数列表一样时才能说是重写,否则就是重载,java中方法重写的一个重要而且容易被忽略的规则是重写的方法的访问权限不能比被重写的方法的访问权限低!重写的另一个规则是重写的方法不能比被重写的方法抛弃(throws)更多种类的异常,其抛弃的异常只能少,或者是其子类,不能以抛弃异常的个数来判断种类,而应该是异常类层次结果上的种类。此题中答案a的错误就是重写的访问权限比被重写的方法的低,而b,c都属于重载,d的错误在于比被重写的方法抛弃了更多种类的异常。

  • 第11题:

    单选题
    现有      public class Parentt      public void change (int x){)     )      public class Child extends Parent{     //覆盖父类change方法     }      下列哪个声明是正确的覆盖了父类的change方法?()
    A

      protected void change (int x){}

    B

      public void change(int x,  int y){}

    C

      public void change (int x){}

    D

      public void change (String s){}


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

  • 第12题:

    单选题
    现有:  interface Data {public void load();}  abstract class Info {public abstract void load();}      下列类定义中正确使用Data和Info的是哪项?()
    A

     public class Employee implements Info extends Data { public void load(){/*dosomething*/}     }

    B

    public class Employee extends Inf.implements Data{ public void load() {/*do something*/}     }

    C

    public class Empl.yee implements Inf extends Data{ public void Data.1oad(){* do something*/}     public void load(){/*do something*/}     }

    D

    public class Employee extends Inf implements Data  {  public void Data.1oad()  {/*do something*/)     public void info.1oad(){/*do something*/}    }


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

  • 第13题:

    下面这个程序的结果是includeclass A{private:int a;public:void seta( );int geta

    下面这个程序的结果是 #include<iostream.h> class A { private: int a; public: void seta( );int geta( );}; void A::seta( ) { a = 1;} int A::geta( ) {return a;} class

    A.1

    B.2

    C.随机输出1或2

    D.程序有错


    正确答案:D
    解析:在类A中有geta()函数,在类B中也有geta()函数,类C继承了类A和类B,这样就产生了二义性,所以程序会出错。

  • 第14题:

    请在每条横线处填写一个语句,使程序的功能完整,且输出结果为91 1。

    注意:请勿改动main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。

    源程序文件代码清单如下:

    public class Outer

    {

    public static void main (String args[]

    {

    Outer i = new Outer();

    i,taskInner();

    }

    public class Inner

    {

    private int size;

    public void doSomething(int size)

    {

    _____________//访问局部变量

    this. size++; //访问内部类的成员变量

    _____________//访问外部类的成员变量

    System.out.println(size+" "+this.size+" "+Outer.this.size);

    }

    }

    public void taskInner()

    {

    ___________

    k.doSomething(8);

    }

    private static int size;

    }


    正确答案:size++; Outer.this.size++: Inner k=new Inner();
    size++; Outer.this.size++: Inner k=new Inner(); 解析:本题主要考查内部类的概念,super,this关键字的用法。解答本题的关键是熟练掌握super, this关键字的用法。在本题中size++;语句是访问局部变量size,Outer.this.size++;语句的功能是访问外部类的成员变量size,InnerK=new Inner();语句的功能是生成内部类Inner的对象K。

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

  • 第17题:

    3下列程序段运行的结果为( )。 public class Test{ static void print(String s,int i){ System.out.pdntlnC String: "+s+",int:"+i); } static void print(iht i,String s){ System.out.prinflnCint:"+i+",gtring:"+s); } public static void main(String[] args){ print(99,"Int first"); } }

    A.String:String first,int: 11

    B.int: 11,String:Int first

    C.String:String first,int:99

    D. int:99,Stfing:Int first


    正确答案:D

  • 第18题:

    public class X {  public X aMethod() { return this;}  }  public class Y extends X {  }  Which two methods can be added to the definition of class Y?()

    • A、 public void aMethod() {}
    • B、 private void aMethod() {}
    • C、 public void aMethod(String s) {}
    • D、 private Y aMethod() { return null; }
    • E、 public X aMethod() { return new Y(); }

    正确答案:C,E

  • 第19题:

    下列代码正确的是哪项?() 

    • A、 public class Session implements Runnable, Clonable{   public void run ();public Object clone () ; }
    • B、 public class Session extends Runnable, Cloneable {  public void run() {/*dosomething*/}       public Object clone() {/*make a copy*/} }
    • C、 public abstract class Session implements Runnable, Clonable {       public void run() {/*do something*/}       public Object clone() {/*make a copy*/}        }
    • D、 public class Session implements Runnable, implements Clonable {       public void run() {/*do something*/}       public Object clone() {/*make a copy*/}       }

    正确答案:C

  • 第20题:

    public class Parent {  public int addValue( int a, int b) {     int s;     s = a+b;     return s;     }     }  class Child extends Parent {  }  Which methods can be added into class Child?()   

    • A、 int addValue( int a, int b ){// do something...}
    • B、 public void addValue (){// do something...}
    • C、 public int addValue( int a ){// do something...}
    • D、 public int addValue( int a, int b )throws MyException {//do something...}

    正确答案:B,C

  • 第21题:

    public abstract class Shape {  private int x;  private int y;  public abstract void draw();  public void setAnchor(int x, int y) {  this.x = x;  this.y = y;  }  }  Which two classes use the Shape class correctly?()

    • A、 public class Circle implements Shape { private int radius; }
    • B、 public abstract class Circle extends Shape { private int radius; }
    • C、 public class Circle extends Shape { private int radius; public void draw(); }
    • D、 public abstract class Circle implements Shape { private int radius; public void draw(); }
    • E、 public class Circle extends Shape { private int radius;public void draw() {/* code here */} }
    • F、 public abstract class Circle implements Shape { private int radius;public void draw() { / code here */ } }

    正确答案:B,E

  • 第22题:

    单选题
    下列代码正确的是哪项?()
    A

     public class Session implements Runnable, Clonable{   public void run ();public Object clone () ; }

    B

     public class Session extends Runnable, Cloneable {  public void run() {/*dosomething*/}       public Object clone() {/*make a copy*/} }

    C

     public abstract class Session implements Runnable, Clonable {       public void run() {/*do something*/}       public Object clone() {/*make a copy*/}        }

    D

     public class Session implements Runnable, implements Clonable {       public void run() {/*do something*/}       public Object clone() {/*make a copy*/}       }


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

  • 第23题:

    多选题
    public abstract class Shape {  private int x;  private int y;  public abstract void draw();  public void setAnchor(int x, int y) {  this.x = x;  this.y = y;  }  }  Which two classes use the Shape class correctly?()
    A

    public class Circle implements Shape { private int radius; }

    B

    public abstract class Circle extends Shape { private int radius; }

    C

    public class Circle extends Shape { private int radius; public void draw(); }

    D

    public abstract class Circle implements Shape { private int radius; public void draw(); }

    E

    public class Circle extends Shape { private int radius;public void draw() {/* code here */} }

    F

    public abstract class Circle implements Shape { private int radius;public void draw() { / code here */ } }


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